1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13: 
 14:  15:  16: 
 17: class ClipitRemoteTrickyTopic extends UBItem {
 18:     const SUBTYPE = "ClipitRemoteTrickyTopic";
 19:     const REL_REMOTETRICKYTOPIC_TAG = "ClipitRemoteTrickyTopic-ClipitTag";
 20:     public $remote_id;
 21:     public $remote_site = 0;
 22:     public $tag_array = "";
 23:      24:  25:  26:  27: 
 28:     protected function copy_from_elgg($elgg_entity) {
 29:         parent::copy_from_elgg($elgg_entity);
 30:         $this->remote_id = (int)$elgg_entity->get("remote_id");
 31:         $this->remote_site = (int)$elgg_entity->get("remote_site");
 32:         $this->tag_array = (array)static::get_tags($this->id);
 33:     }
 34:      35:  36:  37:  38: 
 39:     protected function copy_to_elgg($elgg_entity) {
 40:         parent::copy_to_elgg($elgg_entity);
 41:         $elgg_entity->set("remote_id", (int)$this->remote_id);
 42:         $elgg_entity->set("remote_site", (int)$this->remote_site);
 43:         static::set_tags((int)$this->id, (array)$this->tag_array);
 44:     }
 45:     static function create($prop_value_array){
 46:         
 47:         $remote_site_url = base64_decode($prop_value_array["remote_site"]);
 48:         $remote_site_id = ClipitRemoteSite::get_from_url($remote_site_url, true);
 49:         $prop_value_array["remote_site"] = $remote_site_id;
 50:         
 51:         $prop_value_array["name"] = base64_decode($prop_value_array["name"]);
 52:         $prop_value_array["description"] = base64_decode($prop_value_array["description"]);
 53:         
 54:         $tag_name_array = json_decode(base64_decode($prop_value_array["tag_array"]));
 55:         $tag_array = array();
 56:         foreach($tag_name_array as $tag_name){
 57:             $tag_array[] = (int)ClipitTag::create(array("name" => $tag_name));
 58:         }
 59:         $prop_value_array["tag_array"] = (array)$tag_array;
 60:         $id = parent::create($prop_value_array);
 61:         ClipitRemoteSite::add_tricky_topics($remote_site_url, array($id));
 62:         return $id;
 63:     }
 64: 
 65:      66:  67:  68:  69:  70:  71:  72: 
 73:     static function add_tags($id, $tag_array) {
 74:         return UBCollection::add_items($id, $tag_array, static::REL_REMOTETRICKYTOPIC_TAG);
 75:     }
 76:      77:  78:  79:  80:  81:  82:  83: 
 84:     static function set_tags($id, $tag_array) {
 85:         return UBCollection::set_items($id, $tag_array, static::REL_REMOTETRICKYTOPIC_TAG);
 86:     }
 87:      88:  89:  90:  91:  92:  93:  94: 
 95:     static function remove_tags($id, $tag_array) {
 96:         return UBCollection::remove_items($id, $tag_array, static::REL_REMOTETRICKYTOPIC_TAG);
 97:     }
 98:      99: 100: 101: 102: 103: 104: 
105:     static function get_tags($id) {
106:         return UBCollection::get_items($id, static::REL_REMOTETRICKYTOPIC_TAG);
107:     }
108: 
109:     
110:     111: 112: 113: 114: 
115:     static function get_by_remote_id($remote_site, $remote_id_array){
116:         $remote_site_id = ClipitRemoteSite::get_from_url(base64_decode($remote_site), true);
117:         $tricky_topic_array = static::get_all();
118:         $return_array = array();
119:         foreach($tricky_topic_array as $tricky_topic){
120:             if($tricky_topic->remote_site == $remote_site_id
121:                 && array_search($tricky_topic->remote_id,  $remote_id_array) !== false){
122:                 $return_array[] = $tricky_topic;
123:             }
124:         }
125:         return $return_array;
126:     }
127:     128: 129: 130: 131: 
132:     static function delete_by_remote_id($remote_site, $remote_id_array){
133:         $remote_site_id = ClipitRemoteSite::get_from_url(base64_decode($remote_site), true);
134:         $tricky_topic_array = static::get_by_remote_id($remote_site_id, $remote_id_array);
135:         $delete_array = array();
136:         foreach($tricky_topic_array as $tricky_topic){
137:             $delete_array[] = $tricky_topic->id;
138:         }
139:         static::delete_by_id($delete_array);
140:         return true;
141:     }
142:     143: 144: 145: 146: 
147:     static function get_from_site($remote_site, $remote_ids_only = false){
148:         $remote_site_id = ClipitRemoteSite::get_from_url(base64_decode($remote_site), true);
149:         $tricky_topic_array = static::get_all();
150:         $return_array = array();
151:         foreach($tricky_topic_array as $tricky_topic){
152:             if((int)$tricky_topic->remote_site == $remote_site_id) {
153:                 if($remote_ids_only) {
154:                     $return_array[] = $tricky_topic->remote_id;
155:                 } else{
156:                     $return_array[] = $tricky_topic;
157:                 }
158:             }
159:         }
160:         return $return_array;
161:     }
162:     163: 164: 165: 
166:     static function delete_all_from_site($remote_site){
167:         $remote_site_id = ClipitRemoteSite::get_from_url(base64_decode($remote_site), true);
168:         $tricky_topic_array = static::get_from_site($remote_site_id);
169:         $delete_array = array();
170:         foreach($tricky_topic_array as $tricky_topic){
171:             if((int)$tricky_topic->remote_site == $remote_site_id){
172:                 $delete_array[] = $tricky_topic->id;
173:             }
174:         }
175:         return static::delete_by_id($delete_array);
176:     }
177: 
178: }