OssnAnnotation.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
12  /**
13  * Initialize the objects.
14  *
15  * @return void
16  */
17  public function initAttributes() {
18  $this->OssnDatabase = new OssnDatabase;
19  $this->time_created = time();
20  if(empty($this->subtype)) {
21  $this->subtype = NULL;
22  }
23  if(empty($this->permission)) {
24  $this->permission = OSSN_PUBLIC;
25  }
26  if(empty($this->order_by)) {
27  $this->order_by = '';
28  }
29  }
30  /**
31  * Create annotation;
32  *
33  * Requires : $object->(owner_guid, subject_guid, type, subtype, value)
34  *
35  * @return boolean
36  */
37  public function addAnnotation() {
38  self::initAttributes();
39  if(empty($this->owner_guid) || empty($this->subject_guid)) {
40  return false;
41  }
42  $params['into'] = 'ossn_annotations';
43  $params['names'] = array(
44  'owner_guid',
45  'subject_guid',
46  'type',
47  'time_created'
48  );
49  $params['values'] = array(
50  $this->owner_guid,
51  $this->subject_guid,
52  $this->type,
53  $this->time_created
54  );
55  $this->annotation_type = $this->type;
56  $this->owner_guid_old = $this->owner_guid;
57  if($this->OssnDatabase->insert($params)) {
58  $this->annotation_inserted = $this->OssnDatabase->getLastEntry();
59  $this->atype = $this->type;
60  $this->type = 'annotation';
61  $this->subtype = $this->atype;
62  $this->owner_guid = $this->annotation_inserted;
63  $this->value = $this->value;
64  $this->add();
65 
66  $params['subject_guid'] = $this->subject_guid;
67  $params['owner_guid'] = $this->owner_guid_old;
68  $params['type'] = $this->annotation_type;
69  $params['annotation_guid'] = $this->OssnDatabase->getLastEntry();
70  ossn_trigger_callback('annotations', 'created', $params);
71 
72  return true;
73  }
74  return false;
75  }
76  /**
77  * Get annotation by annotation id;
78  *
79  * Requires : $object->(annotation_id)
80  *
81  * @return object
82  */
83  public function getAnnotationById() {
84  $params = array();
85  $params['annotation_id'] = $this->annotation_id;
86 
87  $data = $this->searchAnnotation($params);
88  if($data) {
89  return $data[0];
90  }
91  }
92  /**
93  * Get annotations by annotation type
94  *
95  * Requires: $object->(annotation_id)
96  *
97  * @return object;
98  */
99  public function getAnnotationsByType() {
100  if(!isset($this->limit)) {
101  $this->limit = false;
102  }
103  if(!isset($this->page_limit)){
104  $this->page_limit = false;
105  }
106  $params = array();
107  $params['type'] = $this->type;
108  $params['subtype'] = $this->subtype;
109  $params['limit'] = $this->limit;
110  $params['order_by'] = $this->order_by;
111 
112  return $this->searchAnnotation($params);
113  }
114  /**
115  * Get annotations by owner
116  *
117  * Requires : $this->owner_guid
118  *
119  * @return object;
120  */
121  public function getAnnotationsByOwner() {
122  if(!isset($this->limit)) {
123  $this->limit = false;
124  }
125  if(!isset($this->page_limit)){
126  $this->page_limit = false;
127  }
128  $params = array();
129  $params['owner_guid'] = $this->subject_guid;
130  $params['limit'] = $this->limit;
131  $params['order_by'] = $this->order_by;
132  $params['page_limit'] = $this->page_limit;
133  $params['type'] = $this->type;
134  return $this->searchAnnotation($params);
135  }
136  /**
137  * Delete annotations by subject guid
138  *
139  * @params $subject = subject_guid,
140  * $type = annotation type
141  * @param string $type
142  *
143  * @return object;
144  */
145  public function annon_delete_by_subject($subject, $type) {
146  self::initAttributes();
147  if(empty($subject)) {
148  return false;
149  }
150  $this->subject_guid = $subject;
151  $this->type = $type;
152  $this->limit = false;
153  $this->page_limit = false;
154  $annotations = $this->getAnnotationBySubject();
155  if($annotations) {
156  foreach($annotations as $annontation) {
157  $this->deleteAnnotation($annontation->id);
158  }
159  return true;
160  }
161  return false;
162  }
163 
164  /**
165  * Get annotation by subject_guid;
166  *
167  * Requires : $object->(subject_guid, types(optional))
168  *
169  * @return object;
170  */
171  public function getAnnotationBySubject() {
172  if(!isset($this->count)) {
173  $this->count = false;
174  }
175  if(!isset($this->limit)) {
176  $this->limit = false;
177  }
178  if(!isset($this->page_limit)){
179  $this->page_limit = false;
180  }
181  $params = array();
182  $params['type'] = $this->type;
183  $params['subject_guid'] = $this->subject_guid;
184  $params['count'] = $this->count;
185  $params['order_by'] = $this->order_by;
186  $params['limit'] = $this->limit;
187  $params['page_limit'] = $this->page_limit;
188 
189  return $this->searchAnnotation($params);
190  }
191 
192  /**
193  * Delete Annotation
194  *
195  * @param integer $annotation annotation_id
196  *
197  * @return boolean;
198  */
199  public function deleteAnnotation($annotation) {
200  self::initAttributes();
201  if($this->deleteByOwnerGuid($annotation, 'annotation')) {
202  $this->statement("DELETE FROM ossn_annotations WHERE(id='{$annotation}')");
203  if($this->execute()) {
204  $data = ossn_get_userdata("annotation/{$annotation}/");
205  if(is_dir($data)) {
206  OssnFile::DeleteDir($data);
207  // As of v2.0 DeleteDir delete directory also
208  //rmdir($data);
209  }
210  $params['annotation'] = $annotation;
211  ossn_trigger_callback('annotation', 'delete', $params);
212  return true;
213  }
214  }
215  return false;
216  }
217  /**
218  * Delete Annotation
219  *
220  * @param integer $annotation annotation_id
221  *
222  * @return boolean
223  */
224  public function deleteAnnotationByOwner($ownerguid) {
225  self::initAttributes();
226  if(empty($ownerguid)) {
227  return false;
228  }
229  $this->owner_guid = $ownerguid;
230  $annotations = $this->getAnnotationsByOwner();
231  if($annotations) {
232  foreach($annotations as $annotation) {
233  $this->deleteAnnotation($annotation->id);
234  }
235  }
236  }
237  /**
238  * Get newly create annoation id
239  *
240  * @return integer
241  */
242  public function getAnnotationId() {
243  return $this->annotation_inserted;
244  }
245  /**
246  * Search annotation by its type, owner etc
247  *
248  * @param array $params A valid options in format:
249  * @param string $params['search_type'] true(default) to performs matching on a per-character basis , false to performs matching on exact value.
250  * @param string $params['type'] Valid annotation type
251  * @param integer $params['subject_guid'] A valid subject guid, which results integer value
252  * @param integer $params['owner_guid'] A valid owner guid, which results integer value
253  * @param integer $params['limit'] Result limit default, Default is 20 values
254  * @param string $params['order_by'] To show result in sepcific order. There is no default order.
255  *
256  * reutrn array|false;
257  */
258  public function searchAnnotation(array $params = array()) {
259  self::initAttributes();
260  if(empty($params)) {
261  return false;
262  }
263  //prepare default attributes
264  $default = array(
265  'search_type' => true,
266  'type' => false,
267  'owner_guid' => false,
268  'annotation_id' => false,
269  'subject_guid' => false,
270  'limit' => false,
271  'order_by' => false,
272  'offset' => input('offset', '', 1),
273  'page_limit' => ossn_call_hook('pagination', 'page_limit', false, 10), //call hook for page limit
274  'count' => false
275  );
276  $options = array_merge($default, $params);
277  $wheres = array();
278 
279  //validate offset values
280  if($options['limit'] !== false && $options['limit'] !== 0 && $options['page_limit'] !== 0) {
281  $offset_vals = ceil($options['limit'] / $options['page_limit']);
282  $offset_vals = abs($offset_vals);
283  $offset_vals = range(1, $offset_vals);
284  if(!in_array($options['offset'], $offset_vals)) {
285  return false;
286  }
287  }
288  //get only required result, don't bust your server memory
289  $getlimit = $this->generateLimit($options['limit'], $options['page_limit'], $options['offset']);
290  if($getlimit) {
291  $options['limit'] = $getlimit;
292  }
293 
294  if(!empty($options['annotation_id'])) {
295  $wheres[] = "a.id='{$options['annotation_id']}'";
296  }
297  if(!empty($params['type'])) {
298  $wheres[] = "a.type='{$options['type']}'";
299  $wheres[] = "e.subtype='{$options['type']}'";
300  }
301  if(!empty($params['owner_guid'])) {
302  $wheres[] = "a.owner_guid ='{$options['owner_guid']}'";
303  }
304  if(!empty($params['subject_guid'])) {
305  $wheres[] = "a.subject_guid ='{$options['subject_guid']}'";
306  }
307  $wheres[] = "e.owner_guid=a.id";
308  $wheres[] = "e.type='annotation'";
309  $wheres[] = "emd.guid=e.guid";
310  //prepare search
311  $params = array();
312 
313  $params['from'] = 'ossn_annotations as a, ossn_entities as e , ossn_entities_metadata as emd';
314  $params['params'] = array(
315  'a.id',
316  'a.time_created',
317  'a.owner_guid',
318  'a.subject_guid',
319  'a.type',
320  'emd.value'
321  );
322  $params['wheres'] = array(
323  $this->constructWheres($wheres)
324  );
325  $params['order_by'] = $options['order_by'];
326  $params['limit'] = $options['limit'];
327 
328  if(!$options['order_by']) {
329  $params['order_by'] = "a.id ASC";
330  }
331 
332  $this->get = $this->select($params, true);
333 
334  //prepare count data;
335  if($options['count'] === true) {
336  unset($params['params']);
337  unset($params['limit']);
338  $count = array();
339  $count['params'] = array(
340  "count(*) as total"
341  );
342  $count = array_merge($params, $count);
343  return $this->select($count)->total;
344  }
345  if($this->get) {
346  foreach($this->get as $annotation) {
347  $merge = array(
348  $annotation->type => $annotation->value
349  );
350  //unset value
351  unset($annotation->value);
352 
353  //get object vars and then merge into arrays
354  $values = get_object_vars($annotation);
355  $merge = array_merge($values, $merge);
356 
357  $this->owner_guid = $annotation->id;
358  $this->type = 'annotation';
359  $this->order_by = '';
360  $entities = $this->get_entities();
361  if(!empty($entities)) {
362  foreach($entities as $entity) {
363  $entities_data[$entity->subtype] = $entity->value;
364  }
365  $merge = array_merge($merge, $entities_data);
366  unset($entities_data);
367  }
368  //construct object again
369  $annotations[] = arrayObject($merge, get_class($this));
370  }
371  return $annotations;
372  }
373  return false;
374  }
375  /**
376  * Can change
377  * Check if user can change the requested item or not
378  *
379  * @param object $user User
380  * @return boolean
381  */
382  public function canChange($user = '') {
383  if(empty($user)) {
384  $user = ossn_loggedin_user();
385  }
386  $allowed = false;
387  if(isset($user->guid) && $user instanceof OssnUser) {
388  if((isset($this->owner_guid) && $this->owner_guid == $user->guid) || ossn_isAdminLoggedin()) {
389  $allowed = true;
390  }
391  }
392  return ossn_call_hook('user', 'can:change', $this, $allowed);
393  }
394 } //class