1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class ClipitFile extends UBFile {
19: 20: 21:
22: const SUBTYPE = "ClipitFile";
23: const REL_FILE_TAG = "ClipitFile-ClipitTag";
24: const REL_FILE_LABEL = "ClipitFile-ClipitLabel";
25: const REL_FILE_USER = "ClipitFile-ClipitUser";
26: const SCOPE_GROUP = "group";
27: const SCOPE_ACTIVITY = "activity";
28: const SCOPE_SITE = "site";
29:
30: const SCOPE_EXAMPLE = "example";
31: const SCOPE_TRICKYTOPIC = "tricky_topic";
32: const SCOPE_TASK = "task";
33: public $tag_array = array();
34: public $label_array = array();
35: public $read_array = array();
36:
37: 38: 39: 40: 41:
42: protected function copy_from_elgg($elgg_file)
43: {
44: parent::copy_from_elgg($elgg_file);
45: $this->tag_array = (array)static::get_tags($this->id);
46: $this->label_array = (array)static::get_labels($this->id);
47: $this->read_array = (array)static::get_read_array($this->id);
48: }
49:
50: 51: 52: 53: 54:
55: protected function copy_to_elgg($elgg_file)
56: {
57: parent::copy_to_elgg($elgg_file);
58:
59: }
60:
61: 62: 63: 64: 65: 66: 67:
68: protected function save($double_save = false)
69: {
70: parent::save($double_save);
71: static::set_tags($this->id, $this->tag_array);
72: static::set_labels($this->id, $this->label_array);
73: static::set_read_array($this->id, $this->read_array);
74: return $this->id;
75: }
76:
77:
78: static function get_by_tags($tag_array)
79: {
80: $return_array = array();
81: $all_items = static::get_all(0, 0, "", true, true);
82: foreach ($all_items as $item_id) {
83: $item_tags = static::get_tags((int)$item_id);
84: foreach ($tag_array as $search_tag) {
85: if (array_search($search_tag, $item_tags) !== false) {
86: $return_array[(int)$item_id] = new static((int)$item_id);
87: break;
88: }
89: }
90: }
91: return $return_array;
92: }
93:
94: static function get_by_labels($label_array)
95: {
96: $return_array = array();
97: $all_items = static::get_all(0, 0, "", true, true);
98: foreach ($all_items as $item_id) {
99: $item_labels = (array)static::get_labels((int)$item_id);
100: foreach ($label_array as $search_tag) {
101: if (array_search($search_tag, $item_labels) !== false) {
102: $return_array[(int)$item_id] = new static((int)$item_id);
103: break;
104: }
105: }
106: }
107: return $return_array;
108: }
109:
110: static function get_scope($id)
111: {
112: $site = static::get_site($id, false);
113: if (!empty($site)) {
114: return static::SCOPE_SITE;
115: }
116: $task = static::get_task($id);
117: if (!empty($task)) {
118: return static::SCOPE_TASK;
119: }
120: $group = static::get_group($id);
121: if (!empty($group)) {
122: return static::SCOPE_GROUP;
123: }
124: $activity = static::get_activity($id);
125: if (!empty($activity)) {
126: return static::SCOPE_ACTIVITY;
127: }
128: $example = static::get_example($id);
129: if (!empty($example)) {
130: return static::SCOPE_EXAMPLE;
131: }
132: $tricky_topic = static::get_tricky_topic($id);
133: if (!empty($tricky_topic)) {
134: return static::SCOPE_TRICKYTOPIC;
135: }
136: return null;
137: }
138:
139: static function get_site($id, $recursive = false)
140: {
141: $site_array = UBCollection::get_items($id, ClipitSite::REL_SITE_VIDEO, true);
142: if (!empty($site_array)) {
143: return array_pop($site_array);
144: }
145: if ($recursive) {
146: $clone_array = static::get_clones($id, true);
147: foreach ($clone_array as $clone_id) {
148: $site_array = UBCollection::get_items($clone_id, ClipitSite::REL_SITE_VIDEO, true);
149: if (!empty($site_array)) {
150: return array_pop($site_array);
151: }
152: }
153: }
154: return null;
155: }
156:
157: static function get_task($id)
158: {
159: $task = UBCollection::get_items($id, ClipitTask::REL_TASK_FILE, true);
160: if (empty($task)) {
161: return null;
162: }
163: return array_pop($task);
164: }
165:
166: 167: 168: 169: 170: 171: 172:
173: static function get_group($id) {
174: $file = new static($id);
175: if(!empty($file->cloned_from)) {
176: return static::get_group($file->cloned_from);
177: }
178: $group = UBCollection::get_items($id, ClipitGroup::REL_GROUP_FILE, true);
179: if (empty($group)) {
180: return null;
181: }
182: return (int)array_pop($group);
183: }
184:
185: static function get_activity($id)
186: {
187: $group_id = static::get_group($id);
188: if (!empty($group_id)) {
189: return ClipitGroup::get_activity($group_id);
190: } else {
191: $activity = UBCollection::get_items($id, ClipitActivity::REL_ACTIVITY_FILE, true);
192: if (empty($activity)) {
193: return null;
194: }
195: return array_pop($activity);
196: }
197: }
198:
199: static function get_example($id)
200: {
201: $example = UBCollection::get_items($id, ClipitExample::REL_EXAMPLE_FILE, true);
202: if (empty($example)) {
203: return null;
204: }
205: return array_pop($example);
206: }
207:
208: static function get_tricky_topic($id)
209: {
210: $activity_array = UBCollection::get_items($id, ClipitTrickyTopic::REL_TRICKYTOPIC_FILE, true);
211: if (empty($activity_array)) {
212: return null;
213: }
214: return array_pop($activity_array);
215: }
216:
217: 218: 219: 220: 221: 222: 223: 224:
225: static function add_tags($id, $tag_array)
226: {
227: return UBCollection::add_items($id, $tag_array, static::REL_FILE_TAG);
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237:
238: static function remove_tags($id, $tag_array)
239: {
240: return UBCollection::remove_items($id, $tag_array, static::REL_FILE_TAG);
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250:
251: static function add_labels($id, $label_array)
252: {
253: return UBCollection::add_items($id, $label_array, static::REL_FILE_LABEL);
254: }
255:
256: 257: 258: 259: 260: 261: 262: 263:
264: static function remove_labels($id, $label_array)
265: {
266: return UBCollection::remove_items($id, $label_array, static::REL_FILE_LABEL);
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276:
277: static function add_read_array($id, $read_array) {
278: return UBCollection::add_items($id, $read_array, static::REL_FILE_USER);
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288:
289: static function set_read_array($id, $read_array) {
290: return UBCollection::set_items($id, $read_array, static::REL_FILE_USER);
291: }
292:
293: 294: 295: 296: 297: 298: 299: 300:
301: static function remove_read_array($id, $read_array) {
302: return UBCollection::remove_items($id, $read_array, static::REL_FILE_USER);
303: }
304:
305: 306: 307: 308: 309: 310: 311:
312: static function get_read_array($id) {
313: return UBCollection::get_items($id, static::REL_FILE_USER);
314: }
315:
316:
317: 318: 319: 320: 321: 322: 323: 324:
325: static function get_read_status($id, $user_array = null) {
326: $props = static::get_properties($id, array("read_array", "owner_id"));
327: $read_array = $props["read_array"];
328: $owner_id = $props["owner_id"];
329: if (!$user_array) {
330: return $read_array;
331: } else {
332: $return_array = array();
333: foreach ($user_array as $user_id) {
334: if ((int)$user_id == (int)$owner_id || in_array($user_id, $read_array)) {
335: $return_array[$user_id] = true;
336: } else {
337: $return_array[$user_id] = false;
338: }
339: }
340: return $return_array;
341: }
342: }
343:
344: 345: 346: 347: 348: 349: 350: 351: 352: 353:
354: static function set_read_status($id, $read_value, $user_array) {
355: $read_array = static::get_read_array($id);
356: $update_flag = false;
357: foreach ($user_array as $user_id) {
358: $index = array_search((int)$user_id, $read_array);
359: if ($read_value === true && $index === false) {
360: array_push($read_array, $user_id);
361: $update_flag = true;
362: } elseif ($read_value === false && $index !== false) {
363: array_splice($read_array, $index, 1);
364: $update_flag = true;
365: }
366: }
367: if ($update_flag) {
368: return static::set_read_array($id, $read_array);
369: } else {
370: return $id;
371: }
372: }
373:
374:
375:
376: 377: 378: 379: 380: 381: 382:
383: static function get_tags($id)
384: {
385: return UBCollection::get_items($id, static::REL_FILE_TAG);
386: }
387:
388: 389: 390: 391: 392: 393: 394:
395: static function get_labels($id)
396: {
397: return UBCollection::get_items($id, static::REL_FILE_LABEL);
398: }
399:
400: 401: 402: 403: 404: 405: 406: 407:
408: static function set_tags($id, $tag_array)
409: {
410: return UBCollection::set_items($id, $tag_array, static::REL_FILE_TAG);
411: }
412:
413: 414: 415: 416: 417: 418: 419: 420:
421: static function set_labels($id, $label_array)
422: {
423: return UBCollection::set_items($id, $label_array, static::REL_FILE_LABEL);
424: }
425: }
426: