ILIAS  Release_4_2_x_branch Revision 61807
 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 ilPaymentCouponGUI($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 
31  $this->setSection(6);
32 
34 
35  $ilTabs->setTabActive('paya_header');
36  $ilTabs->setSubTabActive('paya_coupons_coupons');
37  }
38 
39  public function executeCommand()
40  {
41  global $tree;
42 
43  $cmd = $this->ctrl->getCmd();
44  switch ($this->ctrl->getNextClass($this))
45  {
46  default:
47  if (!$cmd = $this->ctrl->getCmd())
48  {
49  $cmd = 'showCoupons';
50  }
51  $this->prepareOutput();
52  $this->$cmd();
53  break;
54  }
55  }
56 
57  public function resetFilter()
58  {
59  unset($_SESSION["pay_coupons"]);
60  unset($_POST["title_type"]);
61  unset($_POST["title_value"]);
62  unset($_POST["coupon_type"]);
63  unset($_POST["updateView"]);
64  unset($_POST['from_check']);
65  unset($_POST['until_check']);
66 
67  $this->showCoupons();
68 
69  return true;
70  }
71 
72  public function showCoupons()
73  {
74  global $ilToolbar;
75 
76  include_once("Services/User/classes/class.ilObjUser.php");
77 
78  $ilToolbar->addButton($this->lng->txt('paya_coupons_add'), $this->ctrl->getLinkTarget($this,'addCoupon'));
79  if(!$_POST['show_filter'] && $_POST['updateView'] == '1')
80  {
81  $this->resetFilter();
82  }
83  else
84  if ($_POST['updateView'] == 1)
85  {
86  $_SESSION['pay_coupons']['show_filter'] = $_POST['show_filter'];
87  $_SESSION['pay_coupons']['updateView'] = true;
88  $_SESSION['pay_coupons']['until_check'] = $_POST['until_check'];
89  $_SESSION['pay_coupons']['from_check'] = $_POST['from_check'];
90  $_SESSION['pay_coupons']['title_type'] = isset($_POST['title_type']) ? $_POST['title_type'] : '' ;
91  $_SESSION['pay_coupons']['title_value'] = isset($_POST['title_value']) ? $_POST['title_value'] : '';
92 
93  if($_SESSION['pay_coupons']['from_check'] == '1')
94  {
95  $_SESSION['pay_coupons']['from']['date']['d'] = $_POST['from']['date']['d'];
96  $_SESSION['pay_coupons']['from']['date']['m'] = $_POST['from']['date']['m'];
97  $_SESSION['pay_coupons']['from']['date']['y'] = $_POST['from']['date']['y'];
98  }
99  else
100  {
101  $_SESSION['pay_coupons']['from']['date']['d'] = '';
102  $_SESSION['pay_coupons']['from']['date']['m'] = '';
103  $_SESSION['pay_coupons']['from']['date']['y'] = '';
104  }
105 
106  if($_SESSION['pay_coupons']['until_check']== '1')
107  {
108  $_SESSION['pay_coupons']['til']['date']['d'] = $_POST['til']['date']['d'];
109  $_SESSION['pay_coupons']['til']['date']['m'] = $_POST['til']['date']['m'];
110  $_SESSION['pay_coupons']['til']['date']['y'] = $_POST['til']['date']['y'];
111  }
112  else
113  {
114  $_SESSION['pay_coupons']['til']['date']['d'] = '';
115  $_SESSION['pay_coupons']['til']['date']['m'] = '';
116  $_SESSION['pay_coupons']['til']['date']['y'] = '';
117  }
118 
119  $_SESSION['pay_coupons']['coupon_type'] = $_POST['coupon_type'];
120 
121  }
122 
123  $this->coupon_obj->setSearchTitleType(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_type']));
124  $this->coupon_obj->setSearchTitleValue(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_value']));
125  $this->coupon_obj->setSearchType(ilUtil::stripSlashes($_SESSION['pay_coupons']['coupon_type']));
126 
127  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.main_view.html', 'Services/Payment');
128 
129  $filter_form = new ilPropertyFormGUI();
130  $filter_form->setFormAction($this->ctrl->getFormAction($this));
131  $filter_form->setTitle($this->lng->txt('pay_filter'));
132  $filter_form->setId('formular');
133  $filter_form->setTableWidth('100 %');
134 
135  $o_hide_check = new ilCheckBoxInputGUI($this->lng->txt('show_filter'),'show_filter');
136  $o_hide_check->setValue(1);
137  $o_hide_check->setChecked($_SESSION['pay_coupons']['show_filter'] ? 1 : 0);
138 
139  $o_hidden = new ilHiddenInputGUI('updateView');
140  $o_hidden->setValue(1);
141  $o_hidden->setPostVar('updateView');
142  $o_hide_check->addSubItem($o_hidden);
143 
144  // Title type
145  $o_title_type = new ilSelectInputGUI();
146  $title_option = array($this->lng->txt('pay_starting'),$this->lng->txt('pay_ending'));
147  $title_value = array('0','1');
148  $o_title_type->setTitle($this->lng->txt('title'));
149  $o_title_type->setOptions($title_option);
150  $o_title_type->setValue($_SESSION['pay_coupons']['title_type']);
151  $o_title_type->setPostVar('title_type');
152  $o_hide_check->addSubItem($o_title_type);
153 
154  // Title value
155  $o_title_val = new ilTextInputGUI();
156  $o_title_val->setValue($_SESSION['pay_coupons']['title_value']);
157  $o_title_val->setPostVar('title_value');
158  $o_hide_check->addSubItem($o_title_val);
159 
160  //coupon type
161  $o_coupon_type = new ilSelectInputGUI();
162  $coupon_option = array(''=>'','fix'=>$this->lng->txt('paya_coupons_fix'),'percent'=>$this->lng->txt('paya_coupons_percentaged'));
163 
164  $o_coupon_type->setTitle($this->lng->txt('coupon_type'));
165  $o_coupon_type->setOptions($coupon_option);
166  $o_coupon_type->setValue($_SESSION['pay_coupons']['coupon_type']);
167  $o_coupon_type->setPostVar('coupon_type');
168 
169  $o_hide_check->addSubItem($o_coupon_type);
170 
171  // date from
172  $o_from_check = new ilCheckBoxInputGUI($this->lng->txt('pay_order_date_from'),'from_check');
173  $o_from_check->setValue(1);
174  $o_from_check->setChecked($_SESSION['pay_coupons']['from_check'] ? 1 : 0);
175 
176  $o_date_from = new ilDateTimeInputGUI();
177  $o_date_from->setPostVar('from');
178  $_POST['from'] = $_SESSION['pay_coupons']['from'];
179  if($_SESSION['pay_coupons']['from_check'] == '1')
180  {
181  $o_date_from->checkInput();
182  }
183 
184  $o_from_check->addSubItem($o_date_from);
185  $o_hide_check->addSubItem($o_from_check);
186 
187  // date until
188  $o_until_check = new ilCheckBoxInputGUI($this->lng->txt('pay_order_date_til'), 'until_check');
189  $o_until_check->setValue(1);
190  $o_until_check->setChecked($_SESSION['pay_coupons']['until_check'] ? 1 : 0);
191 
192  $o_date_until = new ilDateTimeInputGUI();
193  $o_date_until->setPostVar('til');
194  $_POST['til'] = $_SESSION['pay_coupons']['til'];
195  if($_SESSION['pay_coupons']['until_check'] == '1')
196  {
197  $o_date_until->checkInput();
198  }
199 
200  $o_until_check->addSubItem($o_date_until);
201  $o_hide_check->addSubItem($o_until_check);
202 
203  $filter_form->addCommandButton('showCoupons', $this->lng->txt('pay_update_view'));
204  $filter_form->addCommandButton('resetFilter', $this->lng->txt('pay_reset_filter'));
205 
206  $filter_form->addItem($o_hide_check);
207 
208  $this->tpl->setVariable('FORM', $filter_form->getHTML());
209 
210  if (!count($coupons = $this->coupon_obj->getCoupons()))
211  {
212  ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
213  //if ($_POST['updateView'] == '1') ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
214  //else ilUtil::sendInfo($this->lng->txt('paya_coupons_not_available'));
215 
216  return true;
217  }
218 
219  $counter = 0;
220  foreach ($coupons as $coupon)
221  {
222  $f_result[$counter]['pc_title'] = $coupon['pc_title'];
223  $f_result[$counter]['number_of_codes'] = $coupon['number_of_codes'];
224  $f_result[$counter]['usage_of_codes'] = $coupon['usage_of_codes'];
225 
226  if (!empty($coupon['objects']))
227  {
228  $objects = "";
229  for ($i = 0; $i < count($coupon['objects']); $i++)
230  {
231  $tmp_obj = ilObjectFactory::getInstanceByRefId($coupon['objects'][$i], false);
232  if($tmp_obj)
233  {
234  $objects .= $tmp_obj->getTitle();
235  }
236  else
237  {
238  $objects .= $this->lng->txt('object_not_found');
239  }
240 
241  if ($i < count($coupon['objects']) - 1) $objects .= "<br />";
242 
243  unset($tmp_obj);
244  }
245  }
246  else
247  {
248  $objects = "";
249  }
250 
251  $f_result[$counter]['objects'] = $objects;
252  $f_result[$counter]['pc_from'] = ($coupon['pc_from'] != NULL && $coupon['pc_from_enabled'] == '1') ? $coupon['pc_from'] : '';
253  $f_result[$counter]['pc_till'] = ($coupon['pc_till'] != NULL && $coupon['pc_till_enabled'] == '1') ? $coupon['pc_till'] : '';
254  $f_result[$counter]['pc_last_changed'] = ($coupon['pc_last_changed'] != NULL ? $coupon['pc_last_changed'] : '');
255  $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']));
256  $this->ctrl->setParameter($this, 'coupon_id', $coupon['pc_pk']);
257  $f_result[$counter]['options'] = "<div class=\"il_ContainerItemCommands\"><a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "addCoupon")."\">".$this->lng->txt("edit")."</a></div>";
258 
259  ++$counter;
260  }
261 
262  return $this->__showCouponsTable($f_result);
263  }
264 
265  private function __showCouponsTable($f_result)
266  {
267  require_once 'Services/Payment/classes/class.ilShopCouponsTableGUI.php';
268  $tbl = new ilShopCouponsTableGUI($this, 'showCoupons');
269  $tbl->setTitle($this->lng->txt("paya_coupons_coupons"));
270  $tbl->setId('tbl_show_coupons');
271  $tbl->setRowTemplate("tpl.shop_coupons_row.html", "Services/Payment");
272 
273  $tbl->addColumn($this->lng->txt('paya_coupons_title'), 'pc_title', '10%');
274  $tbl->addColumn($this->lng->txt('paya_coupons_number_of_codes'), 'number_of_codes', '10%');
275  $tbl->addColumn($this->lng->txt('paya_coupons_usage_of_codes'), 'usage_of_codes', '10%');
276  $tbl->addColumn($this->lng->txt('paya_coupons_objects'), 'objects', '10%');
277  $tbl->addColumn($this->lng->txt('paya_coupons_from'), 'pc_from', '10%');
278  $tbl->addColumn($this->lng->txt('paya_coupons_till'), 'pc_till', '10%');
279  $tbl->addColumn($this->lng->txt('last_change'), 'pc_last_changed', '10%');
280  $tbl->addColumn($this->lng->txt('author'), 'pc_last_changed_author', '10%');
281  $tbl->addColumn('', 'options','10%');
282 
283  $tbl->setData($f_result);
284  $this->tpl->setVariable('TABLE', $tbl->getHTML());
285 
286  return true;
287  }
288 
289  public function saveCouponForm()
290  {
291  $this->error = '';
292 
293  if ($_POST['title'] == '') $this->error .= 'paya_coupons_title,';
294  if ($_POST['coupon_type'] == '') $this->error .= 'paya_coupons_type,';
295  if ($_POST['coupon_value'] == '') $this->error .= 'paya_coupons_value,';
296  else $_POST['coupon_value'] = ilFormat::checkDecimal($_POST['coupon_value']);
297 
298  $this->coupon_obj->setId($_GET['coupon_id']);
299  $this->coupon_obj->setCouponUser($this->user_obj->getId());
300  $this->coupon_obj->setTitle(ilUtil::stripSlashes($_POST['title']));
301  $this->coupon_obj->setDescription(ilUtil::stripSlashes($_POST['description']));
302  $this->coupon_obj->setType(ilUtil::stripSlashes($_POST['coupon_type']));
303  $this->coupon_obj->setValue(ilUtil::stripSlashes($_POST['coupon_value']));
304  $this->coupon_obj->setFromDate( date("Y-m-d",mktime(0,0,0,$_POST['from']['date']['m'],$_POST['from']['date']['d'],$_POST['from']['date']['y'])));
305  $this->coupon_obj->setTillDate( date("Y-m-d",mktime(0,0,0,$_POST['til']['date']['m'],$_POST['til']['date']['d'],$_POST['til']['date']['y'])));
306  // $this->coupon_obj->setFromDateEnabled(ilUtil::stripSlashes($_POST['from_check']));
307  //$this->coupon_obj->setTillDateEnabled(ilUtil::stripSlashes($_POST['until_check']));
308 
309  $this->coupon_obj->setFromDateEnabled($_POST['from_check']);
310  $this->coupon_obj->setTillDateEnabled($_POST['until_check']);
311  $this->coupon_obj->setUses((int)ilUtil::stripSlashes($_POST['usage']));
312  $this->coupon_obj->setChangeDate(date('Y-m-d H:i:s'));
313 
314  if ($this->error == '')
315  {
316  if ($_GET['coupon_id'] != "")
317  {
318  $this->coupon_obj->update();
319  }
320  else
321  {
322  $_GET['coupon_id'] = $this->coupon_obj->add();
323  }
324 
325  ilUtil::sendInfo($this->lng->txt('saved_successfully'));
326  }
327  else
328  {
329  if (is_array($e = explode(',', $this->error)))
330  {
331  $mandatory = '';
332  for ($i = 0; $i < count($e); $i++)
333  {
334  $e[$i] = trim($e[$i]);
335  if ($e[$i] != '')
336  {
337  $mandatory .= $this->lng->txt($e[$i]);
338  if (array_key_exists($i + 1, $e) && $e[$i + 1] != '') $mandatory .= ', ';
339  }
340  }
341  ilUtil::sendInfo($this->lng->txt('fill_out_all_required_fields') . ': ' . $mandatory);
342  }
343  }
344 
345  $this->addCoupon();
346 
347  return true;
348  }
349 
350  public function addCoupon()
351  {
352  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
353 
354  if (isset($_GET['coupon_id']))
355  {
356  if ($this->error == '') $this->coupon_obj->getCouponById($_GET['coupon_id']);
357 
358  $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
359 
360  $this->__showButtons();
361  }
362 
363 
364  $oForm = new ilPropertyFormGUI();
365  $oForm->setId('frm_add_coupon');
366  $oForm->setFormAction($this->ctrl->getFormAction($this,'saveCouponForm'));
367  $oForm->setTitle($this->coupon_obj->getId() ? $this->lng->txt('paya_coupons_edit') : $this->lng->txt('paya_coupons_add'));
368 
369  // Title
370  $oTitle = new ilTextInputGUI($this->lng->txt(paya_coupons_title),'title');
371  $oTitle->setValue($this->coupon_obj->getTitle());
372  $oTitle->setRequired(true);
373  $oForm->addItem($oTitle);
374 
375  // Description
376  $oDescription = new ilTextAreaInputGUI($this->lng->txt(paya_coupons_description),'description');
377  $oDescription->setValue($this->coupon_obj->getDescription());
378  $oForm->addItem($oDescription);
379 
380  // Type
381  $o_coupon_type = new ilSelectInputGUI();
382  $coupon_option = array('fix'=>$this->lng->txt('paya_coupons_fix'),'percent'=>$this->lng->txt('paya_coupons_percentaged'));
383 
384  $o_coupon_type->setTitle($this->lng->txt('coupon_type'));
385  $o_coupon_type->setOptions($coupon_option);
386  $o_coupon_type->setValue($this->coupon_obj->getType());
387  $o_coupon_type->setRequired(true);
388  $o_coupon_type->setPostVar('coupon_type');
389 
390  $oForm->addItem($o_coupon_type);
391 
392  // Value
393 
394  $o_coupon_value = new ilNumberInputGUI($this->lng->txt('paya_coupons_value'),'coupon_value');
395  $o_coupon_value->setSize(5);
396  $o_coupon_value->setValue($this->coupon_obj->getValue());
397 
398  $o_coupon_value->setRequired(true);
399  $oForm->addItem($o_coupon_value);
400 
401  // Date Valid From
402  $o_from_check = new ilCheckBoxInputGUI($this->lng->txt('paya_coupons_from'),'from_check');
403  $o_from_check->setValue(1);
404  $o_from_check->setChecked($this->coupon_obj->getFromDateEnabled() ? 1 : 0);
405 
406  $o_date_from = new ilDateTimeInputGUI();
407  $o_date_from->setPostVar('from');
408 
409  $from_date = explode('-', $this->coupon_obj->getFromDate());
410  $date_f['from']['date']['d'] = $from_date[2] != '00' ? $from_date[2] : '';
411  $date_f['from']['date']['m'] = $from_date[1] != '00' ? $from_date[1] : '';
412  $date_f['from']['date']['y'] = $from_date[0] != '0000' ? $from_date[0] : '';
413 
414  $_POST['from'] = $date_f['from'];
415  if($this->coupon_obj->getFromDateEnabled() == '1')
416  {
417  $o_date_from->checkInput();
418  }
419 
420  $o_from_check->addSubItem($o_date_from);
421  $oForm->addItem($o_from_check);
422 
423  // Date Valid Until
424  $o_until_check = new ilCheckBoxInputGUI($this->lng->txt('paya_coupons_till'), 'until_check');
425  $o_until_check->setValue(1);
426  $o_until_check->setChecked($this->coupon_obj->getTillDateEnabled() ? 1 : 0);
427 
428  $o_date_until = new ilDateTimeInputGUI();
429  $o_date_until->setPostVar('til');
430 
431  $till_date = explode('-', $this->coupon_obj->getTillDate());
432  $date_t['til']['date']['d']= $till_date[2] != '00' ? $till_date[2] : '';
433  $date_t['til']['date']['m'] = $till_date[1] != '00' ? $till_date[1] : '';
434  $date_t['til']['date']['y'] = $till_date[0] != '0000' ? $till_date[0] : '';
435 
436  $_POST['til'] = $date_t['til'];
437  if($this->coupon_obj->getTillDateEnabled() == '1')
438  {
439  $o_date_until->checkInput();
440  }
441 
442  $o_until_check->addSubItem($o_date_until);
443  $oForm->addItem($o_until_check);
444 
445  $o_usage = new ilNumberInputGUI($this->lng->txt('paya_coupons_availability'),'usage');
446  $o_usage->setSize(5);
447  $o_usage->setValue($this->coupon_obj->getUses());
448  $oForm->addItem($o_usage);
449 
450  $oForm->addCommandButton('saveCouponForm', $this->lng->txt('save'));
451  $oForm->addCommandButton('showCoupons', $this->lng->txt('cancel'));
452 
453  $this->tpl->setVariable('FORM',$oForm->getHTML());
454  }
455 
456  public function deleteAllCodes()
457  {
458  $this->showCodes("all");
459 
460  return true;
461  }
462 
463  public function performDeleteAllCodes()
464  {
465  $this->coupon_obj->deleteAllCodesByCouponId($_GET['coupon_id']);
466 
467  $this->showCodes();
468 
469  return true;
470  }
471 
472  public function deleteCodes()
473  {
474  $_SESSION['paya_delete_codes'] = $_POST['codes'];
475 
476  if (!is_array($_POST['codes']))
477  {
478  ilUtil::sendInfo($this->lng->txt('paya_coupons_no_codes_selected'));
479 
480  $this->showCodes();
481 
482  return true;
483  }
484 
485  $this->showCodes("selected");
486 
487  return true;
488  }
489 
490  public function performDeleteCodes()
491  {
492  if (is_array($_SESSION['paya_delete_codes']))
493  {
494  foreach($_SESSION['paya_delete_codes'] as $id)
495  {
496  $this->coupon_obj->deleteCode($id);
497  }
498  }
499  unset($_SESSION['paya_delete_codes']);
500  ilUtil::sendInfo($this->lng->txt('paya_coupons_code_deleted_successfully'));
501 
502  $this->showCodes();
503 
504  return true;
505  }
506 
507  public function cancelDelete()
508  {
509  unset($_SESSION['paya_delete_codes']);
510 
511  $this->showCodes();
512 
513  return true;
514  }
515 
516  public function showCodes($a_show_delete = "")
517  {
518  $this->coupon_obj->setId($_GET['coupon_id']);
519 
520  if (!count($codes = $this->coupon_obj->getCodesByCouponId($_GET['coupon_id'])))
521  {
522  ilUtil::sendInfo($this->lng->txt('paya_coupons_codes_not_available'));
523  $this->generateCodes();
524 
525  return true;
526  }
527 
528  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
529 
530  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
531  $this->__showButtons();
532 
533  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
534 
535  if($a_show_delete)
536  {
537  switch($a_show_delete)
538  {
539  case 'all': $del_cmd = 'performDeleteAllCodes';
540  $del_info = $this->lng->txt('paya_coupons_sure_delete_all_codes');
541  break;
542  case 'selected': $del_cmd = 'performDeleteCodes';
543  $del_info = $this->lng->txt('paya_coupons_sure_delete_selected_codes');
544  break;
545  }
546 
547  $oConfirmationGUI = new ilConfirmationGUI() ;
548  // set confirm/cancel commands
549  $oConfirmationGUI->setFormAction($this->ctrl->getFormAction($this, $del_cmd));
550  $oConfirmationGUI->setHeaderText($del_info);
551  $oConfirmationGUI->setCancel($this->lng->txt("cancel"), "cancelDelete");
552  $oConfirmationGUI->setConfirm($this->lng->txt("confirm"), $del_cmd);
553 
554  foreach ($codes as $code)
555  {
556  if(in_array($code['pcc_pk'],$_SESSION['paya_delete_codes']))
557  {
558  $oConfirmationGUI->addItem('',$code['pcc_code'], $code['pcc_code']);
559  }
560  }
561 
562  $this->tpl->setVariable('CONFIRMATION', $oConfirmationGUI->getHTML());
563  return true;
564  }
565 
566  $_SESSION['paya_delete_codes'] = $_SESSION['paya_delete_codes'] ? $_SESSION['paya_delete_codes'] : array();
567 
568  $counter = 0;
569  foreach ($codes as $code)
570  {
571  $f_result[$counter]['coupon_id'] = ilUtil::formCheckbox(in_array($code['pcc_pk'], $_SESSION['paya_delete_codes']) ? 1 : 0,
572  "codes[]", $code['pcc_pk']);
573  $f_result[$counter]['coupon_code'] = $code['pcc_code'];
574  $f_result[$counter]['usage_of_codes'] = $code['pcc_used']." ".strtolower($this->lng->txt('of'))." ".$this->coupon_obj->getUses();
575 
576  ++$counter;
577  }
578 
579  $tbl = new ilShopTableGUI($this, 'showCodes');
580  $tbl->setTitle($this->lng->txt("paya_coupons_coupons"));
581  $tbl->setId('tbl_show_codes');
582  $tbl->setRowTemplate("tpl.shop_coupons_row.html", "Services/Payment");
583 
584  $tbl->addColumn('', 'coupon_id', '1%');
585  $tbl->addColumn($this->lng->txt('paya_coupons_code'), 'coupon_code', '30%');
586  $tbl->addColumn($this->lng->txt('paya_coupons_usage_of_codes'), 'usage_of_codes', '60%');
587 
588  $tbl->setSelectAllCheckbox('codes[]');
589 
590  $tbl->addCommandButton('generateCodes',$this->lng->txt('paya_coupons_generate_codes'));
591  $tbl->addCommandButton('exportCodes',$this->lng->txt('export'));
592 
593  $tbl->addMultiCommand("deleteCodes", $this->lng->txt("delete"));
594 
595  $tbl->setData($f_result);
596  $this->tpl->setVariable('TABLE', $tbl->getHTML());
597 
598  return true;
599  }
600 
601  public function exportCodes()
602  {
603  $codes = $this->coupon_obj->getCodesByCouponId($_GET["coupon_id"]);
604 
605  if (is_array($codes))
606  {
607  include_once('./Services/Utilities/classes/class.ilCSVWriter.php');
608 
609  $csv = new ilCSVWriter();
610  $csv->setDelimiter("");
611 
612  foreach($codes as $data)
613  {
614  if ($data["pcc_code"])
615  {
616  $csv->addColumn($data["pcc_code"]);
617  $csv->addRow();
618  }
619  }
620 
621  ilUtil::deliverData($csv->getCSVString(), "code_export_".date("Ymdhis").".csv");
622  }
623 
624  $this->showCodes();
625 
626  return true;
627  }
628 
629  public function saveCodeForm()
630  {
631  if (isset($_POST["generate_length"])) $_SESSION["pay_coupons"]["code_length"] = $_POST["generate_length"];
632  else $_POST["generate_length"] = $_SESSION["pay_coupons"]["code_length"];
633 
634  if (isset($_POST["generate_type"])) $_SESSION["pay_coupons"]["code_type"] = $_POST["generate_type"];
635  else $_POST["generate_type"] = $_SESSION["pay_coupons"]["code_type"];
636 
637  if ($_POST["generate_type"] == "self")
638  {
639  if ($_GET["coupon_id"] && is_array($_POST["codes"]))
640  {
641  $count_inserts = 0;
642 
643  for ($i = 0; $i < count($_POST["codes"]); $i++)
644  {
645  $_POST["codes"][$i] = trim($_POST["codes"][$i]);
646 
647  if ($_POST["codes"][$i] != "")
648  {
649  $code = $this->__makeCode($_POST["codes"][$i], $_SESSION["pay_coupons"]["code_length"]);
650 
651  if ($code != "")
652  {
653  if ($this->coupon_obj->addCode(ilUtil::stripSlashes($code), $_GET["coupon_id"]))
654  {
655  ++$count_inserts;
656  }
657  }
658  }
659  }
660 
661  if ($count_inserts)
662  {
663  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
664  $this->showCodes();
665  }
666  else
667  {
668  ilUtil::sendInfo($this->lng->txt("paya_coupons_no_codes_generated"));
669  $this->generateCodes();
670  }
671  }
672  else if (!is_numeric($_POST["generate_number"]) || $_POST["generate_number"] <= 0)
673  {
674  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
675 
676  $this->generateCodes();
677  }
678  else
679  {
680  $this->generateCodes("input");
681  }
682  }
683  else if ($_POST["generate_type"] == "auto")
684  {
685  if ($_GET["coupon_id"] && is_numeric($_POST["generate_number"]) && $_POST["generate_number"] > 0)
686  {
687  for ($i = 0; $i < $_POST["generate_number"]; $i++)
688  {
689  $code = $this->__makeCode("", $_SESSION["pay_coupons"]["code_length"]);
690 
691  if ($code != "")
692  {
693  $this->coupon_obj->addCode($code, $_GET["coupon_id"]);
694  }
695  }
696 
697  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
698 
699  $this->showCodes();
700  }
701  else
702  {
703  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
704 
705  $this->generateCodes();
706  }
707  }
708  }
709 
710  private function __makeCode($a_code = "", $a_length = 32)
711  {
712  if ($a_code == "") $a_code = md5(uniqid(rand()));
713 
714  if (is_numeric($a_length) && strlen($a_code) > $a_length)
715  {
716  $a_code = substr($a_code, 0, $a_length);
717  }
718 
719  return $a_code;
720  }
721 
722  public function generateCodes($view = "choice")
723  {
724  $this->coupon_obj->setId($_GET['coupon_id']);
725 
726  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
727  $this->__showButtons();
728 
729  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
730 
731  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
732  $oForm_1 = new ilPropertyFormGUI();
733  $oForm_1->setId('save_frm');
734  $oForm_1->setFormAction($this->ctrl->getFormAction($this),'saveCodeForm');
735  $oForm_1->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_code_generation'));
736 
737  if ($view == "choice")
738  {
739  $oTypeRadio = new ilRadioGroupInputGUI($this->lng->txt('paya_coupons_generate_codes'), 'generate_type');
740 
741  $radio_option = new ilRadioOption($this->lng->txt('paya_coupons_type_auto'), 'auto');
742  $oTypeRadio->addOption($radio_option);
743  $radio_option = new ilRadioOption($this->lng->txt('paya_coupons_type_self'), 'self');
744  $oTypeRadio->addOption($radio_option);
745 
746  $oTypeRadio->setValue(isset($_POST["generate_type"]) ? $_POST["generate_type"] : "auto");
747  $oTypeRadio->setPostVar('generate_type');
748  $oForm_1->addItem($oTypeRadio);
749 
750  $oNumCodes = new ilNumberInputGUI($this->lng->txt("paya_coupons_number_of_codes"),'generate_number');
751  $oNumCodes->setSize(5);
752  $oNumCodes->setValue($_POST['generate_number']);
753  $oNumCodes->setRequired(true);
754  $oForm_1->addItem($oNumCodes);
755 
756  $oLength = new ilNumberInputGUI($this->lng->txt("paya_coupons_code_length"),'generate_length');
757  $oLength->setSize(5);
758  $oLength->setValue($_POST['generate_length']);
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 }
1046 ?>