ILIAS  Release_4_4_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 
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  $cmd = $this->ctrl->getCmd();
42  switch ($this->ctrl->getNextClass($this))
43  {
44  default:
45  if (!$cmd = $this->ctrl->getCmd())
46  {
47  $cmd = 'showCoupons';
48  }
49  $this->prepareOutput();
50  $this->$cmd();
51  break;
52  }
53  }
54 
55  public function resetFilter()
56  {
57  unset($_SESSION["pay_coupons"]);
58  unset($_POST["title_type"]);
59  unset($_POST["title_value"]);
60  unset($_POST["coupon_type"]);
61  unset($_POST["updateView"]);
62  unset($_POST['from_check']);
63  unset($_POST['until_check']);
64 
65  $this->showCoupons();
66 
67  return true;
68  }
69 
70  public function showCoupons()
71  {
72  global $ilToolbar;
73 
74  include_once("Services/User/classes/class.ilObjUser.php");
75 
76  $ilToolbar->addButton($this->lng->txt('paya_coupons_add'), $this->ctrl->getLinkTarget($this,'addCoupon'));
77  if(!$_POST['show_filter'] && $_POST['updateView'] == '1')
78  {
79  $this->resetFilter();
80  }
81  else
82  if ($_POST['updateView'] == 1)
83  {
84  $_SESSION['pay_coupons']['show_filter'] = $_POST['show_filter'];
85  $_SESSION['pay_coupons']['updateView'] = true;
86  $_SESSION['pay_coupons']['until_check'] = $_POST['until_check'];
87  $_SESSION['pay_coupons']['from_check'] = $_POST['from_check'];
88  $_SESSION['pay_coupons']['title_type'] = isset($_POST['title_type']) ? $_POST['title_type'] : '' ;
89  $_SESSION['pay_coupons']['title_value'] = isset($_POST['title_value']) ? $_POST['title_value'] : '';
90 
91  if($_SESSION['pay_coupons']['from_check'] == '1')
92  {
93  $_SESSION['pay_coupons']['from']['date']['d'] = $_POST['from']['date']['d'];
94  $_SESSION['pay_coupons']['from']['date']['m'] = $_POST['from']['date']['m'];
95  $_SESSION['pay_coupons']['from']['date']['y'] = $_POST['from']['date']['y'];
96  }
97  else
98  {
99  $_SESSION['pay_coupons']['from']['date']['d'] = '';
100  $_SESSION['pay_coupons']['from']['date']['m'] = '';
101  $_SESSION['pay_coupons']['from']['date']['y'] = '';
102  }
103 
104  if($_SESSION['pay_coupons']['until_check']== '1')
105  {
106  $_SESSION['pay_coupons']['til']['date']['d'] = $_POST['til']['date']['d'];
107  $_SESSION['pay_coupons']['til']['date']['m'] = $_POST['til']['date']['m'];
108  $_SESSION['pay_coupons']['til']['date']['y'] = $_POST['til']['date']['y'];
109  }
110  else
111  {
112  $_SESSION['pay_coupons']['til']['date']['d'] = '';
113  $_SESSION['pay_coupons']['til']['date']['m'] = '';
114  $_SESSION['pay_coupons']['til']['date']['y'] = '';
115  }
116 
117  $_SESSION['pay_coupons']['coupon_type'] = $_POST['coupon_type'];
118 
119  }
120 
121  $this->coupon_obj->setSearchTitleType(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_type']));
122  $this->coupon_obj->setSearchTitleValue(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_value']));
123  $this->coupon_obj->setSearchType(ilUtil::stripSlashes($_SESSION['pay_coupons']['coupon_type']));
124 
125  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.main_view.html', 'Services/Payment');
126 
127  $filter_form = new ilPropertyFormGUI();
128  $filter_form->setFormAction($this->ctrl->getFormAction($this));
129  $filter_form->setTitle($this->lng->txt('pay_filter'));
130  $filter_form->setId('formular');
131  $filter_form->setTableWidth('100 %');
132 
133  $o_hide_check = new ilCheckBoxInputGUI($this->lng->txt('show_filter'),'show_filter');
134  $o_hide_check->setValue(1);
135  $o_hide_check->setChecked($_SESSION['pay_coupons']['show_filter'] ? 1 : 0);
136 
137  $o_hidden = new ilHiddenInputGUI('updateView');
138  $o_hidden->setValue(1);
139  $o_hidden->setPostVar('updateView');
140  $o_hide_check->addSubItem($o_hidden);
141 
142  // Title type
143  $o_title_type = new ilSelectInputGUI();
144  $title_option = array($this->lng->txt('pay_starting'),$this->lng->txt('pay_ending'));
145  $title_value = array('0','1');
146  $o_title_type->setTitle($this->lng->txt('title'));
147  $o_title_type->setOptions($title_option);
148  $o_title_type->setValue($_SESSION['pay_coupons']['title_type']);
149  $o_title_type->setPostVar('title_type');
150  $o_hide_check->addSubItem($o_title_type);
151 
152  // Title value
153  $o_title_val = new ilTextInputGUI();
154  $o_title_val->setValue($_SESSION['pay_coupons']['title_value']);
155  $o_title_val->setPostVar('title_value');
156  $o_hide_check->addSubItem($o_title_val);
157 
158  //coupon type
159  $o_coupon_type = new ilSelectInputGUI();
160  $coupon_option = array(''=>'','fix'=>$this->lng->txt('paya_coupons_fix'),'percent'=>$this->lng->txt('paya_coupons_percentaged'));
161 
162  $o_coupon_type->setTitle($this->lng->txt('coupon_type'));
163  $o_coupon_type->setOptions($coupon_option);
164  $o_coupon_type->setValue($_SESSION['pay_coupons']['coupon_type']);
165  $o_coupon_type->setPostVar('coupon_type');
166 
167  $o_hide_check->addSubItem($o_coupon_type);
168 
169  // date from
170  $o_from_check = new ilCheckBoxInputGUI($this->lng->txt('pay_order_date_from'),'from_check');
171  $o_from_check->setValue(1);
172  $o_from_check->setChecked($_SESSION['pay_coupons']['from_check'] ? 1 : 0);
173 
174  $o_date_from = new ilDateTimeInputGUI();
175  $o_date_from->setPostVar('from');
176  $_POST['from'] = $_SESSION['pay_coupons']['from'];
177  if($_SESSION['pay_coupons']['from_check'] == '1')
178  {
179  $o_date_from->checkInput();
180  }
181 
182  $o_from_check->addSubItem($o_date_from);
183  $o_hide_check->addSubItem($o_from_check);
184 
185  // date until
186  $o_until_check = new ilCheckBoxInputGUI($this->lng->txt('pay_order_date_til'), 'until_check');
187  $o_until_check->setValue(1);
188  $o_until_check->setChecked($_SESSION['pay_coupons']['until_check'] ? 1 : 0);
189 
190  $o_date_until = new ilDateTimeInputGUI();
191  $o_date_until->setPostVar('til');
192  $_POST['til'] = $_SESSION['pay_coupons']['til'];
193  if($_SESSION['pay_coupons']['until_check'] == '1')
194  {
195  $o_date_until->checkInput();
196  }
197 
198  $o_until_check->addSubItem($o_date_until);
199  $o_hide_check->addSubItem($o_until_check);
200 
201  $filter_form->addCommandButton('showCoupons', $this->lng->txt('pay_update_view'));
202  $filter_form->addCommandButton('resetFilter', $this->lng->txt('pay_reset_filter'));
203 
204  $filter_form->addItem($o_hide_check);
205 
206  $this->tpl->setVariable('FORM', $filter_form->getHTML());
207 
208  if (!count($coupons = $this->coupon_obj->getCoupons()))
209  {
210  ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
211  //if ($_POST['updateView'] == '1') ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
212  //else ilUtil::sendInfo($this->lng->txt('paya_coupons_not_available'));
213 
214  return true;
215  }
216 
217  $counter = 0;
218  foreach ($coupons as $coupon)
219  {
220  $f_result[$counter]['pc_title'] = $coupon['pc_title'];
221  $f_result[$counter]['number_of_codes'] = $coupon['number_of_codes'];
222  $f_result[$counter]['usage_of_codes'] = $coupon['usage_of_codes'];
223 
224  if (!empty($coupon['objects']))
225  {
226  $objects = "";
227  for ($i = 0; $i < count($coupon['objects']); $i++)
228  {
229  $tmp_obj = ilObjectFactory::getInstanceByRefId($coupon['objects'][$i], false);
230  if($tmp_obj)
231  {
232  $objects .= $tmp_obj->getTitle();
233  }
234  else
235  {
236  $objects .= $this->lng->txt('object_not_found');
237  }
238 
239  if ($i < count($coupon['objects']) - 1) $objects .= "<br />";
240 
241  unset($tmp_obj);
242  }
243  }
244  else
245  {
246  $objects = "";
247  }
248 
249  $f_result[$counter]['objects'] = $objects;
250  $f_result[$counter]['pc_from'] = ($coupon['pc_from'] != NULL && $coupon['pc_from_enabled'] == '1') ? $coupon['pc_from'] : '';
251  $f_result[$counter]['pc_till'] = ($coupon['pc_till'] != NULL && $coupon['pc_till_enabled'] == '1') ? $coupon['pc_till'] : '';
252  $f_result[$counter]['pc_last_changed'] = ($coupon['pc_last_changed'] != NULL ? $coupon['pc_last_changed'] : '');
253  $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']));
254  $this->ctrl->setParameter($this, 'coupon_id', $coupon['pc_pk']);
255  $f_result[$counter]['options'] = "<div class=\"il_ContainerItemCommands\"><a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "addCoupon")."\">".$this->lng->txt("edit")."</a>";
256  $f_result[$counter]['options'] .= " <a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "deleteCoupon")."\">".$this->lng->txt("delete")."</a></div>";
257 
258  ++$counter;
259  }
260 
261  return $this->__showCouponsTable($f_result);
262  }
263 
264  private function __showCouponsTable($f_result)
265  {
266  require_once 'Services/Payment/classes/class.ilShopCouponsTableGUI.php';
267  $tbl = new ilShopCouponsTableGUI($this, 'showCoupons');
268  $tbl->setTitle($this->lng->txt("paya_coupons_coupons"));
269  $tbl->setId('tbl_show_coupons');
270  $tbl->setRowTemplate("tpl.shop_coupons_row.html", "Services/Payment");
271 
272  $tbl->addColumn($this->lng->txt('paya_coupons_title'), 'pc_title', '10%');
273  $tbl->addColumn($this->lng->txt('paya_coupons_number_of_codes'), 'number_of_codes', '10%');
274  $tbl->addColumn($this->lng->txt('paya_coupons_usage_of_codes'), 'usage_of_codes', '10%');
275  $tbl->addColumn($this->lng->txt('paya_coupons_objects'), 'objects', '10%');
276  $tbl->addColumn($this->lng->txt('paya_coupons_from'), 'pc_from', '10%');
277  $tbl->addColumn($this->lng->txt('paya_coupons_till'), 'pc_till', '10%');
278  $tbl->addColumn($this->lng->txt('last_change'), 'pc_last_changed', '10%');
279  $tbl->addColumn($this->lng->txt('author'), 'pc_last_changed_author', '10%');
280  $tbl->addColumn('', 'options','30%');
281 
282  $tbl->setData($f_result);
283  $this->tpl->setVariable('TABLE', $tbl->getHTML());
284 
285  return true;
286  }
287 
288  public function saveCouponForm()
289  {
290  $this->error = '';
291 
292  if ($_POST['title'] == '') $this->error .= 'paya_coupons_title,';
293  if ($_POST['coupon_type'] == '') $this->error .= 'paya_coupons_type,';
294  if ($_POST['coupon_value'] == '') $this->error .= 'paya_coupons_value,';
295  else $_POST['coupon_value'] = ilFormat::checkDecimal($_POST['coupon_value']);
296 
297  $this->coupon_obj->setId($_GET['coupon_id']);
298  $this->coupon_obj->setCouponUser($this->user_obj->getId());
299  $this->coupon_obj->setTitle(ilUtil::stripSlashes($_POST['title']));
300  $this->coupon_obj->setDescription(ilUtil::stripSlashes($_POST['description']));
301  $this->coupon_obj->setType(ilUtil::stripSlashes($_POST['coupon_type']));
302  $this->coupon_obj->setValue(ilUtil::stripSlashes($_POST['coupon_value']));
303  $this->coupon_obj->setFromDate( date("Y-m-d",mktime(0,0,0,$_POST['from']['date']['m'],$_POST['from']['date']['d'],$_POST['from']['date']['y'])));
304  $this->coupon_obj->setTillDate( date("Y-m-d",mktime(0,0,0,$_POST['til']['date']['m'],$_POST['til']['date']['d'],$_POST['til']['date']['y'])));
305  // $this->coupon_obj->setFromDateEnabled(ilUtil::stripSlashes($_POST['from_check']));
306  //$this->coupon_obj->setTillDateEnabled(ilUtil::stripSlashes($_POST['until_check']));
307 
308  $this->coupon_obj->setFromDateEnabled($_POST['from_check']);
309  $this->coupon_obj->setTillDateEnabled($_POST['until_check']);
310  $this->coupon_obj->setUses((int)ilUtil::stripSlashes($_POST['usage']));
311  $this->coupon_obj->setChangeDate(date('Y-m-d H:i:s'));
312 
313  if ($this->error == '')
314  {
315  if ($_GET['coupon_id'] != "")
316  {
317  $this->coupon_obj->update();
318  }
319  else
320  {
321  $_GET['coupon_id'] = $this->coupon_obj->add();
322  }
323 
324  ilUtil::sendInfo($this->lng->txt('saved_successfully'));
325  }
326  else
327  {
328  if (is_array($e = explode(',', $this->error)))
329  {
330  $mandatory = '';
331  for ($i = 0; $i < count($e); $i++)
332  {
333  $e[$i] = trim($e[$i]);
334  if ($e[$i] != '')
335  {
336  $mandatory .= $this->lng->txt($e[$i]);
337  if (array_key_exists($i + 1, $e) && $e[$i + 1] != '') $mandatory .= ', ';
338  }
339  }
340  ilUtil::sendInfo($this->lng->txt('fill_out_all_required_fields') . ': ' . $mandatory);
341  }
342  }
343 
344  $this->addCoupon();
345 
346  return true;
347  }
348 
349  public function addCoupon()
350  {
351  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
352 
353  if (isset($_GET['coupon_id']))
354  {
355  if ($this->error == '') $this->coupon_obj->getCouponById($_GET['coupon_id']);
356 
357  $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
358 
359  $this->__showButtons();
360  }
361 
362 
363  $oForm = new ilPropertyFormGUI();
364  $oForm->setId('frm_add_coupon');
365  $oForm->setFormAction($this->ctrl->getFormAction($this,'saveCouponForm'));
366  $oForm->setTitle($this->coupon_obj->getId() ? $this->lng->txt('paya_coupons_edit') : $this->lng->txt('paya_coupons_add'));
367 
368  // Title
369  $oTitle = new ilTextInputGUI($this->lng->txt(paya_coupons_title),'title');
370  $oTitle->setValue($this->coupon_obj->getTitle());
371  $oTitle->setRequired(true);
372  $oForm->addItem($oTitle);
373 
374  // Description
375  $oDescription = new ilTextAreaInputGUI($this->lng->txt(paya_coupons_description),'description');
376  $oDescription->setValue($this->coupon_obj->getDescription());
377  $oForm->addItem($oDescription);
378 
379  // Type
380  $o_coupon_type = new ilSelectInputGUI();
381  $coupon_option = array('fix'=>$this->lng->txt('paya_coupons_fix'),'percent'=>$this->lng->txt('paya_coupons_percentaged'));
382 
383  $o_coupon_type->setTitle($this->lng->txt('coupon_type'));
384  $o_coupon_type->setOptions($coupon_option);
385  $o_coupon_type->setValue($this->coupon_obj->getType());
386  $o_coupon_type->setRequired(true);
387  $o_coupon_type->setPostVar('coupon_type');
388 
389  $oForm->addItem($o_coupon_type);
390 
391  // Value
392 
393  $o_coupon_value = new ilNumberInputGUI($this->lng->txt('paya_coupons_value'),'coupon_value');
394  $o_coupon_value->setSize(5);
395  $o_coupon_value->allowDecimals(true);
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  || (!is_numeric($_POST["generate_length"]) || $_POST["generate_length"] <= 0))
674  {
675  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
676 
677  $this->generateCodes();
678  }
679  else
680  {
681  $this->generateCodes("input");
682  }
683  }
684  else if ($_POST["generate_type"] == "auto")
685  {
686  if ($_GET["coupon_id"] && is_numeric($_POST["generate_number"]) && $_POST["generate_number"] > 0)
687  {
688  for ($i = 0; $i < $_POST["generate_number"]; $i++)
689  {
690  $code = $this->__makeCode("", $_SESSION["pay_coupons"]["code_length"]);
691 
692  if ($code != "")
693  {
694  $this->coupon_obj->addCode($code, $_GET["coupon_id"]);
695  }
696  }
697 
698  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
699 
700  $this->showCodes();
701  }
702  else
703  {
704  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
705 
706  $this->generateCodes();
707  }
708  }
709  }
710 
711  private function __makeCode($a_code = "", $a_length = 32)
712  {
713  if ($a_code == "") $a_code = md5(uniqid(rand()));
714 
715  if (is_numeric($a_length) && strlen($a_code) > $a_length)
716  {
717  $a_code = substr($a_code, 0, $a_length);
718  }
719 
720  return $a_code;
721  }
722 
723  public function generateCodes($view = "choice")
724  {
725  $this->coupon_obj->setId($_GET['coupon_id']);
726 
727  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
728  $this->__showButtons();
729 
730  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
731 
732  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
733  $oForm_1 = new ilPropertyFormGUI();
734  $oForm_1->setId('save_frm');
735  $oForm_1->setFormAction($this->ctrl->getFormAction($this),'saveCodeForm');
736  $oForm_1->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_code_generation'));
737 
738  if ($view == "choice")
739  {
740  $oTypeRadio = new ilRadioGroupInputGUI($this->lng->txt('paya_coupons_generate_codes'), 'generate_type');
741 
742  $radio_option = new ilRadioOption($this->lng->txt('paya_coupons_type_auto'), 'auto');
743  $oTypeRadio->addOption($radio_option);
744  $radio_option = new ilRadioOption($this->lng->txt('paya_coupons_type_self'), 'self');
745  $oTypeRadio->addOption($radio_option);
746 
747  $oTypeRadio->setValue(isset($_POST["generate_type"]) ? $_POST["generate_type"] : "auto");
748  $oTypeRadio->setPostVar('generate_type');
749  $oForm_1->addItem($oTypeRadio);
750 
751  $oNumCodes = new ilNumberInputGUI($this->lng->txt("paya_coupons_number_of_codes"),'generate_number');
752  $oNumCodes->setSize(5);
753  $oNumCodes->setValue($_POST['generate_number']);
754  $oNumCodes->setRequired(true);
755  $oForm_1->addItem($oNumCodes);
756 
757  $oLength = new ilNumberInputGUI($this->lng->txt("paya_coupons_code_length"),'generate_length');
758  $oLength->setSize(5);
759  $oLength->setValue($_POST['generate_length']);
760  $oLength->setRequired(true);
761  $oLength->setInfo($this->lng->txt('paya_coupons_type_self'));
762  $oForm_1->addItem($oLength);
763 
764  $oForm_1->addCommandButton('saveCodeForm',$this->lng->txt('save'));
765 
766  $this->tpl->setVariable('FORM', $oForm_1->getHTML());
767 
768  $oForm_2 = new ilPropertyformGUI();
769  $oForm_2->setId('import_frm');
770  $oForm_2->setFormAction($this->ctrl->getFormAction($this), 'showCodeImport');
771  $oForm_2->addCommandButton('showCodeImport',$this->lng->txt('import'));
772  $this->tpl->setVariable('FORM_2', $oForm_2->getHTML());
773 
774  }
775  else if ($view == "input")
776  {
777  if (is_numeric($_POST['generate_number']) && $_POST['generate_number'] > 0)
778  {
779  for ($i = 0; $i < $_POST['generate_number']; $i++)
780  {
781  $index = $i +1;
782  $oLoopCode = new ilTextInputGUI('#'.$index,'codes['.$i.']');
783  $oForm_1->addItem($oLoopCode);
784  }
785  $oForm_1->addCommandButton('saveCodeForm',$this->lng->txt('save'));
786  }
787 
788  $this->tpl->setVariable('FORM',$oForm_1->getHTML());
789 
790  $oLoopCode = new ilTextInputGUI();
791  }
792 
793  return true;
794  }
795 
796  public function assignObjects()
797  {
798  if (is_array($_POST['object_id']))
799  {
800  $this->coupon_obj->setId($_GET["coupon_id"]);
801  foreach($_POST['object_id'] as $id)
802  {
803  if ($id)
804  {
805  $this->coupon_obj->assignObjectToCoupon($id);
806  }
807  }
808  }
809 
810  $this->showObjects();
811 
812  return true;
813  }
814 
815  public function unassignObjects()
816  {
817  if (is_array($_POST['object_id']))
818  {
819  $this->coupon_obj->setId($_GET["coupon_id"]);
820  foreach($_POST['object_id'] as $id)
821  {
822  if ($id)
823  {
824  $this->coupon_obj->unassignObjectFromCoupon($id);
825  }
826  }
827  }
828 
829  $this->showObjects();
830 
831  return true;
832  }
833 
834  public function showObjects()
835  {
836  $this->coupon_obj->setId($_GET['coupon_id']);
837 
838  $this->__initPaymentObject();
839 
840  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
841  $this->__showButtons();
842 
843  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
844 
845  $objects = $this->pobject->_getObjectsData($this->user_obj->getId());
846 
847  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
848 
849  $counter_assigned = 0;
850  $counter_unassigned = 0;
851  $f_result_assigned = array();
852  $f_result_unassigned = array();
853  foreach($objects as $data)
854  {
855  if ($this->coupon_obj->isObjectAssignedToCoupon($data['ref_id']))
856  {
857  $p_counter =& $counter_assigned;
858  $p_result =& $f_result_assigned;
859  }
860  else
861  {
862  $p_counter =& $counter_unassigned;
863  $p_result =& $f_result_unassigned;
864  }
865 
866  $tmp_obj = ilObjectFactory::getInstanceByRefId($data['ref_id'], false);
867  if($tmp_obj)
868  {
869  $p_result[$p_counter]['object_id'] = ilUtil::formCheckbox(0, 'object_id[]', $data['ref_id']);
870  $p_result[$p_counter]['title'] = $tmp_obj->getTitle();
871 
872  switch($data['status'])
873  {
874  case $this->pobject->STATUS_BUYABLE:
875  $p_result[$p_counter]['status'] = $this->lng->txt('paya_buyable');
876  break;
877 
878  case $this->pobject->STATUS_NOT_BUYABLE:
879  $p_result[$p_counter]['status'] = $this->lng->txt('paya_not_buyable');
880  break;
881 
882  case $this->pobject->STATUS_EXPIRES:
883  $p_result[$p_counter]['status'] = $this->lng->txt('paya_expires');
884  break;
885  }
886  include_once './Services/Payment/classes/class.ilPayMethods.php';
887  $p_result[$p_counter]['pay_method'] = ilPaymethods::getStringByPaymethod($data['pay_method']);
888  }
889  else
890  {
891  $p_result[$p_counter]['object_id'] = '';
892  $p_result[$p_counter]['title'] = $this->lng->txt('object_not_found');
893  $p_result[$p_counter]['status'] = '';
894  $p_result[$p_counter]['pay_method'] ='';
895  }
896  ++$p_counter;
897 
898  unset($tmp_obj);
899  }
900 
901  $this->ctrl->setParameter($this, "cmd", "showObjects");
902 
903  if (count($f_result_assigned) > 0)
904  {
905  $tbl = new ilShopTableGUI($this);
906  $tbl->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_assigned_objects"));
907  $tbl->setId('tbl_show_assigned');
908  $tbl->setPrefix('assigned');
909  $tbl->setRowTemplate("tpl.shop_objects_row.html", "Services/Payment");
910 
911  $tbl->addColumn('', 'object_id', '1%');
912  $tbl->addColumn($this->lng->txt('title'),'title', '10%');
913  $tbl->addColumn($this->lng->txt('status'), 'status', '30%');
914  $tbl->addColumn($this->lng->txt('paya_pay_method'), 'paya_pay_method', '60%');
915 
916  $tbl->setSelectAllCheckbox('object_id');
917 
918  $tbl->addMultiCommand('unassignObjects',$this->lng->txt('remove'));
919 
920 
921  $tbl->setData($f_result_assigned);
922 
923  $this->tpl->setVariable('TABLE', $tbl->getHTML());
924  }
925 
926  if (count($f_result_unassigned) > 0)
927  {
928  $tbl_2 = new ilShopTableGUI($this);
929  $tbl_2->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_unassigned_objects"));
930  $tbl_2->setId('tbl_show_unassigned');
931  $tbl_2->setPrefix('unassigned');
932  $tbl_2->setRowTemplate("tpl.shop_objects_row.html", "Services/Payment");
933 
934  $tbl_2->addColumn('', 'object_id', '1%');
935  $tbl_2->addColumn($this->lng->txt('title'),'title', '10%');
936  $tbl_2->addColumn($this->lng->txt('status'), 'status', '30%');
937  $tbl_2->addColumn($this->lng->txt('paya_pay_method'), 'pay_method', '60%');
938 
939  $tbl_2->setSelectAllCheckbox('object_id');
940 
941  $tbl_2->addMultiCommand('assignObjects',$this->lng->txt('add'));
942 
943 
944  $tbl_2->setData($f_result_unassigned);
945 
946  $this->tpl->setVariable('TABLE_2', $tbl_2->getHTML());
947  }
948 
949  return true;
950  }
951 
952  public function importCodes()
953  {
954  include_once('./Services/Utilities/classes/class.ilCSVReader.php');
955 
956  if ($_FILES["codesfile"]["tmp_name"] != "")
957  {
958  $csv = new ilCSVReader();
959  $csv->setDelimiter("");
960 
961  if ($csv->open($_FILES["codesfile"]["tmp_name"]))
962  {
963  $data = $csv->getDataArrayFromCSVFile();
964 
965  for ($i = 0; $i < count($data); $i++)
966  {
967  for ($j = 0; $j < count($data[$i]); $j++)
968  {
969  $this->coupon_obj->addCode(ilUtil::stripSlashes($data[$i][$j]), $_GET["coupon_id"]);
970  }
971  }
972 
973  ilUtil::sendInfo($this->lng->txt("paya_coupon_codes_import_successful"));
974 
975  $this->showCodes();
976  }
977  else
978  {
979  ilUtil::sendInfo($this->lng->txt("paya_coupons_import_error_opening_file"));
980 
981  $this->showCodeImport();
982  }
983  }
984  else
985  {
986  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
987 
988  $this->showCodeImport();
989  }
990 
991  return true;
992  }
993 
994  public function showCodeImport()
995  {
996  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
997  $this->__showButtons();
998  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
999 
1000  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
1001  $oForm = new ilPropertyFormGUI();
1002  $oForm->setId('coup_form');
1003  $oForm->setFormAction($this->ctrl->getFormAction($this), 'importCodes');
1004  $oForm->setTitle( $this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_codes_import'));
1005 
1006  $oFile = new ilFileInputGUI($this->lng->txt('file'),'codesfile');
1007  $oFile->setSuffixes(array("txt"));
1008  $oFile->setRequired(true);
1009  $oFile->setInfo($this->lng->txt('import_use_only_textfile'));
1010  $oForm->addItem($oFile);
1011 
1012  $oForm->addCommandButton('importCodes',$this->lng->txt('upload'));
1013 
1014  $this->tpl->setVariable('FORM', $oForm->getHTML());
1015 
1016  return true;
1017  }
1018 
1019  private function __showButtons()
1020  {
1021  global $ilToolbar;
1022 
1023  $ilToolbar->addButton($this->lng->txt('paya_coupons_edit'), $this->ctrl->getLinkTarget($this, 'addCoupon'));
1024  $ilToolbar->addButton($this->lng->txt('paya_coupons_edit_codes'), $this->ctrl->getLinkTarget($this, 'showCodes'));
1025  $ilToolbar->addButton($this->lng->txt('paya_coupons_edit_objects'), $this->ctrl->getLinkTarget($this, 'showObjects'));
1026 
1027  return true;
1028  }
1029 
1030  private function __initPaymentObject($a_pobject_id = 0)
1031  {
1032  include_once './Services/Payment/classes/class.ilPaymentObject.php';
1033 
1034  $this->pobject = new ilPaymentObject($this->user_obj, $a_pobject_id);
1035 
1036  return true;
1037  }
1038 
1039  private function __initCouponObject()
1040  {
1041  include_once './Services/Payment/classes/class.ilPaymentCoupons.php';
1042 
1043  $this->coupon_obj = new ilPaymentCoupons($this->user_obj, true);
1044 
1045  return true;
1046  }
1047  public function deleteCoupon()
1048  {
1049  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.main_view.html','Services/Payment');
1050 
1051  if (!isset($_GET['coupon_id']))
1052  {
1053  return ilUtil::sendFailure($this->lng->txt('no_coupon_selected'));
1054  }
1055 
1056 # $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
1057 
1058  $this->__showButtons();
1059 
1060  ilUtil::sendQuestion($this->lng->txt('paya_coupons_sure_delete_selected_codes'));
1061  $oConfirmationGUI = new ilConfirmationGUI() ;
1062  // set confirm/cancel commands
1063  $oConfirmationGUI->setFormAction($this->ctrl->getFormAction($this, 'performDeleteCoupon'));
1064  $oConfirmationGUI->setHeaderText('');
1065  $oConfirmationGUI->setCancel($this->lng->txt("cancel"), "showCoupons");
1066  $oConfirmationGUI->setConfirm($this->lng->txt("confirm"), 'performDeleteCoupon');
1067  $oConfirmationGUI->addItem('','', ilPaymentCoupons::_lookupTitle($_GET['coupon_id']));
1068  $oConfirmationGUI->addHiddenItem('coupon_id', $_GET['coupon_id']);
1069  $this->tpl->setVariable('CONFIRMATION', $oConfirmationGUI->getHTML());
1070  return true;
1071 
1072  }
1073 
1074  public function performDeleteCoupon()
1075  {
1076  if (!isset($_POST['coupon_id']))
1077  {
1078  return ilUtil::sendFailure($this->lng->txt('coupon_id_is_missing'));
1079  }
1080  $this->coupon_obj->deleteCouponByCouponId((int)$_POST['coupon_id']);
1081 
1082  $this->showCoupons();
1083  }
1084 }
1085 ?>