OssnObject.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 OssnObject extends OssnEntities {
12 
13  /**
14  * Initialize the objects.
15  *
16  * @return void
17  */
18  public function initAttributes() {
19  $this->time_created = time();
20  if(empty($this->subtype)) {
21  $this->subtype = NULL;
22  }
23  if(empty($this->order_by)) {
24  $this->order_by = '';
25  }
26  }
27 
28  /**
29  * Create objects
30  *
31  * requires $object->(owner_guid, type, subtype, title, description)
32  *
33  * @return boolean
34  */
35  public function addObject() {
36  self::initAttributes();
37  if(empty($this->owner_guid) || empty($this->type)) {
38  return false;
39  }
40  $params['into'] = 'ossn_object';
41  $params['names'] = array(
42  'owner_guid',
43  'type',
44  'subtype',
45  'time_created',
46  'title',
47  'description'
48  );
49  $params['values'] = array(
50  $this->owner_guid,
51  $this->type,
52  $this->subtype,
53  $this->time_created,
54  $this->title,
55  $this->description
56  );
57  if($this->insert($params)) {
58  $this->createdObject = $this->getLastEntry();
59  if(isset($this->data) && is_object($this->data)) {
60  foreach($this->data as $name => $value) {
61  $this->owner_guid = $this->createdObject;
62  $this->type = 'object';
63  $this->subtype = $name;
64  $this->value = $value;
65  $this->add();
66  }
67  }
68  return true;
69  }
70  return false;
71  }
72  /**
73  * Get object by owner guid;
74  *
75  * Requires $object->owner_guid
76  * $object->order_by To sort the data in a recordset
77  *
78  * @return object
79  */
80  public function getObjectByOwner() {
81  if(empty($this->type)) {
82  return false;
83  }
84  $params = array();
85  $params['type'] = $this->type;
86  $params['subtype'] = $this->subtype;
87  $params['owner_guid'] = $this->owner_guid;
88  $params['limit'] = $this->limit;
89  $params['order_by'] = $this->order_by;
90  $params['count'] = $this->count;
91  $params['page_limit'] = $this->page_limit;
92  $params['offset'] = $this->offset;
93  $objects = $this->searchObject($params);
94  if($objects) {
95  return $objects;
96  }
97  return false;
98  }
99 
100  /**
101  * Get object by types;
102  *
103  * Requires : $object->(type , subtype(optional))
104  * $object->order_by To sort the data in a recordset
105  *
106  * @return object
107  */
108  public function getObjectsByTypes() {
109  $params = array();
110  $params['type'] = $this->type;
111  $params['subtype'] = $this->subtype;
112  $params['owner_guid'] = $this->owner_guid;
113  $params['limit'] = $this->limit;
114  $params['order_by'] = $this->order_by;
115  $params['count'] = $this->count;
116  $params['page_limit'] = $this->page_limit;
117  $params['offset'] = $this->offset;
118  $objects = $this->searchObject($params);
119  if($objects) {
120  return $objects;
121  }
122  return false;
123  }
124 
125  /**
126  * Get object by object guid;
127  *
128  * Requires : $object->object_guid
129  *
130  * @return object;
131  */
132  public function getObjectById() {
133  self::initAttributes();
134  if(empty($this->object_guid)) {
135  return false;
136  }
137  $params['from'] = 'ossn_object as o';
138  $params['wheres'] = array(
139  "o.guid='{$this->object_guid}'"
140  );
141  //there is no need to order as its will fetch only one record
142  //$params['order_by'] = $this->order_by;
143  unset($this->order_by);
144 
145  $object = $this->select($params);
146 
147  $this->owner_guid = $object->guid;
148  $this->subtype = '';
149  $this->type = 'object';
150  $this->entities = $this->get_entities();
151 
152  if($this->entities && $object) {
153  foreach($this->entities as $entity) {
154  $fields[$entity->subtype] = $entity->value;
155  }
156  $object_array = get_object_vars($object);
157  if(is_array($object_array)){
158  $data = array_merge($object_array, $fields);
159  }
160  if(!empty($fields)) {
161  return arrayObject($data, get_class($this));
162  }
163  }
164  if(empty($fields)) {
165  return arrayObject($object, get_class($this));
166  }
167  return false;
168  }
169 
170  /**
171  * Get newly created object
172  *
173  * @return void|integer
174  */
175  public function getObjectId() {
176  if(isset($this->createdObject)) {
177  return $this->createdObject;
178  }
179  }
180 
181  /**
182  * Update Object;
183  *
184  * @param array $name Names
185  * @param array $value Values
186  * @param integer $guid Object guid
187  *
188  * @return boolean
189  */
190  public function updateObject($name, $value, $guid) {
191  self::initAttributes();
192  $params['table'] = 'ossn_object';
193  $params['names'] = $name;
194  $params['values'] = $value;
195  $params['wheres'] = array(
196  "guid='{$guid}'"
197  );
198  if($this->update($params)) {
199  if(isset($this->data)) {
200  $this->owner_guid = $guid;
201  $this->type = 'object';
202  $this->save();
203  }
204  return true;
205  }
206  return false;
207  }
208 
209  /**
210  * Delete object;
211  *
212  * @param integer $object Object guid
213  *
214  * @return boolean
215  */
216  public function deleteObject($object) {
217  self::initAttributes();
218  if(isset($this->guid)) {
219  $object = $this->guid;
220  }
221  //delete entites of (this) object
222  if($this->deleteByOwnerGuid($object, 'object')) {
223  $data = ossn_get_userdata("object/{$object}/");
224  if(is_dir($data)) {
225  OssnFile::DeleteDir($data);
226  }
227  }
228  $delete['from'] = 'ossn_object';
229  $delete['wheres'] = array(
230  "guid='{$object}'"
231  );
232  if($this->delete($delete)) {
233  return true;
234  }
235  return false;
236  }
237  /**
238  * Search object by its title, description etc
239  *
240  * @param array $params A valid options in format:
241  * 'search_type' => true(default) to performs matching on a per-character basis
242  * false for performs matching on exact value.
243  * 'subtype' => Valid object subtype
244  * 'type' => Valid object type
245  * 'title' => Valid object title
246  * 'description' => Valid object description
247  * 'owner_guid' => A valid owner guid, which results integer value
248  * 'limit' => Result limit default, Default is 20 values
249  * 'order_by' => To show result in sepcific order. There is no default order.
250  *
251  * reutrn array|false;
252  *
253  */
254  public function searchObject(array $params = array()) {
255  self::initAttributes();
256  if(empty($params)) {
257  return false;
258  }
259  //prepare default attributes
260  $default = array(
261  'search_type' => true,
262  'subtype' => false,
263  'type' => false,
264  'owner_guid' => false,
265  'limit' => false,
266  'order_by' => false,
267  'offset' => input('offset', '', 1),
268  'page_limit' => ossn_call_hook('pagination', 'page_limit', false, 10), //call hook for page limit
269  'count' => false
270  );
271  $options = array_merge($default, $params);
272  $wheres = array();
273 
274  //validate offset values
275  if($options['limit'] !== false && $options['limit'] != 0 && $options['page_limit'] != 0) {
276  $offset_vals = ceil($options['limit'] / $options['page_limit']);
277  $offset_vals = abs($offset_vals);
278  $offset_vals = range(1, $offset_vals);
279  if(!in_array($options['offset'], $offset_vals)) {
280  return false;
281  }
282  }
283  //get only required result, don't bust your server memory
284  $getlimit = $this->generateLimit($options['limit'], $options['page_limit'], $options['offset']);
285  if($getlimit) {
286  $options['limit'] = $getlimit;
287  }
288 
289  if(!empty($options['object_guid'])) {
290  $wheres[] = "o.guid='{$options['object_guid']}'";
291  }
292  if(!empty($options['subtype'])) {
293  $wheres[] = "o.subtype='{$options['subtype']}'";
294  }
295  if(!empty($params['type'])) {
296  $wheres[] = "o.type='{$options['type']}'";
297  }
298  if(!empty($params['owner_guid'])) {
299  $wheres[] = "o.owner_guid ='{$options['owner_guid']}'";
300  }
301  //check if developer want to search title or description
302  if($options['search_type'] === true) {
303  if(!empty($params['title'])) {
304  $wheres[] = "o.title LIKE '%{$options['title']}%'";
305  }
306  if(!empty($params['description'])) {
307  $wheres[] = "o.description LIKE '%{$options['description']}%'";
308  }
309  } elseif($options['search_type'] === false) {
310  if(!empty($params['title'])) {
311  $wheres[] = "o.title = '{$options['title']}'";
312  }
313  if(!empty($params['description'])) {
314  $wheres[] = "o.description = '{$options['description']}'";
315  }
316  }
317  //prepare search
318  $params = array();
319 
320  $params['from'] = 'ossn_object as o';
321  $params['params'] = array(
322  'o.guid',
323  'o.time_created',
324  'o.owner_guid',
325  'o.description',
326  'o.title',
327  'o.subtype'
328  );
329  $params['wheres'] = array(
330  $this->constructWheres($wheres)
331  );
332  $params['order_by'] = $options['order_by'];
333  $params['limit'] = $options['limit'];
334 
335  if(!$options['order_by']){
336  $params['order_by'] = "o.guid ASC";
337  }
338  $this->get = $this->select($params, true);
339 
340  //prepare count data;
341  if($options['count'] === true) {
342  unset($params['params']);
343  unset($params['limit']);
344  $count = array();
345  $count['params'] = array(
346  "count(*) as total"
347  );
348  $count = array_merge($params, $count);
349  return $this->select($count)->total;
350  }
351  if($this->get) {
352  foreach($this->get as $object) {
353  $this->object_guid = $object->guid;
354  $objects[] = $this->getObjectById();
355  }
356  return $objects;
357  }
358  return false;
359  }
360 }