ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentObject.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  private $db = null;
15  private $user_obj = null;
16  private $pobject_id = null;
17  private $ref_id = null;
18  private $status = null;
19  private $pay_method = null;
20  private $vendor_id = null;
21  private $topic_id = 0;
22  private $vat_id = 0;
23  private $subtype = null;
24  private $is_special = 0;
25 
26 
27  public function __construct($user_obj, $a_pobject_id = null)
28  {
29  global $ilDB;
30 
31  $this->db = $ilDB;
32  $this->user_obj = $user_obj;
33 
34  $this->STATUS_NOT_BUYABLE = 0;
35  $this->STATUS_BUYABLE = 1;
36  $this->STATUS_EXPIRES = 2;
37 
38  $this->PAY_METHOD_NOT_SPECIFIED = 0;
39 
40  $this->pobject_id = $a_pobject_id;
41 
42  include_once './Services/Payment/classes/class.ilPayMethods.php';
43  $pmObj = new ilPayMethods();
44  $tmp = $pmObj->readAll();
45 
46  foreach($tmp as $pm)
47  {
48  $tmp = strtoupper($pm['pm_title']);
49  $this->PAY_METHOD_.$tmp = $pm['pm_id'];
50  }
51 
52 
53  $this->__read();
54 
55  }
56 
57  // SETTER GETTER
58  public function getTopicId()
59  {
60  return $this->topic_id;
61  }
62  public function setTopicId($a_topic_id)
63  {
64  $this->topic_id = $a_topic_id;
65  }
66  public function getPobjectId()
67  {
68  return $this->pobject_id;
69  }
70  public function setRefId($a_ref_id)
71  {
72  $this->ref_id = $a_ref_id;
73  }
74  public function getRefId()
75  {
76  return $this->ref_id;
77  }
78  public function setStatus($a_status)
79  {
80  $this->status = $a_status;
81  }
82  public function getStatus()
83  {
84  return $this->status;
85  }
86  public function setPayMethod($a_method)
87  {
88  $this->pay_method = $a_method;
89  }
90  public function getPayMethod()
91  {
92  return $this->pay_method;
93  }
94  public function setVendorId($a_vendor_id)
95  {
96  $this->vendor_id = $a_vendor_id;
97  }
98  public function getVendorId()
99  {
100  return $this->vendor_id;
101  }
102  public function getVatId()
103  {
104  return $this->vat_id;
105  }
106  public function setVatId($a_vat_id)
107  {
108  $this->vat_id = $a_vat_id;
109  }
110 
111  public function setSubtype($a_subtype)
112  {
113  $this->subtype = $a_subtype;
114  }
115 
116  public function getSubtype()
117  {
118  return $this->subtype;
119  }
120  public function setSpecial($a_special)
121  {
122  $this->is_special = $a_special;
123  }
124  public function getSpecial()
125  {
126  return $this->is_special;
127  }
128 
129  function getVat($a_amount = 0, $type = 'CALCULATION')
130  {
131  $oVAT = new ilShopVats($this->getVatId());
132  switch($type)
133  {
134  case 'CALCULATION':
135  $val = (float)($a_amount - (round(($a_amount / (1 + ($oVAT->getRate() / 100))) * 100) / 100));
136  return number_format((float)$val,'2','.','');
137  default:
138  global $lng;
139 
140  $val = (float)($a_amount - (round(($a_amount / (1 + ($oVAT->getRate() / 100))) * 100) / 100));
141  $val = ilShopUtils::_formatFloat($val);
142  return $val;
143  }
144  }
145 
146  public function add()
147  {
148  $next_id = $this->db->nextId('payment_objects');
149 
150  $this->db->insert('payment_objects',array(
151  'pobject_id' => array('integer', $next_id),
152  'ref_id' => array('integer', $this->getRefId()),
153  'status' => array('integer', $this->getStatus()),
154  'pay_method' => array('integer', $this->getPayMethod()),
155  'vendor_id' => array('integer', $this->getVendorId()),
156  'pt_topic_fk' => array('integer', $this->getTopicId()),
157  'vat_id' => array('integer', $this->getVatId()),
158  'subtype' => array('text', $this->getSubtype()),
159  'is_special' => array('integer', $this->getSpecial())
160  ));
161  return $next_id;
162  }
163 
164  public function delete()
165  {
166  if($this->getPobjectId())
167  {
168  include_once 'Services/Payment/classes/class.ilFileDataShop.php';
169  $oFileData = new ilFileDataShop($this->getPobjectId());
170  $oFileData->deassignFileFromPaymentObject();
171 
172  $statement = $this->db->manipulateF('DELETE FROM payment_objects WHERE pobject_id = %s',
173  array('integer'), array($this->getPobjectId()));
174 
175 
176  return true;
177  }
178 
179  return false;
180  }
181 
182  public function update()
183  {
184  if((int)$this->getPobjectId())
185  {
186  $this->db->update('payment_objects',array(
187  'ref_id' => array('integer', $this->getRefId()),
188  'status' => array('integer', $this->getStatus()),
189  'pay_method' => array('integer', $this->getPayMethod()),
190  'vendor_id' => array('integer', $this->getVendorId()),
191  'pt_topic_fk' => array('integer', $this->getTopicId()),
192  'vat_id' => array('integer', $this->getVatId()),
193  'subtype' => array('text', $this->getSubtype()),
194  'is_special' => array('integer', $this->getSpecial())
195  ),
196  array('pobject_id' => array('integer', $this->getPobjectId())));
197 
198  return true;
199  }
200  else
201  return false;
202  }
203  // STATIC
204  public static function _lookupPobjectId($a_ref_id)
205  {
206  global $ilDB;
207 
208  $res = $ilDB->queryf('
209  SELECT * FROM payment_objects
210  WHERE ref_id = %s',
211  array('integer'),
212  array($a_ref_id));
213 
214  while($row = $ilDB->fetchAssoc($res))
215  {
216  return $row['pobject_id'];
217  }
218  return 0;
219  }
220 
221  public static function _lookupTopicId($a_ref_id)
222  {
223  global $ilDB;
224 
225  static $cache = array();
226  if(isset($cache[$a_ref_id]))
227  {
228  return $cache[$a_ref_id];
229  }
230 
231  $result = $ilDB->queryf('SELECT pt_topic_fk FROM payment_objects WHERE ref_id = %s',
232  array('integer'),array($a_ref_id));
233 
234  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
235  {
236  $cache[$a_ref_id] = $row->pt_topic_fk;
237  return (int)$cache[$a_ref_id];
238  }
239 
240  return 0;
241  }
242 
244  {
245  global $ilDB;
246 
247 
248  $result = $ilDB->queryf('SELECT COUNT(pay_method) pm FROM payment_objects WHERE pay_method = %s',
249  array('integer'), array($a_id));
250 
251  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
252  {
253  return (int)$row->pm;
254  }
255 
256  return 0;
257  }
258 
259  function _getObjectsData($a_user_id)
260  {
261  global $ilDB;
262 
263  // get all vendors user is assigned to
264  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
265  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
266 
267  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_user_id);
268 
269  if(ilPaymentVendors::_isVendor($a_user_id))
270  {
271  $vendors[] = $a_user_id;
272  }
273 
274  if(!count($vendors))
275  {
276  return array();
277  }
278 
279  $data_types = array();
280  $data_values = array();
281  $cnt_vendors = count($vendors);
282 
283  $query = 'SELECT * FROM payment_objects WHERE vendor_id IN';
284 
285  if (is_array($vendors) &&
286  $cnt_vendors > 0)
287  {
288  $in = '(';
289  $counter = 0;
290  foreach($vendors as $vendor)
291  {
292  array_push($data_values, $vendor);
293  array_push($data_types, 'integer');
294 
295  if($counter > 0) $in .= ',';
296  $in .= '%s';
297  ++$counter;
298  }
299  $in .= ')';
300  $query .= $in;
301  }
302 
303  $res= $ilDB->queryf($query, $data_types, $data_values);
304 
305 
306  while($row = $ilDB->fetchObject($res))
307  {
308  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
309  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
310  $objects[$row->pobject_id]['status'] = $row->status;
311  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
312  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
313  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
314  $objects[$row->pobject_id]['vat_id'] = $row->vat_id;
315  $objects[$row->pobject_id]['subtype'] = $row->subtype;
316  $objects[$row->pobject_id]['is_special'] = $row->is_special;
317  }
318  return $objects ? $objects : array();
319  }
320 
322  {
323  global $ilDB;
324 
325  $data_types = array();
326  $data_values = array();
327 
328  $query = 'SELECT * FROM payment_objects ';
329 
330  if ($_SESSION['pay_objects']['title_value'] != '')
331  {
332  $query .= ', object_reference obr, object_data od ';
333  }
334 
335  if ($_SESSION['pay_objects']['vendor'] != '')
336  {
337  $query .= ', usr_data ud ';
338  }
339 
340  $query .= ' WHERE 1 = 1 ';
341 
342  if ($_SESSION['pay_objects']['title_value'])
343  {
344  $query .= ' AND obr.ref_id = payment_objects.ref_id AND od.obj_id = obr.obj_id ';
345 
346  $search_string = '';
347 
348  $title_search = explode(' ', trim($_SESSION['pay_objects']['title_value']));
349  for ($i = 0; $i < count($title_search); $i++)
350  {
351  $title_search[$i] = trim($title_search[$i]);
352 
353  if ($title_search[$i] != '')
354  {
355  //$search_string .= " od.title LIKE ".$ilDB->quote("%".$title_search[$i]."%")." ";
356  $search_string .= ' od.title LIKE %s '; // ".$ilDB->quote("%".$title_search[$i]."%")." ";
357  array_push($data_types, 'text');
358  array_push($data_values,'%'.$title_search[$i].'%');
359 
360  switch ($_SESSION['pay_objects']['title_type'])
361  {
362  case 'or' :
363  if ($i < count($title_search) - 1)
364  {
365  $search_string .= ' OR ';
366  }
367  break;
368  case 'and' :
369  if ($i < count($title_search) - 1)
370  {
371  $search_string .= ' AND ';
372  }
373  break;
374  }
375  }
376  }
377 
378  if ($search_string != '')
379  {
380  $query .= ' AND (' . $search_string . ') ';
381  }
382  }
383 
384  if ($_SESSION['pay_objects']['vendor'] != '')
385  {
386  $query .= ' AND ud.usr_id = payment_objects.vendor_id AND login = %s';
387  array_push($data_types, 'text');
388  array_push($data_values, $_SESSION['pay_objects']['vendor']);
389  }
390 
391  if($_SESSION['pay_objects']['pay_method'] > 0)
392  {
393  $query .= ' AND pay_method = %s';
394  array_push($data_types, 'integer');
395  array_push($data_values, $_SESSION['pay_objects']['pay_method']);
396  }
397 
398  $res = $ilDB->queryf($query, $data_types, $data_values);
399 
400  while($row = $ilDB->fetchObject($res))
401  {
402  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
403  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
404  $objects[$row->pobject_id]['status'] = $row->status;
405  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
406  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
407  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
408  $objects[$row->pobject_id]['vat_id'] = $row->vat_id;
409  $objects[$row->pobject_id]['subtype'] = $row->subtype;
410  $objects[$row->pobject_id]['is_special'] = $row->is_special;
411  }
412  return $objects ? $objects : array();
413  }
414 
415  function _getObjectData($a_id)
416  {
417  global $ilDB;
418 
419  $res = $ilDB->queryf('
420  SELECT * FROM payment_objects
421  WHERE pobject_id = %s',
422  array('integer'), array($a_id));
423 
424  if (is_object($res))
425  {
426  return $ilDB->fetchAssoc($res);
427 
428  }
429 
430  return false;
431  }
432 
433  function _isPurchasable($a_ref_id, $a_vendor_id = 0, $a_check_trustee = false, $a_check_status = false)
434  {
435  global $ilDB;
436 
437  // In the moment it's not possible to sell one object twice
438 
439  $data = array();
440  $data_types = array();
441 
442 
443  $query = 'SELECT * FROM payment_objects WHERE ref_id = %s ';
444  $data_types[] = 'integer';
445  $data[]= $a_ref_id;
446 
447  // check if object is buyable !!
448  if($a_check_status)
449  {
450  $query .= 'AND status > %s ';
451  $data_types[] = 'integer';
452  $data[] = 0;
453  }
454 
455  if ($a_vendor_id > 0)
456  {
457  $query .= 'AND vendor_id = %s';
458  $data_types[] = 'integer';
459  $data[] = $a_vendor_id;
460 
461  if($a_check_trustee)
462  {
463  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
464  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
465 
466  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_vendor_id);
467  if(ilPaymentVendors::_isVendor($a_user_id))
468  {
469  $vendors[] = $a_user_id;
470  }
471 
472  if(is_array($vendors) && count($vendors))
473  {
474  $query .= ' OR '.$ilDB->in('vendor_id', $vendors, false, 'integer');
475  }
476  }
477  }
478 
479  $res = $ilDB->queryf($query, $data_types, $data);
480  $rows = $ilDB->numRows($res);
481 
482  return $rows ? true : false;
483 
484  }
485 
486  // base method to check access for a specific object
487  function _hasAccess($a_ref_id, $a_transaction = 0, $a_subtype ='')
488  {
489  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
490  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
491  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
492 
493  global $rbacsystem,$ilDB, $ilUser;
494 
495  // check write access
496  if($rbacsystem->checkAccess('write', $a_ref_id))
497  {
498  return true;
499  }
500  // check if user is vendor/trustee
501  $vendors_of_trustee = ilPaymentTrustees::_getVendorIdsByTrustee($ilUser->getId());
502 
503  if(!IS_PAYMENT_ENABLED)
504  {
505  return true;
506  }
507 
508  if($a_subtype)
509  {
510  $result = $ilDB->queryf('
511  SELECT * FROM payment_objects
512  WHERE ref_id = %s AND (status = %s or status = %s)
513  AND subtype = %s',
514  array('integer', 'integer', 'integer','text'),
515  array($a_ref_id, '1', '2',$a_subtype));
516  }
517  else
518  {
519  $result = $ilDB->queryf('
520  SELECT * FROM payment_objects
521  WHERE ref_id = %s
522  AND (status = %s OR status = %s)
523  OR (vendor_id = %s)',
524  array('integer', 'integer', 'integer','integer'),
525  array($a_ref_id, '1', '2',$ilUser->getId()));
526  }
527  while($row = $ilDB->fetchObject($result))
528  {
529  if($row->vendor_id == $ilUser->getId() || in_array($row->vendor_id, $vendors_of_trustee))
530  {
531  return true;
532  }
533  else
534  if(!ilPaymentBookings::_hasAccess($row->pobject_id, '', $a_transaction))
535  {
536  return false;
537  }
538  else
539  {
540  return true;
541  }
542  }
543  }
544 
545  // base method to check access for a specific object
546  function _getActivation($a_ref_id)
547  {
548  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
549 
550  global $rbacsystem,$ilDB;
551 
552  $res = $ilDB->queryf('
553  SELECT * FROM payment_objects
554  WHERE ref_id = %s
555  AND (status = %s OR status = %s)',
556  array('integer', 'integer', 'integer'),
557  array($a_ref_id, '1', '2'));
558 
559  $row = $ilDB->fetchObject($res);
560  return ilPaymentBookings::_getActivation($row->pobject_id);
561  }
562 
563  public static function _isBuyable($a_ref_id, $a_subtype = '')
564  {
565  global $ilDB;
566 
567 
568  if(!IS_PAYMENT_ENABLED)
569  {
570  return false;
571  }
572 
573  if($a_subtype)
574  {
575  $result = $ilDB->queryf('
576  SELECT * FROM payment_objects
577  WHERE ref_id = %s
578  AND (status = %s or status = %s)
579  AND subtype = %s',
580  array('integer', 'integer', 'integer','text'),
581  array($a_ref_id, '1', '2',$a_subtype));
582  }
583  else
584  {
585  $result = $ilDB->queryf('
586  SELECT * FROM payment_objects
587  WHERE ref_id = %s AND (status = %s or status = %s)',
588  array('integer', 'integer', 'integer'),
589  array($a_ref_id, '1', '2'));
590  }
591 
592  while($row = $ilDB->fetchObject($result))
593  {
594  return true;
595  }
596  return false;
597  }
598 
599  // checks if this new object already exists in payment_objects
600  public static function _isNewObject($a_ref_id)
601  {
602  global $ilDB;
603 
604  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE ref_id = %s',
605  array('integer'), array($a_ref_id));
606 
607  $rows = $ilDB->numRows($res);
608 
609  return $rows ? false : true;
610  }
611 
612 
620  public static function _requiresPurchaseToAccess($a_ref_id, $a_purchasetype = '')
621  {
622 
623  global $ilUser,$ilObjDataCache;
624  /* Check:
625  * - User has no Access -> return false
626  * - User has Access but there are also Extension-Prices available -> User is able to buy the Object AGAIN to extend duration
627  */
628 
629  if( (self::_isBuyable($a_ref_id) && !self::_hasAccess($a_ref_id))
630  || (self::_isBuyable($a_ref_id) && self::_hasAccess($a_ref_id)
631  && self::_hasExtensions($a_ref_id) && isset($a_purchasetype)))
632  {
633  return true;
634  }
635  else return false;
636  }
637 
638  public static function _isInCart($a_ref_id)
639  {
640  global $ilDB, $ilUser;
641 
642  if(ANONYMOUS_USER_ID == $ilUser->getId())
643  {
644  $result = $ilDB->queryf('
645  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
646  WHERE ref_id = %s
647  AND session_id = %s
648  AND po.pobject_id = psc.pobject_id',
649  array('integer', 'text'),
650  array($a_ref_id, session_id()));
651  }
652  else
653  {
654  $result = $ilDB->queryf('
655  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
656  WHERE ref_id = %s
657  AND customer_id = %s
658  AND po.pobject_id = psc.pobject_id',
659  array('integer', 'integer'),
660  array($a_ref_id, $ilUser->getId()));
661  }
662  while($row = $ilDB->fetchObject($result))
663  {
664  return true;
665  }
666 
667  return false;
668  }
669 
670  public static function _hasExtensions($a_ref_id)
671  {
672  global $ilDB;
673 
674  // user has already access to current object
675  // object contain buyable extension-prices
676 
677  $res = $ilDB->queryf('
678  SELECT * FROM payment_prices pp, payment_objects po
679  WHERE po.ref_id = %s
680  AND pp.pobject_id = po.pobject_id
681  AND pp.extension = %s',
682  array('integer', 'integer'), array($a_ref_id, 1));
683 
684  if($row = $ilDB->numRows($res))
685  {
686  return true;
687  }
688  return false;
689  }
690 
691  private function __read()
692  {
693  if($this->getPobjectId())
694  {
695  $result = $this->db->queryf('SELECT * FROM payment_objects WHERE pobject_id = %s',
696  array('integer'), array($this->getPobjectId()));
697 
698  while($row = $this->db->fetchObject($result))
699  {
700  $this->setRefId($row->ref_id);
701  $this->setStatus($row->status);
702  $this->setPayMethod($row->pay_method);
703  $this->setVendorId($row->vendor_id);
704  $this->setTopicId($row->pt_topic_fk);
705  $this->setVatId($row->vat_id);
706  $this->setSubtype($row->subtype);
707  $this->setSpecial($row->is_special);
708 
709  return true;
710  }
711  }
712 
713  return false;
714  }
715 
716  public static function _checkExcSubtype($a_ref_id)
717  {
718  global $ilDB;
719 
720  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE
721  ref_id = %s', array('integer'), array($a_ref_id));
722  $subtypes = array();
723  while($row = $ilDB->fetchAssoc($res))
724  {
725  $subtypes[] = $row['subtype'];
726  }
727  return $subtypes;
728  }
729 
730  public static function getAllBuyableObjects()
731  {
732  global $ilDB;
733 
734 
735  $res = $ilDB->queryF('SELECT ref_id FROM payment_objects WHERE status = %s',
736  array('integer'), array(1));
737 
738  while($row = $ilDB->fetchAssoc($res))
739  {
740  $obj[] = $row['ref_id'];
741  }
742  return $obj;
743  }
744 
745  public static function _getSpecialObjects()
746  {
747  global $ilDB;
748 
749  $res = $ilDB->queryF('SELECT * FROM payment_objects
750  WHERE is_special = %s AND status = %s
751  ORDER BY pt_topic_fk',
752  array('integer', 'integer'), array(1,1));
753 
754  while($row = $ilDB->fetchAssoc($res))
755  {
756  $obj[] = $row;
757  }
758  return $obj;
759  }
760 
761  public static function _getContainerObjects($a_ref_id)
762  {
763  global $ilDB, $tree;
764 
765  $filter = array( "lm", "sahs", "htlm",'file', 'exc', 'tst', 'icrs','crs','crsr','rcrs', 'glo');
766  $objects = $tree->getChildsByTypeFilter($a_ref_id, $filter);
767 
768  $res = array();
769  $counter =0;
770  foreach($objects as $object)
771  {
772  if(self::_isBuyable($object['ref_id']))
773  {
774  $res = $ilDB->queryF('SELECT * FROM payment_objects
775  WHERE ref_id = %s AND status = %s
776  ORDER BY pt_topic_fk',
777  array('integer', 'integer'), array($object['ref_id'],1));
778 
779  while($row = $ilDB->fetchAssoc($res))
780  {
781  $obj_res[$counter] = $row;
782  #$obj_res[$counter]['ref_id'] = $object['ref_id'];
783  $obj_res[$counter]['obj_id'] = $object['obj_id'];
784  $obj_res[$counter]['type'] = $object['type'];
785  $obj_res[$counter]['title'] = $object['title'];
786  $obj_res[$counter]['child'] = $object['child'];
787  $obj_res[$counter]['description'] = $object['description'];
788  }
789  }
790 
791  $counter++;
792  }
793  return $obj_res;
794  }
795 
796 } // END class.ilPaymentObject
797 ?>