ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentObject.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 define ('PAY_METHOD_NOT_SPECIFIED', 0);
25 define ('PAY_METHOD_BILL', 1);
26 define ('PAY_METHOD_BMF', 2);
27 define ('PAY_METHOD_PAYPAL', 3);
28 
38 {
39  private $db = null;
40  private $user_obj = null;
41  private $pobject_id = null;
42  private $ref_id = null;
43  private $status = null;
44  private $pay_method = null;
45  private $vendor_id = null;
46  private $topic_id = 0;
47 
48  public function __construct($user_obj, $a_pobject_id = null)
49  {
50  global $ilDB;
51 
52  $this->db = $ilDB;
53  $this->user_obj = $user_obj;
54 
55  $this->STATUS_NOT_BUYABLE = 0;
56  $this->STATUS_BUYABLE = 1;
57  $this->STATUS_EXPIRES = 2;
58 
63 
64  $this->pobject_id = $a_pobject_id;
65  $this->__read();
66  }
67 
68  // SETTER GETTER
69  public function getTopicId()
70  {
71  return $this->topic_id;
72  }
73  public function setTopicId($a_topic_id)
74  {
75  $this->topic_id = $a_topic_id;
76  }
77  public function getPobjectId()
78  {
79  return $this->pobject_id;
80  }
81  public function setRefId($a_ref_id)
82  {
83  $this->ref_id = $a_ref_id;
84  }
85  public function getRefId()
86  {
87  return $this->ref_id;
88  }
89  public function setStatus($a_status)
90  {
91  $this->status = $a_status;
92  }
93  public function getStatus()
94  {
95  return $this->status;
96  }
97  public function setPayMethod($a_method)
98  {
99  $this->pay_method = $a_method;
100  }
101  public function getPayMethod()
102  {
103  return $this->pay_method;
104  }
105  public function setVendorId($a_vendor_id)
106  {
107  $this->vendor_id = $a_vendor_id;
108  }
109  public function getVendorId()
110  {
111  return $this->vendor_id;
112  }
113 
114  public function add()
115  {
116  $statement = $this->db->prepareManip(
117  'INSERT INTO payment_objects
118  SET
119  ref_id = ?,
120  status = ?,
121  pay_method = ?,
122  vendor_id = ?,
123  pt_topic_fk = ?',
124  array('integer', 'integer', 'integer', 'integer', 'integer'));
125  $data = array($this->getRefId(),
126  $this->getStatus(),
127  $this->getPayMethod(),
128  $this->getVendorId(),
129  $this->getTopicId());
130  $this->db->execute($statement, $data);
131 
132  return (int)$this->db->getLastInsertId();
133  }
134 
135  public function delete()
136  {
137  if($this->getPobjectId())
138  {
139  include_once 'Services/Payment/classes/class.ilFileDataShop.php';
140  $oFileData = new ilFileDataShop($this->getPobjectId());
141  $oFileData->deassignFileFromPaymentObject();
142 
143  $statement = $this->db->prepareManip('DELETE FROM payment_objects WHERE pobject_id = ?',
144  array('integer'));
145  $data = array($this->getPobjectId());
146  $this->db->execute($statement, $data);
147 
148  return true;
149  }
150 
151  return false;
152  }
153 
154  public function update()
155  {
156  if((int)$this->getPobjectId())
157  {
158  $statement = $this->db->prepareManip(
159  'UPDATE payment_objects
160  SET
161  ref_id = ?,
162  status = ?,
163  pay_method = ?,
164  vendor_id = ?,
165  pt_topic_fk = ?
166  WHERE pobject_id = ?',
167  array('integer', 'integer', 'integer', 'integer', 'integer', 'integer'));
168  $data = array($this->getRefId(),
169  $this->getStatus(),
170  $this->getPayMethod(),
171  $this->getVendorId(),
172  $this->getTopicId(),
173  $this->getPobjectId());
174  $this->db->execute($statement, $data);
175 
176  return true;
177  }
178 
179  return false;
180  }
181  // STATIC
182  public function _lookupPobjectId($a_ref_id)
183  {
184  global $ilDB;
185 
186  $query = "SELECT * FROM payment_objects ".
187  "WHERE ref_id = '".$a_ref_id."'";
188 
189  $res = $ilDB->query($query);
190  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
191  {
192  return $row->pobject_id;
193  }
194  return 0;
195  }
196 
197  public static function _lookupTopicId($a_ref_id)
198  {
199  global $ilDB;
200 
201  static $cache = array();
202  if(isset($cache[$a_ref_id]))
203  {
204  return $cache[$a_ref_id];
205  }
206 
207  $statement = $ilDB->prepare('SELECT pt_topic_fk FROM payment_objects WHERE ref_id = ?',
208  array('integer'));
209  $result = $ilDB->execute($statement, array($a_ref_id));
210  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
211  {
212  $cache[$a_ref_id] = $row->pt_topic_fk;
213  return (int)$cache[$a_ref_id];
214  }
215 
216  return 0;
217  }
218 
219  function _getCountObjectsByPayMethod($a_type)
220  {
221  global $ilDB;
222 
223  switch($a_type)
224  {
225  case 'pm_bill':
226  $pm = 1;
227  break;
228 
229  case 'pm_bmf':
230  $pm = 2;
231  break;
232 
233  case 'pm_paypal':
234  $pm = 3;
235  break;
236 
237  default:
238  $pm = -1;
239  }
240 
241  $statement = $ilDB->prepare('SELECT COUNT(pay_method) AS pm FROM payment_objects WHERE pay_method = ?',
242  array('integer'));
243  $result = $ilDB->execute($statement, array($pm));
244  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
245  {
246  return (int)$row->pm;
247  }
248 
249  return 0;
250  }
251 
252  function _getObjectsData($a_user_id)
253  {
254  global $ilDB;
255 
256  // get all vendors user is assigned to
257  include_once './payment/classes/class.ilPaymentTrustees.php';
258  include_once './payment/classes/class.ilPaymentVendors.php';
259 
260  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_user_id);
261 
262  if(ilPaymentVendors::_isVendor($a_user_id))
263  {
264  $vendors[] = $a_user_id;
265  }
266 
267  if(!count($vendors))
268  {
269  return array();
270  }
271  $in = " IN ('";
272  $in .= implode("','",$vendors);
273  $in .= "')";
274 
275  $query = "SELECT * FROM payment_objects ".
276  "WHERE vendor_id ".$in;
277 
278  $res = $ilDB->query($query);
279  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
280  {
281  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
282  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
283  $objects[$row->pobject_id]['status'] = $row->status;
284  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
285  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
286  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
287  }
288  return $objects ? $objects : array();
289  }
290 
292  {
293  global $ilDB;
294 
295  $query = "SELECT * FROM payment_objects ";
296 
297  if ($_SESSION["pay_objects"]["title_value"] != "")
298  {
299  $query .= ", object_reference AS obr ";
300  $query .= ", object_data AS od ";
301  }
302 
303  if ($_SESSION['pay_objects']['vendor'] != "")
304  {
305  $query .= ", usr_data AS ud ";
306  }
307 
308  $query .= " WHERE 1 ";
309 
310  if ($_SESSION["pay_objects"]["title_value"])
311  {
312  $query .= " AND obr.ref_id = payment_objects.ref_id AND od.obj_id = obr.obj_id ";
313 
314  $search_string = "";
315 
316  $title_search = explode(" ", trim($_SESSION["pay_objects"]["title_value"]));
317  for ($i = 0; $i < count($title_search); $i++)
318  {
319  $title_search[$i] = trim($title_search[$i]);
320 
321  if ($title_search[$i] != "")
322  {
323  $search_string .= " od.title LIKE ".$ilDB->quote("%".$title_search[$i]."%")." ";
324 
325  switch ($_SESSION["pay_objects"]["title_type"])
326  {
327  case "or" :
328  if ($i < count($title_search) - 1) $search_string .= " OR ";
329  break;
330  case "and" :
331  if ($i < count($title_search) - 1) $search_string .= " AND ";
332  break;
333  }
334  }
335  }
336 
337  if ($search_string != "")
338  {
339  $query .= " AND (" . $search_string . ") ";
340  }
341  }
342 
343  if ($_SESSION['pay_objects']['vendor'] != "")
344  {
345  $query .= " AND ud.usr_id = payment_objects.vendor_id AND login = ".$ilDB->quote($_SESSION["pay_objects"]["vendor"])." ";
346  }
347 
348 
349  if ($_SESSION["pay_objects"]["pay_method"] == "1" ||
350  $_SESSION["pay_objects"]["pay_method"] == "2" ||
351  $_SESSION["pay_objects"]["pay_method"] == "3")
352  {
353  $query .= " AND pay_method = '" . $_SESSION["pay_objects"]["pay_method"] . "' ";
354  }
355 
356  $res = $ilDB->query($query);
357  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
358  {
359  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
360  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
361  $objects[$row->pobject_id]['status'] = $row->status;
362  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
363  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
364  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
365  }
366  return $objects ? $objects : array();
367  }
368 
369  function _getObjectData($a_id)
370  {
371  global $ilDB;
372 
373  $query = "SELECT * FROM payment_objects ".
374  "WHERE pobject_id = '".$a_id."'";
375 
376  $res = $ilDB->query($query);
377 
378  if (is_object($res))
379  {
380  return $res->fetchRow(DB_FETCHMODE_ASSOC);
381  }
382 
383  return false;
384  }
385 
386  function _isPurchasable($a_ref_id, $a_vendor_id = 0, $a_check_trustee = false)
387  {
388  global $ilDB;
389 
390  // In the moment it's not possible to sell one object twice
391  $query = "SELECT * FROM payment_objects ".
392  "WHERE ref_id = '".$a_ref_id."' ";
393  if ($a_vendor_id > 0)
394  {
395  $query .= "AND vendor_id = '".$a_vendor_id."' ";
396  if($a_check_trustee)
397  {
398  include_once './payment/classes/class.ilPaymentTrustees.php';
399  include_once './payment/classes/class.ilPaymentVendors.php';
400 
401  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_vendor_id);
402  if(ilPaymentVendors::_isVendor($a_user_id))
403  {
404  $vendors[] = $a_user_id;
405  }
406 
407  if(is_array($vendors) && count($vendors))
408  {
409  $in = " IN ('";
410  $in .= implode("','",$vendors);
411  $in .= "')";
412 
413  $query .= ' OR vendor_id '.$in;
414  }
415  }
416  }
417  #"AND status = '1' OR status = '3' ";
418 
419  $res = $ilDB->query($query);
420 
421  return $res->numRows() ? false : true;
422  }
423 
424  // base method to check access for a specific object
425  function _hasAccess($a_ref_id)
426  {
427  include_once './payment/classes/class.ilPaymentBookings.php';
428 
429  global $rbacsystem,$ilDB;
430 
431  // check write access
432  if($rbacsystem->checkAccess('write', $a_ref_id))
433  {
434  return true;
435  }
436 
437  include_once 'payment/classes/class.ilGeneralSettings.php';
438  if(!(bool)ilGeneralSettings::_getInstance()->get('shop_enabled'))
439  {
440  return true;
441  }
442 
443  $query = "SELECT * FROM payment_objects ".
444  "WHERE ref_id = '".$a_ref_id."' ".
445  "AND (status = '1' OR status = '2')";
446 
447  $res = $ilDB->query($query);
448  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
449  {
450  if(!ilPaymentBookings::_hasAccess($row->pobject_id))
451  {
452  return false;
453  }
454  }
455  return true;
456  }
457 
458  // base method to check access for a specific object
459  function _getActivation($a_ref_id)
460  {
461  include_once './payment/classes/class.ilPaymentBookings.php';
462 
463  global $rbacsystem,$ilDB;
464 
465  $query = "SELECT * FROM payment_objects ".
466  "WHERE ref_id = '".$a_ref_id."' ".
467  "AND (status = '1' OR status = '2')";
468 
469  $res = $ilDB->query($query);
470  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
471  return ilPaymentBookings::_getActivation($row->pobject_id);
472  }
473 
474  public static function _isBuyable($a_ref_id)
475  {
476  global $ilDB;
477 
478  include_once 'payment/classes/class.ilGeneralSettings.php';
479  if(!(bool)ilGeneralSettings::_getInstance()->get('shop_enabled'))
480  {
481  return false;
482  }
483 
484  $statement = $ilDB->prepare('SELECT * FROM payment_objects
485  WHERE ref_id = ? AND (status = 1 or status = 2)',
486  array('integer'));
487  $result = $ilDB->execute($statement, array($a_ref_id));
488  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
489  {
490  return true;
491  }
492 
493  return false;
494  }
495 
496  public static function _requiresPurchaseToAccess($a_ref_id)
497  {
498  return (bool)(self::_isBuyable($a_ref_id) && !self::_hasAccess($a_ref_id));
499  }
500 
501  public static function _isInCart($a_ref_id)
502  {
503  global $ilDB, $ilUser;
504 
505  $statement = $ilDB->prepare('SELECT psc_id
506  FROM payment_objects AS po, payment_shopping_cart AS psc
507  WHERE ref_id = ? AND customer_id = ? AND po.pobject_id = psc.pobject_id',
508  array('integer', 'integer'));
509  $result = $ilDB->execute($statement, array($a_ref_id, $ilUser->getId()));
510  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
511  {
512  return true;
513  }
514 
515  return false;
516  }
517 
518  private function __read()
519  {
520  if($this->getPobjectId())
521  {
522  $statement = $this->db->prepare('SELECT * FROM payment_objects WHERE pobject_id = ?',
523  array('integer'));
524  $result = $this->db->execute($statement, array($this->getPobjectId()));
525  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
526  {
527  $this->setRefId($row->ref_id);
528  $this->setStatus($row->status);
529  $this->setPayMethod($row->pay_method);
530  $this->setVendorId($row->vendor_id);
531  $this->setTopicId($row->pt_topic_fk);
532 
533  return true;
534  }
535  }
536 
537  return false;
538  }
539 } // END class.ilPaymentObject
540 ?>