ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentCouponGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
5 include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
6 include_once './Services/Payment/classes/class.ilShopTableGUI.php';
7 
9 {
10  public $ctrl;
11 
12  public $lng;
13 
14  public $user_obj = null;
15  public $coupon_obj = null;
16  public $pobject = null;
17 
18  public function __construct($user_obj)
19  {
21 
22  $this->ctrl->saveParameter($this, 'baseClass');
23  $this->user_obj = $user_obj;
24  $this->__initCouponObject();
25  }
26 
27  protected function prepareOutput()
28  {
29  global $ilTabs;
30 
32 
33  $ilTabs->setTabActive('paya_header');
34  $ilTabs->setSubTabActive('paya_coupons_coupons');
35  }
36 
37  public function executeCommand()
38  {
39  $cmd = $this->ctrl->getCmd();
40  switch ($this->ctrl->getNextClass($this))
41  {
42  default:
43  if (!$cmd = $this->ctrl->getCmd())
44  {
45  $cmd = 'showCoupons';
46  }
47  $this->prepareOutput();
48  $this->$cmd();
49  break;
50  }
51  }
52 
53  public function resetFilter()
54  {
55  unset($_SESSION["pay_coupons"]);
56  unset($_POST["title_type"]);
57  unset($_POST["title_value"]);
58  unset($_POST["coupon_type"]);
59  unset($_POST["updateView"]);
60  unset($_POST['from_check']);
61  unset($_POST['until_check']);
62 
63  $this->showCoupons();
64 
65  return true;
66  }
67 
68  public function showCoupons()
69  {
70  global $ilToolbar;
71 
72  include_once("Services/User/classes/class.ilObjUser.php");
73 
74  $ilToolbar->addButton($this->lng->txt('paya_coupons_add'), $this->ctrl->getLinkTarget($this,'addCoupon'));
75  if(!$_POST['show_filter'] && $_POST['updateView'] == '1')
76  {
77  $this->resetFilter();
78  }
79  else
80  if ($_POST['updateView'] == 1)
81  {
82  $_SESSION['pay_coupons']['show_filter'] = $_POST['show_filter'];
83  $_SESSION['pay_coupons']['updateView'] = true;
84  $_SESSION['pay_coupons']['until_check'] = $_POST['until_check'];
85  $_SESSION['pay_coupons']['from_check'] = $_POST['from_check'];
86  $_SESSION['pay_coupons']['title_type'] = isset($_POST['title_type']) ? $_POST['title_type'] : '' ;
87  $_SESSION['pay_coupons']['title_value'] = isset($_POST['title_value']) ? $_POST['title_value'] : '';
88 
89  if($_SESSION['pay_coupons']['from_check'] == '1')
90  {
91  $_SESSION['pay_coupons']['from']['date']['d'] = $_POST['from']['date']['d'];
92  $_SESSION['pay_coupons']['from']['date']['m'] = $_POST['from']['date']['m'];
93  $_SESSION['pay_coupons']['from']['date']['y'] = $_POST['from']['date']['y'];
94  }
95  else
96  {
97  $_SESSION['pay_coupons']['from']['date']['d'] = '';
98  $_SESSION['pay_coupons']['from']['date']['m'] = '';
99  $_SESSION['pay_coupons']['from']['date']['y'] = '';
100  }
101 
102  if($_SESSION['pay_coupons']['until_check']== '1')
103  {
104  $_SESSION['pay_coupons']['til']['date']['d'] = $_POST['til']['date']['d'];
105  $_SESSION['pay_coupons']['til']['date']['m'] = $_POST['til']['date']['m'];
106  $_SESSION['pay_coupons']['til']['date']['y'] = $_POST['til']['date']['y'];
107  }
108  else
109  {
110  $_SESSION['pay_coupons']['til']['date']['d'] = '';
111  $_SESSION['pay_coupons']['til']['date']['m'] = '';
112  $_SESSION['pay_coupons']['til']['date']['y'] = '';
113  }
114 
115  $_SESSION['pay_coupons']['coupon_type'] = $_POST['coupon_type'];
116 
117  }
118 
119  $this->coupon_obj->setSearchTitleType(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_type']));
120  $this->coupon_obj->setSearchTitleValue(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_value']));
121  $this->coupon_obj->setSearchType(ilUtil::stripSlashes($_SESSION['pay_coupons']['coupon_type']));
122 
123  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.main_view.html', 'Services/Payment');
124 
125  $filter_form = new ilPropertyFormGUI();
126  $filter_form->setFormAction($this->ctrl->getFormAction($this));
127  $filter_form->setTitle($this->lng->txt('pay_filter'));
128  $filter_form->setId('formular');
129  $filter_form->setTableWidth('100 %');
130 
131  $o_hide_check = new ilCheckBoxInputGUI($this->lng->txt('show_filter'),'show_filter');
132  $o_hide_check->setValue(1);
133  $o_hide_check->setChecked($_SESSION['pay_coupons']['show_filter'] ? 1 : 0);
134 
135  $o_hidden = new ilHiddenInputGUI('updateView');
136  $o_hidden->setValue(1);
137  $o_hidden->setPostVar('updateView');
138  $o_hide_check->addSubItem($o_hidden);
139 
140  // Title type
141  $o_title_type = new ilSelectInputGUI();
142  $title_option = array($this->lng->txt('pay_starting'),$this->lng->txt('pay_ending'));
143  $title_value = array('0','1');
144  $o_title_type->setTitle($this->lng->txt('title'));
145  $o_title_type->setOptions($title_option);
146  $o_title_type->setValue($_SESSION['pay_coupons']['title_type']);
147  $o_title_type->setPostVar('title_type');
148  $o_hide_check->addSubItem($o_title_type);
149 
150  // Title value
151  $o_title_val = new ilTextInputGUI();
152  $o_title_val->setValue($_SESSION['pay_coupons']['title_value']);
153  $o_title_val->setPostVar('title_value');
154  $o_hide_check->addSubItem($o_title_val);
155 
156  //coupon type
157  $o_coupon_type = new ilSelectInputGUI();
158  $coupon_option = array(''=>'','fix'=>$this->lng->txt('paya_coupons_fix'),'percent'=>$this->lng->txt('paya_coupons_percentaged'));
159 
160  $o_coupon_type->setTitle($this->lng->txt('coupon_type'));
161  $o_coupon_type->setOptions($coupon_option);
162  $o_coupon_type->setValue($_SESSION['pay_coupons']['coupon_type']);
163  $o_coupon_type->setPostVar('coupon_type');
164 
165  $o_hide_check->addSubItem($o_coupon_type);
166 
167  // date from
168  $o_from_check = new ilCheckBoxInputGUI($this->lng->txt('pay_order_date_from'),'from_check');
169  $o_from_check->setValue(1);
170  $o_from_check->setChecked($_SESSION['pay_coupons']['from_check'] ? 1 : 0);
171 
172  $o_date_from = new ilDateTimeInputGUI();
173  $o_date_from->setPostVar('from');
174  $_POST['from'] = $_SESSION['pay_coupons']['from'];
175  if($_SESSION['pay_coupons']['from_check'] == '1')
176  {
177  $o_date_from->checkInput();
178  }
179 
180  $o_from_check->addSubItem($o_date_from);
181  $o_hide_check->addSubItem($o_from_check);
182 
183  // date until
184  $o_until_check = new ilCheckBoxInputGUI($this->lng->txt('pay_order_date_til'), 'until_check');
185  $o_until_check->setValue(1);
186  $o_until_check->setChecked($_SESSION['pay_coupons']['until_check'] ? 1 : 0);
187 
188  $o_date_until = new ilDateTimeInputGUI();
189  $o_date_until->setPostVar('til');
190  $_POST['til'] = $_SESSION['pay_coupons']['til'];
191  if($_SESSION['pay_coupons']['until_check'] == '1')
192  {
193  $o_date_until->checkInput();
194  }
195 
196  $o_until_check->addSubItem($o_date_until);
197  $o_hide_check->addSubItem($o_until_check);
198 
199  $filter_form->addCommandButton('showCoupons', $this->lng->txt('pay_update_view'));
200  $filter_form->addCommandButton('resetFilter', $this->lng->txt('pay_reset_filter'));
201 
202  $filter_form->addItem($o_hide_check);
203 
204  $this->tpl->setVariable('FORM', $filter_form->getHTML());
205 
206  if (!count($coupons = $this->coupon_obj->getCoupons()))
207  {
208  ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
209  //if ($_POST['updateView'] == '1') ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
210  //else ilUtil::sendInfo($this->lng->txt('paya_coupons_not_available'));
211 
212  return true;
213  }
214 
215  $counter = 0;
216  foreach ($coupons as $coupon)
217  {
218  $f_result[$counter]['pc_title'] = $coupon['pc_title'];
219  $f_result[$counter]['number_of_codes'] = $coupon['number_of_codes'];
220  $f_result[$counter]['usage_of_codes'] = $coupon['usage_of_codes'];
221 
222  if (!empty($coupon['objects']))
223  {
224  $objects = "";
225  for ($i = 0; $i < count($coupon['objects']); $i++)
226  {
227  $tmp_obj = ilObjectFactory::getInstanceByRefId($coupon['objects'][$i], false);
228  if($tmp_obj)
229  {
230  $objects .= $tmp_obj->getTitle();
231  }
232  else
233  {
234  $objects .= $this->lng->txt('object_not_found');
235  }
236 
237  if ($i < count($coupon['objects']) - 1) $objects .= "<br />";
238 
239  unset($tmp_obj);
240  }
241  }
242  else
243  {
244  $objects = "";
245  }
246 
247  $f_result[$counter]['objects'] = $objects;
248  $f_result[$counter]['pc_from'] = ($coupon['pc_from'] != NULL && $coupon['pc_from_enabled'] == '1') ? $coupon['pc_from'] : '';
249  $f_result[$counter]['pc_till'] = ($coupon['pc_till'] != NULL && $coupon['pc_till_enabled'] == '1') ? $coupon['pc_till'] : '';
250  $f_result[$counter]['pc_last_changed'] = ($coupon['pc_last_changed'] != NULL ? $coupon['pc_last_changed'] : '');
251  $f_result[$counter]['pc_last_changed_author'] = ($coupon['pc_last_change_usr_id'] > 0 ? ilObjUser::_lookupLogin($coupon['pc_last_change_usr_id']) : ilObjUser::_lookupLogin($coupon['usr_id']));
252  $this->ctrl->setParameter($this, 'coupon_id', $coupon['pc_pk']);
253  $f_result[$counter]['options'] = "<div class=\"il_ContainerItemCommands\"><a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "addCoupon")."\">".$this->lng->txt("edit")."</a>";
254  $f_result[$counter]['options'] .= " <a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "deleteCoupon")."\">".$this->lng->txt("delete")."</a></div>";
255 
256  ++$counter;
257  }
258 
259  return $this->__showCouponsTable($f_result);
260  }
261 
262  private function __showCouponsTable($f_result)
263  {
264  require_once 'Services/Payment/classes/class.ilShopCouponsTableGUI.php';
265  $tbl = new ilShopCouponsTableGUI($this, 'showCoupons');
266  $tbl->setTitle($this->lng->txt("paya_coupons_coupons"));
267  $tbl->setId('tbl_show_coupons');
268  $tbl->setRowTemplate("tpl.shop_coupons_row.html", "Services/Payment");
269 
270  $tbl->addColumn($this->lng->txt('paya_coupons_title'), 'pc_title', '10%');
271  $tbl->addColumn($this->lng->txt('paya_coupons_number_of_codes'), 'number_of_codes', '10%');
272  $tbl->addColumn($this->lng->txt('paya_coupons_usage_of_codes'), 'usage_of_codes', '10%');
273  $tbl->addColumn($this->lng->txt('paya_coupons_objects'), 'objects', '10%');
274  $tbl->addColumn($this->lng->txt('paya_coupons_from'), 'pc_from', '10%');
275  $tbl->addColumn($this->lng->txt('paya_coupons_till'), 'pc_till', '10%');
276  $tbl->addColumn($this->lng->txt('last_change'), 'pc_last_changed', '10%');
277  $tbl->addColumn($this->lng->txt('author'), 'pc_last_changed_author', '10%');
278  $tbl->addColumn('', 'options','30%');
279 
280  $tbl->setData($f_result);
281  $this->tpl->setVariable('TABLE', $tbl->getHTML());
282 
283  return true;
284  }
285 
286  public function saveCouponForm()
287  {
288  $this->error = '';
289 
290  if ($_POST['title'] == '') $this->error .= 'paya_coupons_title,';
291  if ($_POST['coupon_type'] == '') $this->error .= 'paya_coupons_type,';
292  if ($_POST['coupon_value'] == '') $this->error .= 'paya_coupons_value,';
293  else $_POST['coupon_value'] = ilFormat::checkDecimal($_POST['coupon_value']);
294 
295  $this->coupon_obj->setId($_GET['coupon_id']);
296  $this->coupon_obj->setCouponUser($this->user_obj->getId());
297  $this->coupon_obj->setTitle(ilUtil::stripSlashes($_POST['title']));
298  $this->coupon_obj->setDescription(ilUtil::stripSlashes($_POST['description']));
299  $this->coupon_obj->setType(ilUtil::stripSlashes($_POST['coupon_type']));
300  $this->coupon_obj->setValue(ilUtil::stripSlashes($_POST['coupon_value']));
301  $this->coupon_obj->setFromDate( date("Y-m-d",mktime(0,0,0,$_POST['from']['date']['m'],$_POST['from']['date']['d'],$_POST['from']['date']['y'])));
302  $this->coupon_obj->setTillDate( date("Y-m-d",mktime(0,0,0,$_POST['til']['date']['m'],$_POST['til']['date']['d'],$_POST['til']['date']['y'])));
303  // $this->coupon_obj->setFromDateEnabled(ilUtil::stripSlashes($_POST['from_check']));
304  //$this->coupon_obj->setTillDateEnabled(ilUtil::stripSlashes($_POST['until_check']));
305 
306  $this->coupon_obj->setFromDateEnabled($_POST['from_check']);
307  $this->coupon_obj->setTillDateEnabled($_POST['until_check']);
308  $this->coupon_obj->setUses((int)ilUtil::stripSlashes($_POST['usage']));
309  $this->coupon_obj->setChangeDate(date('Y-m-d H:i:s'));
310 
311  if ($this->error == '')
312  {
313  if ($_GET['coupon_id'] != "")
314  {
315  $this->coupon_obj->update();
316  }
317  else
318  {
319  $_GET['coupon_id'] = $this->coupon_obj->add();
320  }
321 
322  ilUtil::sendInfo($this->lng->txt('saved_successfully'));
323  }
324  else
325  {
326  if (is_array($e = explode(',', $this->error)))
327  {
328  $mandatory = '';
329  for ($i = 0; $i < count($e); $i++)
330  {
331  $e[$i] = trim($e[$i]);
332  if ($e[$i] != '')
333  {
334  $mandatory .= $this->lng->txt($e[$i]);
335  if (array_key_exists($i + 1, $e) && $e[$i + 1] != '') $mandatory .= ', ';
336  }
337  }
338  ilUtil::sendInfo($this->lng->txt('fill_out_all_required_fields') . ': ' . $mandatory);
339  }
340  }
341 
342  $this->addCoupon();
343 
344  return true;
345  }
346 
347  public function addCoupon()
348  {
349  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
350 
351  if (isset($_GET['coupon_id']))
352  {
353  if ($this->error == '') $this->coupon_obj->getCouponById($_GET['coupon_id']);
354 
355  $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
356 
357  $this->__showButtons();
358  }
359 
360 
361  $oForm = new ilPropertyFormGUI();
362  $oForm->setId('frm_add_coupon');
363  $oForm->setFormAction($this->ctrl->getFormAction($this,'saveCouponForm'));
364  $oForm->setTitle($this->coupon_obj->getId() ? $this->lng->txt('paya_coupons_edit') : $this->lng->txt('paya_coupons_add'));
365 
366  // Title
367  $oTitle = new ilTextInputGUI($this->lng->txt(paya_coupons_title),'title');
368  $oTitle->setValue($this->coupon_obj->getTitle());
369  $oTitle->setRequired(true);
370  $oForm->addItem($oTitle);
371 
372  // Description
373  $oDescription = new ilTextAreaInputGUI($this->lng->txt(paya_coupons_description),'description');
374  $oDescription->setValue($this->coupon_obj->getDescription());
375  $oForm->addItem($oDescription);
376 
377  // Type
378  $o_coupon_type = new ilSelectInputGUI();
379  $coupon_option = array('fix'=>$this->lng->txt('paya_coupons_fix'),'percent'=>$this->lng->txt('paya_coupons_percentaged'));
380 
381  $o_coupon_type->setTitle($this->lng->txt('coupon_type'));
382  $o_coupon_type->setOptions($coupon_option);
383  $o_coupon_type->setValue($this->coupon_obj->getType());
384  $o_coupon_type->setRequired(true);
385  $o_coupon_type->setPostVar('coupon_type');
386 
387  $oForm->addItem($o_coupon_type);
388 
389  // Value
390 
391  $o_coupon_value = new ilNumberInputGUI($this->lng->txt('paya_coupons_value'),'coupon_value');
392  $o_coupon_value->setSize(5);
393  $o_coupon_value->allowDecimals(true);
394  $o_coupon_value->setValue($this->coupon_obj->getValue());
395 
396  $o_coupon_value->setRequired(true);
397  $oForm->addItem($o_coupon_value);
398 
399  // Date Valid From
400  $o_from_check = new ilCheckBoxInputGUI($this->lng->txt('paya_coupons_from'),'from_check');
401  $o_from_check->setValue(1);
402  $o_from_check->setChecked($this->coupon_obj->getFromDateEnabled() ? 1 : 0);
403 
404  $o_date_from = new ilDateTimeInputGUI();
405  $o_date_from->setPostVar('from');
406 
407  $from_date = explode('-', $this->coupon_obj->getFromDate());
408  $date_f['from']['date']['d'] = $from_date[2] != '00' ? $from_date[2] : '';
409  $date_f['from']['date']['m'] = $from_date[1] != '00' ? $from_date[1] : '';
410  $date_f['from']['date']['y'] = $from_date[0] != '0000' ? $from_date[0] : '';
411 
412  $_POST['from'] = $date_f['from'];
413  if($this->coupon_obj->getFromDateEnabled() == '1')
414  {
415  $o_date_from->checkInput();
416  }
417 
418  $o_from_check->addSubItem($o_date_from);
419  $oForm->addItem($o_from_check);
420 
421  // Date Valid Until
422  $o_until_check = new ilCheckBoxInputGUI($this->lng->txt('paya_coupons_till'), 'until_check');
423  $o_until_check->setValue(1);
424  $o_until_check->setChecked($this->coupon_obj->getTillDateEnabled() ? 1 : 0);
425 
426  $o_date_until = new ilDateTimeInputGUI();
427  $o_date_until->setPostVar('til');
428 
429  $till_date = explode('-', $this->coupon_obj->getTillDate());
430  $date_t['til']['date']['d']= $till_date[2] != '00' ? $till_date[2] : '';
431  $date_t['til']['date']['m'] = $till_date[1] != '00' ? $till_date[1] : '';
432  $date_t['til']['date']['y'] = $till_date[0] != '0000' ? $till_date[0] : '';
433 
434  $_POST['til'] = $date_t['til'];
435  if($this->coupon_obj->getTillDateEnabled() == '1')
436  {
437  $o_date_until->checkInput();
438  }
439 
440  $o_until_check->addSubItem($o_date_until);
441  $oForm->addItem($o_until_check);
442 
443  $o_usage = new ilNumberInputGUI($this->lng->txt('paya_coupons_availability'),'usage');
444  $o_usage->setSize(5);
445  $o_usage->setValue($this->coupon_obj->getUses());
446  $oForm->addItem($o_usage);
447 
448  $oForm->addCommandButton('saveCouponForm', $this->lng->txt('save'));
449  $oForm->addCommandButton('showCoupons', $this->lng->txt('cancel'));
450 
451  $this->tpl->setVariable('FORM',$oForm->getHTML());
452  }
453 
454  public function deleteAllCodes()
455  {
456  $this->showCodes("all");
457 
458  return true;
459  }
460 
461  public function performDeleteAllCodes()
462  {
463  $this->coupon_obj->deleteAllCodesByCouponId($_GET['coupon_id']);
464 
465  $this->showCodes();
466 
467  return true;
468  }
469 
470  public function deleteCodes()
471  {
472  $_SESSION['paya_delete_codes'] = $_POST['codes'];
473 
474  if (!is_array($_POST['codes']))
475  {
476  ilUtil::sendInfo($this->lng->txt('paya_coupons_no_codes_selected'));
477 
478  $this->showCodes();
479 
480  return true;
481  }
482 
483  $this->showCodes("selected");
484 
485  return true;
486  }
487 
488  public function performDeleteCodes()
489  {
490  if (is_array($_SESSION['paya_delete_codes']))
491  {
492  foreach($_SESSION['paya_delete_codes'] as $id)
493  {
494  $this->coupon_obj->deleteCode($id);
495  }
496  }
497  unset($_SESSION['paya_delete_codes']);
498  ilUtil::sendInfo($this->lng->txt('paya_coupons_code_deleted_successfully'));
499 
500  $this->showCodes();
501 
502  return true;
503  }
504 
505  public function cancelDelete()
506  {
507  unset($_SESSION['paya_delete_codes']);
508 
509  $this->showCodes();
510 
511  return true;
512  }
513 
514  public function showCodes($a_show_delete = "")
515  {
516  $this->coupon_obj->setId($_GET['coupon_id']);
517 
518  if (!count($codes = $this->coupon_obj->getCodesByCouponId($_GET['coupon_id'])))
519  {
520  ilUtil::sendInfo($this->lng->txt('paya_coupons_codes_not_available'));
521  $this->generateCodes();
522 
523  return true;
524  }
525 
526  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
527 
528  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
529  $this->__showButtons();
530 
531  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
532 
533  if($a_show_delete)
534  {
535  switch($a_show_delete)
536  {
537  case 'all': $del_cmd = 'performDeleteAllCodes';
538  $del_info = $this->lng->txt('paya_coupons_sure_delete_all_codes');
539  break;
540  case 'selected': $del_cmd = 'performDeleteCodes';
541  $del_info = $this->lng->txt('paya_coupons_sure_delete_selected_codes');
542  break;
543  }
544 
545  $oConfirmationGUI = new ilConfirmationGUI() ;
546  // set confirm/cancel commands
547  $oConfirmationGUI->setFormAction($this->ctrl->getFormAction($this, $del_cmd));
548  $oConfirmationGUI->setHeaderText($del_info);
549  $oConfirmationGUI->setCancel($this->lng->txt("cancel"), "cancelDelete");
550  $oConfirmationGUI->setConfirm($this->lng->txt("confirm"), $del_cmd);
551 
552  foreach ($codes as $code)
553  {
554  if(in_array($code['pcc_pk'],$_SESSION['paya_delete_codes']))
555  {
556  $oConfirmationGUI->addItem('',$code['pcc_code'], $code['pcc_code']);
557  }
558  }
559 
560  $this->tpl->setVariable('CONFIRMATION', $oConfirmationGUI->getHTML());
561  return true;
562  }
563 
564  $_SESSION['paya_delete_codes'] = $_SESSION['paya_delete_codes'] ? $_SESSION['paya_delete_codes'] : array();
565 
566  $counter = 0;
567  foreach ($codes as $code)
568  {
569  $f_result[$counter]['coupon_id'] = ilUtil::formCheckbox(in_array($code['pcc_pk'], $_SESSION['paya_delete_codes']) ? 1 : 0,
570  "codes[]", $code['pcc_pk']);
571  $f_result[$counter]['coupon_code'] = $code['pcc_code'];
572  $f_result[$counter]['usage_of_codes'] = $code['pcc_used']." ".strtolower($this->lng->txt('of'))." ".$this->coupon_obj->getUses();
573 
574  ++$counter;
575  }
576 
577  $tbl = new ilShopTableGUI($this, 'showCodes');
578  $tbl->setTitle($this->lng->txt("paya_coupons_coupons"));
579  $tbl->setId('tbl_show_codes');
580  $tbl->setRowTemplate("tpl.shop_coupons_row.html", "Services/Payment");
581 
582  $tbl->addColumn('', 'coupon_id', '1%');
583  $tbl->addColumn($this->lng->txt('paya_coupons_code'), 'coupon_code', '30%');
584  $tbl->addColumn($this->lng->txt('paya_coupons_usage_of_codes'), 'usage_of_codes', '60%');
585 
586  $tbl->setSelectAllCheckbox('codes[]');
587 
588  $tbl->addCommandButton('generateCodes',$this->lng->txt('paya_coupons_generate_codes'));
589  $tbl->addCommandButton('exportCodes',$this->lng->txt('export'));
590 
591  $tbl->addMultiCommand("deleteCodes", $this->lng->txt("delete"));
592 
593  $tbl->setData($f_result);
594  $this->tpl->setVariable('TABLE', $tbl->getHTML());
595 
596  return true;
597  }
598 
599  public function exportCodes()
600  {
601  $codes = $this->coupon_obj->getCodesByCouponId($_GET["coupon_id"]);
602 
603  if (is_array($codes))
604  {
605  include_once('./Services/Utilities/classes/class.ilCSVWriter.php');
606 
607  $csv = new ilCSVWriter();
608  $csv->setDelimiter("");
609 
610  foreach($codes as $data)
611  {
612  if ($data["pcc_code"])
613  {
614  $csv->addColumn($data["pcc_code"]);
615  $csv->addRow();
616  }
617  }
618 
619  ilUtil::deliverData($csv->getCSVString(), "code_export_".date("Ymdhis").".csv");
620  }
621 
622  $this->showCodes();
623 
624  return true;
625  }
626 
627  public function saveCodeForm()
628  {
629  if (isset($_POST["generate_length"])) $_SESSION["pay_coupons"]["code_length"] = $_POST["generate_length"];
630  else $_POST["generate_length"] = $_SESSION["pay_coupons"]["code_length"];
631 
632  if (isset($_POST["generate_type"])) $_SESSION["pay_coupons"]["code_type"] = $_POST["generate_type"];
633  else $_POST["generate_type"] = $_SESSION["pay_coupons"]["code_type"];
634 
635  if ($_POST["generate_type"] == "self")
636  {
637  if ($_GET["coupon_id"] && is_array($_POST["codes"]))
638  {
639  $count_inserts = 0;
640 
641  for ($i = 0; $i < count($_POST["codes"]); $i++)
642  {
643  $_POST["codes"][$i] = trim($_POST["codes"][$i]);
644 
645  if ($_POST["codes"][$i] != "")
646  {
647  $code = $this->__makeCode($_POST["codes"][$i], $_SESSION["pay_coupons"]["code_length"]);
648 
649  if ($code != "")
650  {
651  if ($this->coupon_obj->addCode(ilUtil::stripSlashes($code), $_GET["coupon_id"]))
652  {
653  ++$count_inserts;
654  }
655  }
656  }
657  }
658 
659  if ($count_inserts)
660  {
661  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
662  $this->showCodes();
663  }
664  else
665  {
666  ilUtil::sendInfo($this->lng->txt("paya_coupons_no_codes_generated"));
667  $this->generateCodes();
668  }
669  }
670  else if ((!is_numeric($_POST["generate_number"]) || $_POST["generate_number"] <= 0)
671  || (!is_numeric($_POST["generate_length"]) || $_POST["generate_length"] <= 0))
672  {
673  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
674 
675  $this->generateCodes();
676  }
677  else
678  {
679  $this->generateCodes("input");
680  }
681  }
682  else if ($_POST["generate_type"] == "auto")
683  {
684  if ($_GET["coupon_id"] && is_numeric($_POST["generate_number"]) && $_POST["generate_number"] > 0)
685  {
686  for ($i = 0; $i < $_POST["generate_number"]; $i++)
687  {
688  $code = $this->__makeCode("", $_SESSION["pay_coupons"]["code_length"]);
689 
690  if ($code != "")
691  {
692  $this->coupon_obj->addCode($code, $_GET["coupon_id"]);
693  }
694  }
695 
696  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
697 
698  $this->showCodes();
699  }
700  else
701  {
702  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
703 
704  $this->generateCodes();
705  }
706  }
707  }
708 
709  private function __makeCode($a_code = "", $a_length = 32)
710  {
711  if ($a_code == "") $a_code = md5(uniqid(rand()));
712 
713  if (is_numeric($a_length) && strlen($a_code) > $a_length)
714  {
715  $a_code = substr($a_code, 0, $a_length);
716  }
717 
718  return $a_code;
719  }
720 
721  public function generateCodes($view = "choice")
722  {
723  $this->coupon_obj->setId($_GET['coupon_id']);
724 
725  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
726  $this->__showButtons();
727 
728  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
729 
730  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
731  $oForm_1 = new ilPropertyFormGUI();
732  $oForm_1->setId('save_frm');
733  $oForm_1->setFormAction($this->ctrl->getFormAction($this),'saveCodeForm');
734  $oForm_1->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_code_generation'));
735 
736  if ($view == "choice")
737  {
738  $oTypeRadio = new ilRadioGroupInputGUI($this->lng->txt('paya_coupons_generate_codes'), 'generate_type');
739 
740  $radio_option = new ilRadioOption($this->lng->txt('paya_coupons_type_auto'), 'auto');
741  $oTypeRadio->addOption($radio_option);
742  $radio_option = new ilRadioOption($this->lng->txt('paya_coupons_type_self'), 'self');
743  $oTypeRadio->addOption($radio_option);
744 
745  $oTypeRadio->setValue(isset($_POST["generate_type"]) ? $_POST["generate_type"] : "auto");
746  $oTypeRadio->setPostVar('generate_type');
747  $oForm_1->addItem($oTypeRadio);
748 
749  $oNumCodes = new ilNumberInputGUI($this->lng->txt("paya_coupons_number_of_codes"),'generate_number');
750  $oNumCodes->setSize(5);
751  $oNumCodes->setValue($_POST['generate_number']);
752  $oNumCodes->setRequired(true);
753  $oForm_1->addItem($oNumCodes);
754 
755  $oLength = new ilNumberInputGUI($this->lng->txt("paya_coupons_code_length"),'generate_length');
756  $oLength->setSize(5);
757  $oLength->setValue($_POST['generate_length']);
758  $oLength->setRequired(true);
759  $oLength->setInfo($this->lng->txt('paya_coupons_type_self'));
760  $oForm_1->addItem($oLength);
761 
762  $oForm_1->addCommandButton('saveCodeForm',$this->lng->txt('save'));
763 
764  $this->tpl->setVariable('FORM', $oForm_1->getHTML());
765 
766  $oForm_2 = new ilPropertyformGUI();
767  $oForm_2->setId('import_frm');
768  $oForm_2->setFormAction($this->ctrl->getFormAction($this), 'showCodeImport');
769  $oForm_2->addCommandButton('showCodeImport',$this->lng->txt('import'));
770  $this->tpl->setVariable('FORM_2', $oForm_2->getHTML());
771 
772  }
773  else if ($view == "input")
774  {
775  if (is_numeric($_POST['generate_number']) && $_POST['generate_number'] > 0)
776  {
777  for ($i = 0; $i < $_POST['generate_number']; $i++)
778  {
779  $index = $i +1;
780  $oLoopCode = new ilTextInputGUI('#'.$index,'codes['.$i.']');
781  $oForm_1->addItem($oLoopCode);
782  }
783  $oForm_1->addCommandButton('saveCodeForm',$this->lng->txt('save'));
784  }
785 
786  $this->tpl->setVariable('FORM',$oForm_1->getHTML());
787 
788  $oLoopCode = new ilTextInputGUI();
789  }
790 
791  return true;
792  }
793 
794  public function assignObjects()
795  {
796  if (is_array($_POST['object_id']))
797  {
798  $this->coupon_obj->setId($_GET["coupon_id"]);
799  foreach($_POST['object_id'] as $id)
800  {
801  if ($id)
802  {
803  $this->coupon_obj->assignObjectToCoupon($id);
804  }
805  }
806  }
807 
808  $this->showObjects();
809 
810  return true;
811  }
812 
813  public function unassignObjects()
814  {
815  if (is_array($_POST['object_id']))
816  {
817  $this->coupon_obj->setId($_GET["coupon_id"]);
818  foreach($_POST['object_id'] as $id)
819  {
820  if ($id)
821  {
822  $this->coupon_obj->unassignObjectFromCoupon($id);
823  }
824  }
825  }
826 
827  $this->showObjects();
828 
829  return true;
830  }
831 
832  public function showObjects()
833  {
834  $this->coupon_obj->setId($_GET['coupon_id']);
835 
836  $this->__initPaymentObject();
837 
838  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
839  $this->__showButtons();
840 
841  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
842 
843  $objects = $this->pobject->_getObjectsData($this->user_obj->getId());
844 
845  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
846 
847  $counter_assigned = 0;
848  $counter_unassigned = 0;
849  $f_result_assigned = array();
850  $f_result_unassigned = array();
851  foreach($objects as $data)
852  {
853  if ($this->coupon_obj->isObjectAssignedToCoupon($data['ref_id']))
854  {
855  $p_counter =& $counter_assigned;
856  $p_result =& $f_result_assigned;
857  }
858  else
859  {
860  $p_counter =& $counter_unassigned;
861  $p_result =& $f_result_unassigned;
862  }
863 
864  $tmp_obj = ilObjectFactory::getInstanceByRefId($data['ref_id'], false);
865  if($tmp_obj)
866  {
867  $p_result[$p_counter]['object_id'] = ilUtil::formCheckbox(0, 'object_id[]', $data['ref_id']);
868  $p_result[$p_counter]['title'] = $tmp_obj->getTitle();
869 
870  switch($data['status'])
871  {
872  case $this->pobject->STATUS_BUYABLE:
873  $p_result[$p_counter]['status'] = $this->lng->txt('paya_buyable');
874  break;
875 
876  case $this->pobject->STATUS_NOT_BUYABLE:
877  $p_result[$p_counter]['status'] = $this->lng->txt('paya_not_buyable');
878  break;
879 
880  case $this->pobject->STATUS_EXPIRES:
881  $p_result[$p_counter]['status'] = $this->lng->txt('paya_expires');
882  break;
883  }
884  include_once './Services/Payment/classes/class.ilPayMethods.php';
885  $p_result[$p_counter]['pay_method'] = ilPaymethods::getStringByPaymethod($data['pay_method']);
886  }
887  else
888  {
889  $p_result[$p_counter]['object_id'] = '';
890  $p_result[$p_counter]['title'] = $this->lng->txt('object_not_found');
891  $p_result[$p_counter]['status'] = '';
892  $p_result[$p_counter]['pay_method'] ='';
893  }
894  ++$p_counter;
895 
896  unset($tmp_obj);
897  }
898 
899  $this->ctrl->setParameter($this, "cmd", "showObjects");
900 
901  if (count($f_result_assigned) > 0)
902  {
903  $tbl = new ilShopTableGUI($this);
904  $tbl->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_assigned_objects"));
905  $tbl->setId('tbl_show_assigned');
906  $tbl->setPrefix('assigned');
907  $tbl->setRowTemplate("tpl.shop_objects_row.html", "Services/Payment");
908 
909  $tbl->addColumn('', 'object_id', '1%');
910  $tbl->addColumn($this->lng->txt('title'),'title', '10%');
911  $tbl->addColumn($this->lng->txt('status'), 'status', '30%');
912  $tbl->addColumn($this->lng->txt('paya_pay_method'), 'paya_pay_method', '60%');
913 
914  $tbl->setSelectAllCheckbox('object_id');
915 
916  $tbl->addMultiCommand('unassignObjects',$this->lng->txt('remove'));
917 
918 
919  $tbl->setData($f_result_assigned);
920 
921  $this->tpl->setVariable('TABLE', $tbl->getHTML());
922  }
923 
924  if (count($f_result_unassigned) > 0)
925  {
926  $tbl_2 = new ilShopTableGUI($this);
927  $tbl_2->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_unassigned_objects"));
928  $tbl_2->setId('tbl_show_unassigned');
929  $tbl_2->setPrefix('unassigned');
930  $tbl_2->setRowTemplate("tpl.shop_objects_row.html", "Services/Payment");
931 
932  $tbl_2->addColumn('', 'object_id', '1%');
933  $tbl_2->addColumn($this->lng->txt('title'),'title', '10%');
934  $tbl_2->addColumn($this->lng->txt('status'), 'status', '30%');
935  $tbl_2->addColumn($this->lng->txt('paya_pay_method'), 'pay_method', '60%');
936 
937  $tbl_2->setSelectAllCheckbox('object_id');
938 
939  $tbl_2->addMultiCommand('assignObjects',$this->lng->txt('add'));
940 
941 
942  $tbl_2->setData($f_result_unassigned);
943 
944  $this->tpl->setVariable('TABLE_2', $tbl_2->getHTML());
945  }
946 
947  return true;
948  }
949 
950  public function importCodes()
951  {
952  include_once('./Services/Utilities/classes/class.ilCSVReader.php');
953 
954  if ($_FILES["codesfile"]["tmp_name"] != "")
955  {
956  $csv = new ilCSVReader();
957  $csv->setDelimiter("");
958 
959  if ($csv->open($_FILES["codesfile"]["tmp_name"]))
960  {
961  $data = $csv->getDataArrayFromCSVFile();
962 
963  for ($i = 0; $i < count($data); $i++)
964  {
965  for ($j = 0; $j < count($data[$i]); $j++)
966  {
967  $this->coupon_obj->addCode(ilUtil::stripSlashes($data[$i][$j]), $_GET["coupon_id"]);
968  }
969  }
970 
971  ilUtil::sendInfo($this->lng->txt("paya_coupon_codes_import_successful"));
972 
973  $this->showCodes();
974  }
975  else
976  {
977  ilUtil::sendInfo($this->lng->txt("paya_coupons_import_error_opening_file"));
978 
979  $this->showCodeImport();
980  }
981  }
982  else
983  {
984  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
985 
986  $this->showCodeImport();
987  }
988 
989  return true;
990  }
991 
992  public function showCodeImport()
993  {
994  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
995  $this->__showButtons();
996  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
997 
998  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
999  $oForm = new ilPropertyFormGUI();
1000  $oForm->setId('coup_form');
1001  $oForm->setFormAction($this->ctrl->getFormAction($this), 'importCodes');
1002  $oForm->setTitle( $this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_codes_import'));
1003 
1004  $oFile = new ilFileInputGUI($this->lng->txt('file'),'codesfile');
1005  $oFile->setSuffixes(array("txt"));
1006  $oFile->setRequired(true);
1007  $oFile->setInfo($this->lng->txt('import_use_only_textfile'));
1008  $oForm->addItem($oFile);
1009 
1010  $oForm->addCommandButton('importCodes',$this->lng->txt('upload'));
1011 
1012  $this->tpl->setVariable('FORM', $oForm->getHTML());
1013 
1014  return true;
1015  }
1016 
1017  private function __showButtons()
1018  {
1019  global $ilToolbar;
1020 
1021  $ilToolbar->addButton($this->lng->txt('paya_coupons_edit'), $this->ctrl->getLinkTarget($this, 'addCoupon'));
1022  $ilToolbar->addButton($this->lng->txt('paya_coupons_edit_codes'), $this->ctrl->getLinkTarget($this, 'showCodes'));
1023  $ilToolbar->addButton($this->lng->txt('paya_coupons_edit_objects'), $this->ctrl->getLinkTarget($this, 'showObjects'));
1024 
1025  return true;
1026  }
1027 
1028  private function __initPaymentObject($a_pobject_id = 0)
1029  {
1030  include_once './Services/Payment/classes/class.ilPaymentObject.php';
1031 
1032  $this->pobject = new ilPaymentObject($this->user_obj, $a_pobject_id);
1033 
1034  return true;
1035  }
1036 
1037  private function __initCouponObject()
1038  {
1039  include_once './Services/Payment/classes/class.ilPaymentCoupons.php';
1040 
1041  $this->coupon_obj = new ilPaymentCoupons($this->user_obj, true);
1042 
1043  return true;
1044  }
1045  public function deleteCoupon()
1046  {
1047  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
1048 
1049  if (!isset($_GET['coupon_id']))
1050  {
1051  return ilUtil::sendFailure($this->lng->txt('no_coupon_selected'));
1052  }
1053 
1054 # $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
1055 
1056  $this->__showButtons();
1057 
1058  ilUtil::sendQuestion($this->lng->txt('paya_coupons_sure_delete_selected_codes'));
1059  $oConfirmationGUI = new ilConfirmationGUI() ;
1060  // set confirm/cancel commands
1061  $oConfirmationGUI->setFormAction($this->ctrl->getFormAction($this, 'performDeleteCoupon'));
1062  $oConfirmationGUI->setHeaderText('');
1063  $oConfirmationGUI->setCancel($this->lng->txt("cancel"), "showCoupons");
1064  $oConfirmationGUI->setConfirm($this->lng->txt("confirm"), 'performDeleteCoupon');
1065  $oConfirmationGUI->addItem('','', ilPaymentCoupons::_lookupTitle($_GET['coupon_id']));
1066  $oConfirmationGUI->addHiddenItem('coupon_id', $_GET['coupon_id']);
1067  $this->tpl->setVariable('CONFIRMATION', $oConfirmationGUI->getHTML());
1068  return true;
1069 
1070  }
1071 
1072  public function performDeleteCoupon()
1073  {
1074  if (!isset($_POST['coupon_id']))
1075  {
1076  return ilUtil::sendFailure($this->lng->txt('coupon_id_is_missing'));
1077  }
1078  $this->coupon_obj->deleteCouponByCouponId((int)$_POST['coupon_id']);
1079 
1080  $this->showCoupons();
1081  }
1082 }
1083 ?>