diff --git a/code/TagField.php b/code/TagField.php index f788e02..c4aa32e 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -42,10 +42,10 @@ class TagField extends TextField { protected $tagTopicClass; /** - * @var string $tagFieldName Only applies to object-based tagging. + * @var string $tagObjectField Only applies to object-based tagging. * The fieldname for textbased tagging is inferred from the formfield name. */ - protected $tagFieldName = 'Title'; + protected $tagObjectField = 'Title'; /** * @var string $tagFilter @@ -65,7 +65,6 @@ class TagField extends TextField { protected static $separator_to_regex = array( ' ' => '\s', - ',' => '\,', ); /** @@ -80,9 +79,9 @@ class TagField extends TextField { */ public $maxSuggestionsNum = 50; - function __construct($name, $title = null, $value = null, $tagTopicClass = null, $tagFieldName = "Title") { + function __construct($name, $title = null, $value = null, $tagTopicClass = null, $tagObjectField = "Title") { $this->tagTopicClass = $tagTopicClass; - $this->tagFieldName = ($tagFieldName) ? $tagFieldName : $name; + $this->tagObjectField = $tagObjectField; parent::__construct($name, $title, $value); } @@ -126,12 +125,12 @@ class TagField extends TextField { if($this->customTags) { $tags = $this->customTags; - } else if($tagTopicClassObj->many_many($this->tagFieldName)) { + } else if($tagTopicClassObj->many_many($this->Name())) { $tags = $this->getObjectTags($searchString); - } else if($tagTopicClassObj->hasField($this->tagFieldName)) { + } else if($tagTopicClassObj->hasField($this->Name())) { $tags = $this->getTextbasedTags($searchString); } else { - user_error('TagField::suggest(): Cant find valid relation or text property with name "' . $this->tagFieldName . '"', E_USER_ERROR); + user_error('TagField::suggest(): Cant find valid relation or text property with name "' . $this->Name() . '"', E_USER_ERROR); } return Convert::raw2json($tags); @@ -140,9 +139,9 @@ class TagField extends TextField { function saveInto($record) { if($this->value) { // $record should match the $tagTopicClass - if($record->many_many($this->tagFieldName)) { + if($record->many_many($this->Name())) { $this->saveIntoObjectTags($record); - } elseif($record->hasField($this->tagFieldName)) { + } elseif($record->hasField($this->Name())) { $this->saveIntoTextbasedTags($record); } else { user_error('TagField::saveInto(): Cant find valid field or relation to save into', E_USER_ERROR); @@ -151,10 +150,10 @@ class TagField extends TextField { } function setValue($value, $obj = null) { - if(isset($obj) && is_object($obj) && $obj instanceof DataObject && $obj->many_many($this->tagFieldName)) { + if(isset($obj) && is_object($obj) && $obj instanceof DataObject && $obj->many_many($this->Name())) { //if(!$obj->many_many($this->Name())) user_error("TagField::setValue(): Cant find relationship named '$this->Name()' on object", E_USER_ERROR); - $tags = $obj->{$this->tagFieldName}(); - $this->value = implode($this->separator, array_values($tags->map('ID',$this->tagFieldName))); + $tags = $obj->{$this->Name()}(); + $this->value = implode($this->separator, array_values($tags->map('ID',$this->tagObjectField))); } else { parent::setValue($value, $obj); } @@ -187,7 +186,7 @@ class TagField extends TextField { if(!$record->isInDB()) $record->write(); $tagsArr = $this->splitTagsToArray($this->value); - $relationName = $this->$this->tagFieldName; + $relationName = $this->Name(); $existingTagsComponentSet = $record->$relationName(); $tagClass = $this->getTagClass(); $tagBaseClass = ClassInfo::baseDataClass($tagClass); @@ -196,13 +195,13 @@ class TagField extends TextField { if($tagsArr) foreach($tagsArr as $tagString) { $SQL_filter = sprintf('`%s`.`%s` = "%s"', $tagBaseClass, - $this->tagFieldName, + $this->tagObjectField, Convert::raw2sql($tagString) ); $tagObj = DataObject::get_one($tagClass, $SQL_filter); if(!$tagObj) { $tagObj = new $tagClass(); - $tagObj->{$this->tagFieldName} = $tagString; + $tagObj->{$this->tagObjectField} = $tagString; $tagObj->write(); } $tagsToAdd[] = $tagObj; @@ -214,7 +213,7 @@ class TagField extends TextField { } protected function saveIntoTextbasedTags($record) { - $tagFieldName = $this->tagFieldName; + $tagFieldName = $this->Name(); // necessary step to filter whitespace etc. $RAW_tagsArr = $this->splitTagsToArray($this->value); @@ -234,9 +233,9 @@ class TagField extends TextField { * Use only when storing tags in objects */ protected function getTagClass() { - $tagManyMany = singleton($this->getTagTopicClass())->many_many($this->tagFieldName); + $tagManyMany = singleton($this->getTagTopicClass())->many_many($this->Name()); if(!$tagManyMany) { - user_error('TagField::getTagClass(): Cant find relation with name "' . $this->tagFieldName . '"', E_USER_ERROR); + user_error('TagField::getTagClass(): Cant find relation with name "' . $this->Name() . '"', E_USER_ERROR); } return $tagManyMany[1]; @@ -248,29 +247,29 @@ class TagField extends TextField { $SQL_filter = sprintf("`%s`.`%s` LIKE '%%%s%%'", $tagBaseClass, - $this->tagFieldName, + $this->tagObjectField, Convert::raw2sql($searchString) ); if($this->tagFilter) $SQL_filter .= ' AND ' . $this->tagFilter; $tagObjs = DataObject::get($tagClass, $SQL_filter, $this->tagSort, "", $this->maxSuggestionsNum); - $tagArr = ($tagObjs) ? array_values($tagObjs->map('ID', $this->tagFieldName)) : array(); + $tagArr = ($tagObjs) ? array_values($tagObjs->map('ID', $this->tagObjectField)) : array(); return $tagArr; } - /*protected function getTextbasedTags($searchString) { + protected function getTextbasedTags($searchString) { $baseClass = ClassInfo::baseDataClass($this->getTagTopicClass()); $SQL_filter = sprintf("`%s`.`%s` LIKE '%%%s%%'", $baseClass, - $this->tagFieldName, + $this->Name(), Convert::raw2sql($searchString) ); if($this->tagFilter) $SQL_filter .= ' AND ' . $this->tagFilter; $allTopicObjs = DataObject::get($this->getTagTopicClass(), $SQL_filter, $this->tagSort); - $multipleTagsArr = ($allTopicObjs) ? array_values($allTopicObjs->map('ID', $this->tagFieldName)) : array(); + $multipleTagsArr = ($allTopicObjs) ? array_values($allTopicObjs->map('ID', $this->Name())) : array(); $filteredTagArr = array(); foreach($multipleTagsArr as $multipleTags) { @@ -287,22 +286,6 @@ class TagField extends TextField { $filteredTagArr = array_unique($filteredTagArr); return $filteredTagArr; - }*/ - - protected function getTextbasedTags($searchString) { - $baseClass = ClassInfo::baseDataClass($this->getTagTopicClass()); - - $SQL_filter = sprintf("`%s`.`%s` LIKE '%%%s%%'", - $baseClass, - $this->tagFieldName, - Convert::raw2sql($searchString) - ); - if($this->tagFilter) $SQL_filter .= ' AND ' . $this->tagFilter; - - $sql = "Select `".$this->tagFieldName."` From `".$this->getTagTopicClass(). "` Where ".$SQL_filter; - $map = DB::query($sql)->column(); - - return $map; } public function setTagFilter($sql) { @@ -338,4 +321,4 @@ class TagField extends TextField { } } -?> \ No newline at end of file +?>