Source of file FrontEndEditorExplanationsBaseClass.php
Size: 7,521 Bytes - Last Modified: 2021-12-23T10:41:44+00:00
/var/www/docs.ssmods.com/process/src/code/model/FrontEndEditorExplanationsBaseClass.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 | <?php class FrontEndEditorExplanationsBaseClass extends DataObject { private static $db = array( "ObjectClassName" => "Varchar(100)", "ObjectFieldName" => "Varchar(100)", "ShortDescription" => "Varchar(255)", "LongDescription" => "HTMLText", "DefaultValue" => "Varchar(255)" ); private static $indexes = array( "ObjectClassName" => true, "ObjectFieldName" => true, "Unique" => array( "type" => "unique", "value" => "\"ObjectClassName\", \"ObjectFieldName\"", ) ); private static $casting = array( "Title" => "Varchar", "ClassNameNice" => "Varchar", "FieldNameNice" => "Varchar", "HasLongDescription" => "Boolean", "HasLongDescriptionNice" => "Varchar" ); private static $searchable_fields = array( "ObjectClassName" => "PartialMatchFilter", "ShortDescription" => "PartialMatchFilter" ); private static $default_sort = "ObjectClassName ASC"; protected static $_cache_for_explanation_objects = []; /** * * @param string $className * @param string $type * * @return FrontEndEditorExplanationsBaseClass */ public static function add_or_find_item($className, $type = '') : FrontEndEditorExplanationsBaseClass { if (! $type || $type === 'FrontEndEditorExplanationsBaseClass') { user_error('A type must be provided!'); } $key = $type.'_'.$className; if (isset(self::$_cache_for_explanation_objects[$key])) { //do nothing } else { $filter = array( "ObjectClassName" => $className ); $obj = DataObject::get_one( $type, $filter, $cacheDataObjectGetOne = false ); if (! $obj) { $obj = $type::create($filter); } $id = $obj->write(); self::$_cache_for_explanation_objects[$key] = DataObject::get_one($type, ['ID' => $id]); } return self::$_cache_for_explanation_objects[$key]; } /** * Determine which properties on the DataObject are * searchable, and map them to their default {@link FormField} * representations. Used for scaffolding a searchform for {@link ModelAdmin}. * * Some additional logic is included for switching field labels, based on * how generic or specific the field type is. * * Used by {@link SearchContext}. * * @param array $_params * 'fieldClasses': Associative array of field names as keys and FormField classes as values * 'restrictFields': Numeric array of a field name whitelist * * @return FieldList */ public function scaffoldSearchFields($_params = null) { $fieldList = parent::scaffoldSearchFields($_params); $fieldLabels = $this->FieldLabels(); $objectNames = array_unique(FrontEndEditorExplanationsBaseClass::get()->column('ObjectClassName')); $newList = ['' => '-- ANY --']; foreach ($objectNames as $key => $value) { $newList[$value] = $this->getClassNameNice($value); } asort($newList); $fieldList->replaceField( 'ObjectClassName', DropdownField::create( 'ObjectClassName', $fieldLabels['ObjectClassName'], $newList ) ); $fieldList->push( TextField::create('ShortDescription', 'Short Description') ); //allow changes $this->extend('UpdateSearchFields', $fieldList, $_params); return $fieldList; } public function getCMSFields() { $fields = parent::getCMSFields(); $fieldLabels = $this->FieldLabels(); $fields->removeByName("ObjectClassName"); $fields->removeByName("ObjectFieldName"); $fields->removeByName("DefaultValue"); $fields->addFieldToTab("Root.Main", ReadonlyField::create("ObjectClassName", $fieldLabels['ObjectClassName']), "ShortDescription"); $fields->addFieldToTab("Root.Main", ReadonlyField::create("ClassNameNice", $fieldLabels['ClassNameNice']), "ShortDescription"); $fields->dataFieldByName('LongDescription')->setRows(3); return $fields; } /** * * @return string */ public function getTitle() : string { return $this->getClassNameNice()." (".$this->ObjectClassName.")"; } /** * caching variable for objects` * @var array */ private static $_cache_for_class_objects = []; /** * * @param string|null $className * * @return FrontEndEditble (DataObject) */ protected function getClassNameObjectFromCache($className = null) { if ($className === null) { $className = $this->ObjectClassName; } if (! isset(self::$_cache_for_class_objects[$className])) { if (! class_exists($className)) { $className = 'DataObject'; $this->ObjectClassName = "DataObject"; } self::$_cache_for_class_objects[$className] = Injector::inst()->get($className); } return self::$_cache_for_class_objects[$className]; } /** * * @param [type] $className [description] * @return [type] [description] */ public function getClassNameNice($className = null) : string { if ($className === null) { $className = $this->ObjectClassName; } if ($obj = $this->getClassNameObjectFromCache($className)) { return $obj->i18n_singular_name(); } return "ERROR IN FINDING NAME FOR CLASS: ".$className; } /** * @casted variable * * @return bool */ public function HasLongDescription() : bool { return $this->getHasLongDescription(); } /** * @casted variable * @return bool */ public function getHasLongDescription() : bool { return strlen($this->LongDescription) > 10 ? true : false; } /** * @casted variable * @return string */ public function HasLongDescriptionNice() : string { return $this->getHasLongDescriptionNice(); } public function getHasLongDescriptionNice() : string { return $this->HasLongDescription() ? "yes" : "no"; } /** * * @return string|null */ public function BestDescription() : string { if ($this->getHasLongDescription()) { return $this->LongDescription; } else { return $this->ShortDescription; } return ''; } /** * * @return bool */ public function HasDescription() : bool { if ($this->getHasLongDescription()) { return true; } else { if ($this->ShortDescription) { return true; } } return false; } public function canCreate($member = null) { return false; } public function canDelete($member = null) { if (! $this->getClassNameObjectFromCache()) { return parent::canDelete($member); } return false; } protected static function get_my_static_class() { return 'ddd'; } } |