1: <?php
2: /**
3: * ClipIt - JuxtaLearn Web Space
4: * PHP version: >= 5.2
5: * Creation date: 2013-10-10
6: * Last update: $Date$
7: * @author Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
8: * @version $Version$
9: * @link http://www.juxtalearn.eu
10: * @license GNU Affero General Public License v3
11: * @package ClipIt
12: * @subpackage urjc_backend
13: */
14:
15: /**
16: * <Class Description>
17: */
18: abstract class UBCollection {
19: /**
20: * Adds Items to a Collection.
21: *
22: * @param int $id Id from Collection to add Items to.
23: * @param array $item_array Array of Item Ids to add.
24: * @param string $rel_name Name of the relationship to use.
25: * @param bool $exclusive Whether the added items have an exclusive relationship with the collection.
26: *
27: * @return bool Returns true if success, false if error
28: */
29: static function add_items($id, $item_array, $rel_name, $exclusive = false) {
30: foreach ($item_array as $item_id) {
31: if ($exclusive) {
32: $rel_array = get_entity_relationships($item_id, true);
33: foreach ($rel_array as $rel) {
34: if ($rel->relationship == $rel_name) {
35: delete_relationship($rel->id);
36: }
37: }
38: }
39: add_entity_relationship($id, $rel_name, $item_id);
40: }
41: return true;
42: }
43:
44: /**
45: * Sets Items to a Collection.
46: *
47: * @param int $id Id from Collection to add Items to.
48: * @param array $item_array Array of Item Ids to set.
49: * @param string $rel_name Name of the relationship to use.
50: * @param bool $exclusive Whether the added items have an exclusive relationship with the collection.
51: *
52: * @return bool Returns true if success, false if error
53: */
54: static function set_items($id, $item_array, $rel_name, $exclusive = false) {
55: static::remove_all_items($id, $rel_name);
56: if(empty($item_array)){
57: return true;
58: }
59: foreach ($item_array as $item_id) {
60: if ($exclusive) {
61: $rel_array = get_entity_relationships($item_id, true);
62: foreach ($rel_array as $rel) {
63: if ($rel->relationship == $rel_name) {
64: delete_relationship($rel->id);
65: }
66: }
67: }
68: add_entity_relationship($id, $rel_name, $item_id);
69: }
70: return true;
71: }
72:
73: /**
74: * Get Items from a Collection.
75: *
76: * @param int $id Id from Collection to get Items from.
77: * @param string $rel_name Name of the relationship linking the items.
78: * @param bool $inverse Whether the Id specified is in the first (false) or second (true) term of the relationship.
79: *
80: * @return int[]|bool Returns an array of Item IDs, or false if error.
81: */
82: static function get_items($id, $rel_name, $inverse = false) {
83: $rel_array = get_entity_relationships($id, $inverse);
84: $item_array = array();
85: foreach ($rel_array as $rel) {
86: if ($rel->relationship == $rel_name) {
87: if ($inverse) {
88: $item_array[$rel->id] = (int)$rel->guid_one;
89: } else {
90: $item_array[$rel->id] = (int)$rel->guid_two;
91: }
92: }
93: }
94: // IDs are created sequentially, so inverse ordering == reverse chrono-order
95: uasort($item_array, 'UBItem::sort_numbers_inv');
96: return $item_array;
97: }
98:
99: /**
100: * Count the number of related items.
101: *
102: * @param int $id Item to count related items with.
103: * @param string $rel_name Name of the relationship
104: * @param bool $inverse position of the Item in the relationship (first = false, seccond = true)
105: * @param bool $recursive Whether to count recursively or not
106: *
107: * @return int Number of items related with the one specified.
108: */
109: static function count_items($id, $rel_name, $inverse = false, $recursive = false) {
110: $rel_array = get_entity_relationships($id, $inverse);
111: $count = 0;
112: foreach ($rel_array as $rel) {
113: if ($rel->relationship === $rel_name) {
114: $count++;
115: if ($recursive) {
116: if ($inverse) {
117: $count += static::count_items($rel->guid_one, $rel_name, $inverse, $recursive);
118: } else {
119: $count += static::count_items($rel->guid_two, $rel_name, $inverse, $recursive);
120: }
121: }
122: }
123: }
124: return $count;
125: }
126:
127: /**
128: * Remove Items from a Collection.
129: *
130: * @param int $id Id from Collection to remove Items from.
131: * @param array $item_array Array of Item Ids to remove.
132: * @param string $rel_name Name of the relationship to use.
133: *
134: * @return bool Returns true if success, false if error.
135: */
136: static function remove_items($id, $item_array, $rel_name) {
137: foreach ($item_array as $item_id) {
138: remove_entity_relationship($id, $rel_name, $item_id);
139: }
140: return true;
141: }
142:
143: /**
144: * Remove all items from a collection.
145: *
146: * @param int $id ID of the Collection to remove items from.
147: * @param string $rel_name Name of the relationship to use.
148: *
149: * @return bool Returns true if success, false if error.
150: */
151: static function remove_all_items($id, $rel_name) {
152: return remove_entity_relationships($id, $rel_name);
153: }
154:
155: static function set_timestamp($id1, $id2, $rel_name, $timestamp){
156: $rel_array = get_entity_relationships($id1);
157: if(empty($rel_array)){
158: return null;
159: }
160: foreach($rel_array as $rel){
161: if($rel->relationship == $rel_name && (int)$rel->guid_two == (int)$id2){
162: $rel->time_created = $timestamp;
163: return $rel->save();
164: }
165: }
166: return null;
167: }
168:
169: static function get_timestamp($id1, $id2, $rel_name) {
170: $rel_array = get_entity_relationships($id1);
171: if (empty($rel_array)) {
172: return null;
173: }
174: foreach ($rel_array as $rel) {
175: if ($rel->relationship == $rel_name && (int)$rel->guid_two == (int)$id2) {
176: return $rel->getTimeCreated();
177: }
178: }
179: return null;
180: }
181: }