OssnEntities.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  */
11 class OssnEntities extends OssnDatabase {
12  /**
13  * Initialize the objects.
14  *
15  * @return void
16  */
17  private function initAttributes() {
18  $this->data = new stdClass;
19  $this->time_created = time();
20  $this->time_updated = '';
21  $this->active = 1;
22 
23  if(empty($this->permission)) {
24  $this->permission = OSSN_PUBLIC;
25  }
26 
27  $this->types = array(
28  'object' => 'OssnObject',
29  'user' => 'OssnUser',
30  'annotation' => 'OssnAnnotation',
31  'entity' => 'OssnEntities',
32  'site' => 'OssnSite',
33  'component' => 'OssnComponents'
34  );
35 
36  //generate entity types from $this->types
37  foreach($this->types as $type => $class) {
38  $this->entity_types[] = $type;
39  }
40 
41  if(empty($this->order_by)) {
42  $this->order_by = '';
43  }
44  if(empty($this->limit)) {
45  $this->limit = false;
46  }
47  if(empty($this->type)) {
48  $this->type = 'entity';
49  }
50  $this->data = new stdClass;
51  $this->annotations = new OssnAnnotation;
52 
53  if(!isset($this->offset)) {
54  $this->offset = 1;
55  }
56  if(!isset($this->page_limit)) {
57  //default OssnPagination limit
58  $this->page_limit = ossn_call_hook('pagination', 'per_page', false, 10);
59  }
60  if(!isset($this->count)) {
61  $this->count = false;
62  }
63  }
64 
65  /**
66  * Add new entity.
67  *
68  * Requires object $this->type => entity type; (this usually is user, object, annotation, site)
69  * $this->subtype => entity subtype;
70  * $this->entity_permission => OSSN_ACCESS
71  * $this->active = is entity is active or not
72  * $this->value = data you want to insert
73  * $this->owner_guid = entity owner guid
74  *
75  * @return boolean
76  */
77  public function add() {
78  self::initAttributes();
79  if(!empty($this->owner_guid) && in_array($this->type, $this->entity_types)) {
80  $this->params['into'] = 'ossn_entities';
81  $this->params['names'] = array(
82  'owner_guid',
83  'type',
84  'subtype',
85  'time_created',
86  'time_updated',
87  'permission',
88  'active'
89  );
90  $this->params['values'] = array(
91  $this->owner_guid,
92  $this->type,
93  $this->subtype,
94  $this->time_created,
95  $this->time_updated,
96  $this->permission,
97  $this->active
98  );
99  if($this->insert($this->params)) {
100  $this->params['into'] = 'ossn_entities_metadata';
101  $this->params['names'] = array(
102  'guid',
103  'value'
104  );
105  $this->params['values'] = array(
106  $this->getLastEntry(),
107  $this->value
108  );
109  $this->insert($this->params);
110  return true;
111  }
112  }
113  return false;
114  }
115  /**
116  * Get Entity.
117  *
118  * Requires object $this->entity_guid Entity guid in database;
119  *
120  * @return object|false
121  */
122  public function get_entity() {
123  self::initAttributes();
124 
125  $params = array();
126  $params['from'] = 'ossn_entities as e';
127  $params['params'] = array(
128  'e.guid',
129  'e.time_created',
130  'e.time_updated',
131  'e.permission',
132  'e.active',
133  'e.owner_guid',
134  'emd.value',
135  'e.type',
136  'e.subtype'
137  );
138  $params['joins'] = "JOIN ossn_entities_metadata as emd ON e.guid=emd.guid";
139  $params['wheres'] = array(
140  "e.guid ='{$this->entity_guid}'"
141  );
142 
143  $data = $this->select($params);
144  if($data) {
145  $entity = arrayObject($data, $this->types[$this->type]);
146  return $entity;
147  }
148  }
149 
150  /**
151  * Update Entity in database.
152  *
153  * Requires $object->data
154  *
155  * @return boolean
156  */
157  public function save() {
158  if(!empty($this->owner_guid)) {
159  $this->datavars = $this->get_data_vars();
160  foreach($this->get_entities() as $entity) {
161  if(isset($this->datavars[$entity->subtype])) {
162  $params['table'] = 'ossn_entities_metadata';
163  $params['names'] = array(
164  'value'
165  );
166  $params['values'] = array(
167  $this->datavars[$entity->subtype]
168  );
169  $params['wheres'] = array(
170  "guid='{$entity->guid}'"
171  );
172  if($this->update($params)) {
173  $params['table'] = 'ossn_entities';
174  $params['names'] = array(
175  'time_updated'
176  );
177  $params['values'] = array(
178  time()
179  );
180  $params['wheres'] = array(
181  "guid='{$entity->guid}'"
182  );
183  $this->update($params);
184  }
185  }
186  }
187  // i don't think we need to add new data on save $arsalanshah; v1.x to 2.x
188  // added again in v3.0 $arsalanshah
189  //code re arrange 1st July 2015 $arsalanshah
190  if(!empty($this->datavars)){
191  foreach($this->datavars as $vars => $value) {
192  if(!in_array($vars, $this->get_data_dbvars())) {
193  $this->subtype = $vars;
194  $this->value = $value;
195  $this->add();
196  }
197  }
198  }
199  return true;
200  }
201  return false;
202  }
203 
204  /**
205  * Get data object.
206  *
207  * Requires $object->data
208  *
209  * @return false|arrray;
210  */
211  private function get_data_vars() {
212  if(!$this->data) {
213  return false;
214  }
215  foreach($this->data as $name => $value) {
216  $vars[$name] = $value;
217  }
218  return $vars;
219  }
220 
221  /**
222  * Get entities.
223  *
224  * Requires object $this->type => entity type;
225  * $this->subtype => entity subtype;
226  * $this->owner_guid => guid of entity owner
227  * $this->order_by = to sort the data in a recordset
228  *
229  * @return object
230  */
231  public function get_entities() {
232  self::initAttributes();
233  $options = array(
234  'subtype' => $this->subtype,
235  'type' => $this->type,
236  'owner_guid' => $this->owner_guid,
237  'offset' => $this->offset,
238  'order_by' => $this->order_by,
239  'page_limit' => $this->page_limit,
240  'count' => $this->count,
241  'limit' => $this->limit
242  );
243  return $this->searchEntities($options);
244  }
245 
246  /**
247  * Get newly added entity guid.
248  *
249  * @return integer
250  */
251  public function AddedEntityGuid() {
252  return $this->getLastEntry();
253  }
254 
255  /**
256  * Update entity metadata only.
257  *
258  * @return bool;
259  */
260  public function updateEntity() {
261  if(!empty($this->guid)) {
262 
263  $params['table'] = 'ossn_entities_metadata';
264  $params['names'] = array(
265  'value'
266  );
267  $params['values'] = array(
268  $this->value
269  );
270  $params['wheres'] = array(
271  "guid='{$this->guid}'"
272  );
273 
274  if($this->update($params)) {
275 
276  $params['table'] = 'ossn_entities';
277  $params['names'] = array(
278  'time_updated'
279  );
280  $params['values'] = array(
281  time()
282  );
283  $params['wheres'] = array(
284  "guid='{$this->guid}'"
285  );
286 
287  $this->update($params);
288  return true;
289  }
290  }
291  return false;
292  }
293 
294  /**
295  * Delete all entities related to owner guid.
296  *
297  * @param integer $guid Entity guid in database
298  * @param string $type Entity type
299  *
300  * @todo why not there is subtype?
301  * @return boolean
302  */
303  public function deleteByOwnerGuid($guid, $type) {
304 
305  $params['from'] = 'ossn_entities';
306  $params['wheres'] = array(
307  "owner_guid='{$guid}' AND type='{$type}'"
308  );
309 
310  $ids = $this->select($params, true);
311  if(!$ids) {
312  return false;
313  }
314  foreach($ids as $entity) {
315  $this->deleteEntity($entity->guid);
316  }
317  return true;
318  }
319 
320  /**
321  * Delete entity.
322  *
323  * @param integer $guid Entity guid in database
324  *
325  * @return boolean
326  */
327  public function deleteEntity($guid) {
328  if(isset($this->guid) && !empty($this->guid) && empty($guid)) {
329  $guid = $this->guid;
330  }
331  $params['from'] = 'ossn_entities';
332  $params['wheres'] = array(
333  "guid = '{$guid}'"
334  );
335 
336  if($this->delete($params)) {
337  $metadata['from'] = 'ossn_entities_metadata';
338  $metadata['wheres'] = array(
339  "guid = '{$guid}'"
340  );
341  $this->delete($metadata);
342 
343  $vars['entity'] = $guid;
344  ossn_trigger_callback('delete', 'entity', $vars);
345  return true;
346  }
347  return false;
348  }
349  /**
350  * Get subtypes from entites.
351  *
352  * Requires $object->data
353  *
354  * @return array
355  */
356  private function get_data_dbvars() {
357  $entities = $this->get_entities();
358  if($entities) {
359  foreach($entities as $entity) {
360  $vars[] = $entity->subtype;
361  }
362  return $vars;
363  }
364  return false;
365  }
366  /**
367  * Search entities
368  *
369  * @param array $params A valid options in format:
370  * 'search_type' => true(default) to performs matching on a per-character basis
371  * false for performs matching on exact value.
372  * 'subtype' => Valid entity subtype
373  * 'type' => Valid entity type
374  * 'value' => Value which you want to search
375  * 'owner_guid' => A valid owner guid, which results integer value
376  * 'limit' => Result limit default, Default is 20 values
377  * 'order_by' => To show result in sepcific order. There is no default order.
378  *
379  * reutrn array|false;
380  *
381  */
382  public function searchEntities(array $params = array()) {
383  self::initAttributes();
384  //set default values
385  $default = array(
386  'search_type' => true,
387  'subtype' => false,
388  'type' => false,
389  'value' => false,
390  'owner_guid' => false,
391  'limit' => false,
392  'order_by' => false,
393  'offset' => 1,
394  'page_limit' => ossn_call_hook('pagination', 'per_page', false, 10), //call hook for page limit
395  'count' => false
396  );
397  $options = array_merge($default, $params);
398  $wheres = array();
399  //prepare limit
400  $limit = $options['limit'];
401 
402  //validate offset values
403  if($options['limit'] !== false && $options['limit'] !== 0 && $options['page_limit'] !== 0) {
404  $offset_vals = ceil($options['limit'] / $options['page_limit']);
405  $offset_vals = abs($offset_vals);
406  $offset_vals = range(1, $offset_vals);
407  if(!in_array($options['offset'], $offset_vals)) {
408  return false;
409  }
410  }
411  //get only required result, don't bust your server memory
412  $getlimit = $this->generateLimit($options['limit'], $options['page_limit'], $options['offset']);
413  if($getlimit) {
414  $options['limit'] = $getlimit;
415  }
416 
417  //search entities
418  if(!empty($options['subtype'])) {
419  $wheres[] = "e.subtype='{$options['subtype']}'";
420  }
421  if(!empty($options['type'])) {
422  $wheres[] = "e.type='{$options['type']}'";
423  }
424  if(!empty($options['owner_guid'])) {
425  $wheres[] = "e.owner_guid ='{$options['owner_guid']}'";
426  }
427  if(!empty($options['value']) && $options['search_type'] === true) {
428  $wheres[] = "emd.value LIKE '%{$options['value']}%'";
429  } elseif(!empty($options['value']) && $options['search_type'] === false) {
430  $wheres[] = "emd.value = '{$options['value']}'";
431  }
432  $params = array();
433  $params['from'] = 'ossn_entities as e';
434  $params['params'] = array(
435  'e.guid',
436  'e.time_created',
437  'e.time_updated',
438  'e.permission',
439  'e.active',
440  'e.owner_guid',
441  'emd.value',
442  'e.type',
443  'e.subtype'
444  );
445  $params['joins'] = "JOIN ossn_entities_metadata as emd ON e.guid=emd.guid";
446  $params['wheres'] = array(
447  $this->constructWheres($wheres)
448  );
449  $params['order_by'] = $options['order_by'];
450  $params['limit'] = $options['limit'];
451 
452  if(!$options['order_by']){
453  $params['order_by'] = "e.guid ASC";
454  }
455  $this->get = $this->select($params, true);
456 
457  //prepare count data;
458  if($options['count'] === true) {
459  unset($params['params']);
460  unset($params['limit']);
461  $count = array();
462  $count['params'] = array(
463  "count(*) as total"
464  );
465  $count = array_merge($params, $count);
466  return $this->select($count)->total;
467  }
468  if($this->get) {
469  foreach($this->get as $entity) {
470  //prepare entities for display
471  $entities[] = arrayObject($entity, $this->types[$this->type]);
472  }
473  return $entities;
474  }
475  return false;
476  }
477  /**
478  * Can change
479  * Check if user can change the requested item or not
480  *
481  * @param object $user User
482  * @return boolean
483  */
484  public function canChange($user = ''){
485  if(empty($user)){
486  $user = ossn_loggedin_user();
487  }
488  $allowed = false;
489  if(isset($user->guid) && $user instanceof OssnUser){
490  if((isset($this->owner_guid) && $this->type == 'user' && $this->owner_guid == $user->guid) || ossn_isAdminLoggedin()){
491  $allowed = true;
492  }
493  }
494  return ossn_call_hook('user' , 'can:change', $this, $allowed);
495  }
496 } //class