Source of file TemplateOverviewDescription.php
Size: 10,429 Bytes - Last Modified: 2021-12-23T10:47:11+00:00
/var/www/docs.ssmods.com/process/src/code/dataobjects/TemplateOverviewDescription.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 | <?php /** *@author nicolaas[at]sunnysideup.co.nz *@description: more details per template. Records are auto-added *@link: http://localhost/www.mysite.co.nz/admin/templates/TemplateoverviewDescription/13/edit **/ class TemplateoverviewDescription extends DataObject { private static $db = array( "Title" => "Varchar", "Description" => "Text", "ToDoListHyperLink" => "Varchar(255)", "ClassNameLink" => "Varchar(120)" ); private static $has_one = array( "Image1" => "Image", "Image2" => "Image", "Image3" => "Image", "Image4" => "Image", "Image5" => "Image", "Image6" => "Image", "Image7" => "Image" ); private static $searchable_fields = array( "ClassNameLink" => "PartialMatchFilter" ); private static $summary_fields = array( "ClassNameLink", "ToDoListHyperLink" ); private static $field_labels = array( "ClassNameLink" => "Page Type Name", "ToDoListHyperLink" => "Link to To Do" ); private static $singular_name = 'Template Description'; private static $plural_name = 'Template Descriptions'; private static $default_sort = 'ClassNameLink ASC'; private static $indexes = array( "ClassNameLink" => true ); private static $casting = array( "Title" => "Varchar" ); /** * Location where we keep the template overview designs. * @var String */ private static $image_folder_name = "templateoverview_advanced/designz"; /** * Location where we keep the template overview designs. * @var String */ private static $image_source_folder = ""; public function canCreate($member = null) { return false; } public function canDelete($member = null) { return true; } public function ClassNameLinkFancy() { return implode(" ", preg_split('/(?<=\\w)(?=[A-Z])/', $this->ClassNameLink)); return preg_replace("/(?<=[^A-Z])([A-Z])/", "$1", $this->ClassNameLink); } public function Title() { return $this->getTitle(); } public function getTitle() { return $this->ClassNameLinkFancy(); } public function ModelAdminLink() { return TemplateoverviewDescriptionModelAdmin::get_full_url_segment().$this->ClassName."/EditForm/field/TemplateoverviewDescription/item/".$this->ID."/edit/"; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName("ClassNameLink"); $fields->removeByName("Image1"); $fields->removeByName("Image2"); $fields->removeByName("Image3"); $fields->removeByName("Image4"); $fields->removeByName("Image5"); $fields->addFieldToTab("Root.Design", new UploadField("Image1", "Design One")); $fields->addFieldToTab("Root.Design", new UploadField("Image2", "Design Two")); $fields->addFieldToTab("Root.Design", new UploadField("Image6", "Design Three")); $fields->addFieldToTab("Root.Instructions", new UploadField("Image3", "Instructions One")); $fields->addFieldToTab("Root.Instructions", new UploadField("Image4", "Instructions Two")); $fields->addFieldToTab("Root.Instructions", new UploadField("Image5", "Instructions Three")); $fields->addFieldToTab("Root.Instructions", new UploadField("Image7", "Instructions Four")); $fields->addFieldToTab("Root.Main", new HeaderField("ClassNameLinkInfo", "Details for: ".$this->ClassNameLink), "Description"); $fields->addFieldToTab("Root.Main", new LiteralField("BackLink", '<p><a href="'.$page->Link().'#sectionFor-'.$this->ClassNameLink.'">go back to template overview page</a> - dont forget to SAVE FIRST.</p>')); return $fields; } public function requireDefaultRecords() { parent::requireDefaultRecords(); $data = ClassInfo::subclassesFor("SiteTree"); $fileList = null; if ($this->Config()->get("image_source_folder")) { $fileList = CMSHelp::get_list_of_files($this->Config()->get("image_source_folder")); if (!is_array($fileList)) { $fileList = null; } elseif (!count($fileList)) { $fileList = null; } } if ($fileList) { $destinationDir = Director::baseFolder()."/assets/".$this->Config()->get("image_folder_name")."/"; $destinationFolder = Folder::find_or_make($this->Config()->get("image_folder_name")); } if ($data) { foreach ($data as $className) { $object = TemplateoverviewDescription::get() ->filter(array("ClassNameLink" => $className))->First(); if (!$object) { $object = new TemplateoverviewDescription(); $object->ClassNameLink = $className; $object->write(); DB::alteration_message("adding template description for $className", "created"); } else { $otherObjects = TemplateoverviewDescription::get() ->filter(array("ClassNameLink" => $className))->exclude(array("ID" => $object->ID)); foreach ($otherObjects as $otherObject) { DB::alteration_message("Deleting superfluous TemplateoverviewDescription with Class Name Link: $className", "deleted"); $otherObject->delete(); } } if ($fileList) { $i = 0; foreach ($fileList as $fileArray) { $explodeByDot = explode(".", $fileArray["FileName"]); if (is_array($explodeByDot) && count($explodeByDot)) { $base = $explodeByDot[0]; $explodeByUnderscore = explode("_", $base); if (is_array($explodeByUnderscore) && count($explodeByUnderscore)) { $base = $explodeByUnderscore[0]; $classNameOptionArray = array($className); for ($j = 0; $j < 10; $j++) { $classNameOptionArray[] = $className.$j; } foreach ($classNameOptionArray as $potentialBase) { if ($base == $potentialBase) { $i++; $filename = "".$this->Config()->get("image_folder_name")."/".$fileArray["FileName"]; if (!file_exists($destinationDir.$fileArray["FileName"])) { copy($fileArray["FullLocation"], $destinationDir.$fileArray["FileName"]); } $image = $Image::get() ->filter(array("ParentID" => $destinationFolder->ID, "Name" => $fileArray["FileName"]))->First(); if (!$image) { $image = new Image(); $image->ParentID = $destinationFolder->ID; $image->Filename = $filename; $image->Name = $fileArray["FileName"]; $image->Title = $fileArray["Title"]; $image->write(); } $fieldName = "Image$i"."ID"; if ($object->$fieldName != $image->ID) { $object->$fieldName = $image->ID; $object->write(); DB::alteration_message("adding image to $className: ".$image->Title." (".$image->Filename.") using $fieldName field.", "created"); } } } } } } } else { DB::alteration_message("no design images found for $className", "deleted"); } } } $helpDirectory = Director::baseFolder()."/".Config::inst()->get("CMSHelp", "help_file_directory_name")."/"; if (!file_exists($helpDirectory)) { mkdir($helpDirectory); } $this->createManifestExcludeFile($helpDirectory); $devDirectory = Director::baseFolder()."/".Config::inst()->get("CMSHelp", "dev_file_directory_name")."/"; if (!file_exists($devDirectory)) { mkdir($devDirectory); } $this->createManifestExcludeFile($devDirectory); $this->createHTACCESSDenyAll($devDirectory); } private function createManifestExcludeFile($dir) { $myFile = $dir.'_manifest_exclude'; if (!file_exists($myFile)) { $handle = fopen($myFile, 'w') or user_error('Cannot open file: '.$myFile); $data = ''; fwrite($handle, $data); } } private function createHTACCESSDenyAll($dir) { $myFile = $dir.'.htaccess'; if (!file_exists($myFile)) { $handle = fopen($myFile, 'w') or user_error('Cannot open file: '.$myFile); $data = ' Order Deny,Allow Deny from all '; fwrite($handle, $data); } } public function validate() { if ($this->ID) { if ( TemplateoverviewDescription::get() ->filter(array("ClassNameLink" => $this->ClassNameLink)) ->exclude(array("ID" => $this->ID)) ) { return new ValidationResult(false, _t("TemplateoverviewDescription.ALREADYEXISTS", "This template already exists")); } } return new ValidationResult(); } public function onBeforeWrite() { parent::onBeforeWrite(); } public function forTemplate() { return $this->ClassNameLink; } } |