1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18:
19: class ClipitTag extends UBItem {
20: 21: 22:
23: const SUBTYPE = "ClipitTag";
24:
25: public $ontologylink = "";
26:
27: 28: 29: 30: 31: 32: 33:
34: static function create($prop_value_array) {
35: if(!isset($prop_value_array["name"])){
36: return null;
37: }
38: $id_array = static::get_from_search($prop_value_array["name"], true, true);
39: if(!empty($id_array)){
40: return array_pop($id_array)->id;
41: }
42: return static::set_properties(null, $prop_value_array);
43: }
44:
45: static function get_tricky_topics($id) {
46: return UBCollection::get_items($id, ClipitTrickyTopic::REL_TRICKYTOPIC_TAG, true);
47: }
48:
49: protected function copy_from_elgg($elgg_entity)
50: {
51: parent::copy_from_elgg($elgg_entity);
52: $this->ontologylink = (string)$elgg_entity->get("ontologylink");
53: }
54:
55: protected function copy_to_elgg($elgg_entity)
56: {
57: parent::copy_to_elgg($elgg_entity);
58: if (!empty($this->ontologylink)) {
59: $elgg_entity->set("ontologylink", (string)$this->ontologylink);
60: } else {
61: $elgg_entity->set("ontologylink", "<http://www.juxtalearn.org/Stumbling_Block/".str_replace(" ","_",$this->name).">");
62: }
63: }
64: public function save(){
65: return parent::save();
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78:
79: static function get_from_search($search_string, $name_only = true, $strict = false, $limit = 0, $offset = 0) {
80: $search_result = array();
81: if(!$strict) {
82: $search_string = strtolower($search_string);
83:
84: $elgg_object_array = elgg_get_entities_from_metadata(
85: array('type' => static::TYPE, 'subtype' => static::SUBTYPE, 'limit' => 0, 'metadata_name_value_pairs' => array( name => 'name', value => $search_string.'%', 'operand' => 'LIKE', 'case_sensitive' => FALSE ))
86: );
87: $search_result = array();
88: foreach($elgg_object_array as $elgg_object) {
89:
90: if(strpos(strtolower($elgg_object->name), $search_string) !== false) {
91: $search_result[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
92: continue;
93: }
94:
95: if($name_only === false) {
96: if(strpos(strtolower($elgg_object->description), $search_string) !== false) {
97: $search_result[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
98: }
99: }
100: }
101: } else {
102:
103: $elgg_object_array = elgg_get_entities_from_metadata(
104: array(
105: 'type' => static::TYPE, 'subtype' => static::SUBTYPE, 'metadata_names' => array("name"),
106: 'metadata_values' => array($search_string), 'limit' => 0
107: )
108: );
109: if(!empty($elgg_object_array)) {
110: foreach($elgg_object_array as $elgg_object) {
111: $search_result[(int)$elgg_object->guid] = new static((int)$elgg_object->guid, $elgg_object);
112: }
113: }
114: }
115: usort($search_result, function($a,$b){
116: $length1 = strlen($a->name);
117: $length2 = strlen($b->name);
118: return ($length1 < $length2) ? -1 : (($length1 > $length2) ? 1 : 0);
119: });
120: if(empty($limit)){
121: return array_slice($search_result, (int)$offset, count($search_result), true);
122: } else {
123: return array_slice($search_result, (int)$offset, (int)$limit, true);
124: }
125:
126: }
127:
128: }