ILIAS  Release_4_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
3 {
4  var $ctrl;
5 
6  var $lng;
7 
8  var $user_obj = null;
9  var $coupon_obj = null;
10  var $pobject = null;
11 
12  public function ilPaymentCouponGUI($user_obj)
13  {
15 
16  $this->ctrl->saveParameter($this, 'baseClass');
17  $this->user_obj = $user_obj;
18  $this->__initCouponObject();
19  }
20 
21  protected function prepareOutput()
22  {
23  global $ilTabs;
24 
25  $this->setSection(6);
26 
28 
29  $ilTabs->setTabActive('paya_header');
30  $ilTabs->setSubTabActive('paya_coupons_coupons');
31  }
32 
33  public function executeCommand()
34  {
35  global $tree;
36 
37  $cmd = $this->ctrl->getCmd();
38  switch ($this->ctrl->getNextClass($this))
39  {
40  default:
41  if (!$cmd = $this->ctrl->getCmd())
42  {
43  $cmd = 'showCoupons';
44  }
45  $this->prepareOutput();
46  $this->$cmd();
47  break;
48  }
49  }
50 
51  function resetFilter()
52  {
53  unset($_POST['title_type']);
54  unset($_POST['title_value']);
55  unset($_POST['type']);
56  unset($_POST['from']['d']);
57  unset($_POST['from']['m']);
58  unset($_POST['from']['y']);
59  unset($_POST['till']['d']);
60  unset($_POST['till']['m']);
61  unset($_POST['till']['y']);
62  unset($_POST['from_enabled']);
63  unset($_POST['till_enabled']);
64 
65  $this->showCoupons();
66 
67  return true;
68  }
69 
70  function showCoupons()
71  {
72  include_once("Services/User/classes/class.ilObjUser.php");
73 
74  $this->showButton('addCoupon', $this->lng->txt('paya_coupons_add'));
75 
76  if ($_POST['updateView'] == '1')
77  {
78  $_SESSION['pay_coupons']['title_type'] = $_POST['title_type'];
79  $_SESSION['pay_coupons']['title_value'] = $_POST['title_value'];
80  $_SESSION['pay_coupons']['type'] = $_POST['type'];
81  $_SESSION['pay_coupons']['from']['d'] = $_POST['from']['d'];
82  $_SESSION['pay_coupons']['from']['m'] = $_POST['from']['m'];
83  $_SESSION['pay_coupons']['from']['y'] = $_POST['from']['y'];
84  $_SESSION['pay_coupons']['till']['d'] = $_POST['till']['d'];
85  $_SESSION['pay_coupons']['till']['m'] = $_POST['till']['m'];
86  $_SESSION['pay_coupons']['till']['y'] = $_POST['till']['y'];
87  $_SESSION['pay_coupons']['from_enabled'] = $_POST['from_enabled'];
88  $_SESSION['pay_coupons']['till_enabled'] = $_POST['till_enabled'];
89  }
90 
91  $this->coupon_obj->setSearchTitleType(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_type']));
92  $this->coupon_obj->setSearchTitleValue(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_value']));
93  $this->coupon_obj->setSearchType(ilUtil::stripSlashes($_SESSION['pay_coupons']['type']));
94  $this->coupon_obj->setSearchFromDay(ilUtil::stripSlashes($_SESSION['pay_coupons']['from']['d']));
95  $this->coupon_obj->setSearchFromMonth(ilUtil::stripSlashes($_SESSION['pay_coupons']['from']['m']));
96  $this->coupon_obj->setSearchFromYear(ilUtil::stripSlashes($_SESSION['pay_coupons']['from']['y']));
97  $this->coupon_obj->setSearchTillDay(ilUtil::stripSlashes($_SESSION['pay_coupons']['till']['d']));
98  $this->coupon_obj->setSearchTillMonth(ilUtil::stripSlashes($_SESSION['pay_coupons']['till']['m']));
99  $this->coupon_obj->setSearchTillYear(ilUtil::stripSlashes($_SESSION['pay_coupons']['till']['y']));
100  $this->coupon_obj->setSearchFromDateEnabled(ilUtil::stripSlashes($_SESSION['pay_coupons']['from_enabled']));
101  $this->coupon_obj->setSearchTillDateEnabled(ilUtil::stripSlashes($_SESSION['pay_coupons']['till_enabled']));
102 
103 
104  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paya_coupons.html', 'payment');
105 
106  $this->tpl->setVariable("TXT_FILTER", $this->lng->txt('pay_filter'));
107  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
108 
109  $this->tpl->setVariable("TXT_COUPON",$this->lng->txt('paya_coupons_title'));
110  $this->tpl->setVariable("TXT_STARTING",$this->lng->txt('pay_starting'));
111  $this->tpl->setVariable("TXT_ENDING",$this->lng->txt('pay_ending'));
112  $this->tpl->setVariable("TXT_TYPE",$this->lng->txt('paya_coupons_type'));
113  $this->tpl->setVariable("TXT_TYPE_FIX",$this->lng->txt('paya_coupons_fix'));
114  $this->tpl->setVariable("TXT_TYPE_PERCENTAGED",$this->lng->txt('paya_coupons_percentaged'));
115  $this->tpl->setVariable("TXT_VALID_DATE_FROM",$this->lng->txt('paya_coupons_from'));
116  $this->tpl->setVariable("TXT_VALID_DATE_TILL",$this->lng->txt('paya_coupons_till'));
117  $this->tpl->setVariable("TXT_UPDATE_VIEW",$this->lng->txt('pay_update_view'));
118  $this->tpl->setVariable("TXT_RESET_FILTER",$this->lng->txt('pay_reset_filter'));
119  $this->tpl->setVariable('TXT_ENABLED', $this->lng->txt('enabled'));
120  $this->tpl->setVariable("TITLE_TYPE_" . $this->coupon_obj->getSearchTitleType(), " selected=\"selected\"");
121  $this->tpl->setVariable("TITLE_VALUE", ilUtil::prepareFormOutput($this->coupon_obj->getSearchTitleValue(), true));
122  $this->tpl->setVariable("TYPE_" . strtoupper($this->coupon_obj->getSearchType()), " selected=\"selected\"");
123 
124 
125  if ($this->coupon_obj->getSearchFromDateEnabled()) $this->tpl->setVariable('CHECKED_STARTING_DATE', " checked=\"checked\"");
126  if ($this->coupon_obj->getSearchTillDateEnabled()) $this->tpl->setVariable('CHECKED_ENDING_DATE', " checked=\"checked\"");
127  $this->tpl->setVariable('DATE_FROM', ilUtil::makeDateSelect("from", $this->coupon_obj->getSearchFromYear(), $this->coupon_obj->getSearchFromMonth(), $this->coupon_obj->getSearchFromDay(), 2004, true, array("first_option_empty" => true)));
128  $this->tpl->setVariable('DATE_TILL', ilUtil::makeDateSelect("till", $this->coupon_obj->getSearchTillYear(), $this->coupon_obj->getSearchTillMonth(), $this->coupon_obj->getSearchTillDay(), 2004, true, array("first_option_empty" => true)));
129 
130  if (!count($coupons = $this->coupon_obj->getCoupons()))
131  {
132  ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
133  //if ($_POST['updateView'] == '1') ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
134  //else ilUtil::sendInfo($this->lng->txt('paya_coupons_not_available'));
135 
136  return true;
137  }
138 
139  $counter = 0;
140  foreach ($coupons as $coupon)
141  {
142  $f_result[$counter][] = $coupon['pc_title'];
143  $f_result[$counter][] = $coupon['number_of_codes'];
144  $f_result[$counter][] = $coupon['usage_of_codes'];
145 
146  if (!empty($coupon['objects']))
147  {
148  $objects = "";
149  for ($i = 0; $i < count($coupon['objects']); $i++)
150  {
151  $tmp_obj =& ilObjectFactory::getInstanceByRefId($coupon['objects'][$i], false);
152  if($tmp_obj)
153  {
154  $objects .= $tmp_obj->getTitle();
155  }
156  else
157  {
158  $objects .= $this->lng->txt('object_not_found');
159  }
160 
161  if ($i < count($coupon['objects']) - 1) $objects .= "<br />";
162 
163  unset($tmp_obj);
164  }
165  }
166  else
167  {
168  $objects = "";
169  }
170 
171  $f_result[$counter][] = $objects;
172 
173 
174  // $f_result[$counter][] = ($coupon['pc_from'] != '0000-00-00' && $coupon['pc_from_enabled'] == '1') ? ilFormat::formatDate($coupon['pc_from'], 'date') : '';
175  // $f_result[$counter][] = ($coupon['pc_till'] != '0000-00-00' && $coupon['pc_till_enabled'] == '1') ? ilFormat::formatDate($coupon['pc_till'], 'date') : '';
176  $f_result[$counter][] = ($coupon['pc_from'] != NULL && $coupon['pc_from_enabled'] == '1') ? ilFormat::formatDate($coupon['pc_from'], 'date') : '';
177  $f_result[$counter][] = ($coupon['pc_till'] != NULl && $coupon['pc_till_enabled'] == '1') ? ilFormat::formatDate($coupon['pc_till'], 'date') : '';
178  $f_result[$counter][] =
179  ($coupon['pc_last_changed'] != '0000-00-00 00:00:00' ? ilFormat::formatDate($coupon['pc_last_changed']) : '') .
180  ($coupon['pc_last_change_usr_id'] != '0' ? "[" . ilObjUser::_lookupLogin($coupon['pc_last_change_usr_id']) . "]" : '');
181  $this->ctrl->setParameter($this, 'coupon_id', $coupon['pc_pk']);
182  $f_result[$counter][] = "<div class=\"il_ContainerItemCommands\"><a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "addCoupon")."\">".$this->lng->txt("edit")."</a></div>";
183 
184  ++$counter;
185  }
186 
187  return $this->__showCouponsTable($f_result);
188  }
189 
190  function __showCouponsTable($f_result)
191  {
192  $tbl =& $this->initTableGUI();
193  $tpl =& $tbl->getTemplateObject();
194 
195  $tpl->setCurrentBlock("tbl_form_header");
196 
197  $tpl->setVariable("FORMACTION",$this->ctrl->getFormAction($this));
198  $tpl->parseCurrentBlock();
199 
200  $tbl->setTitle($this->lng->txt("paya_coupons_coupons"), "icon_pays.gif", $this->lng->txt("paya_coupons_coupons"));
201  $tbl->setHeaderNames(array($this->lng->txt("paya_coupons_title"),
202  $this->lng->txt("paya_coupons_number_of_codes"),
203  $this->lng->txt("paya_coupons_usage_of_codes"),
204  $this->lng->txt("paya_coupons_objects"),
205  $this->lng->txt("paya_coupons_from"),
206  $this->lng->txt("paya_coupons_till"),
207  $this->lng->txt("last_change"), ''));
208  $header_params = $this->ctrl->getParameterArray($this, '');
209  $tbl->setHeaderVars(array('pc_title',
210  'number_of_codes',
211  'usage_of_codes',
212  'objects',
213  'pc_from',
214  'pc_till',
215  'last_changed'),
216  $header_params
217  );
218  $offset = $_GET['offset'];
219  $order = $_GET['sort_by'];
220  $direction = $_GET['sort_order'] ? $_GET['sort_order'] : 'desc';
221  $tbl->setOrderColumn($order,'pc_title');
222  $tbl->setOrderDirection($direction);
223  $tbl->setOffset($offset);
224  $tbl->setLimit($_GET['limit']);
225  $tbl->setMaxCount(count($f_result));
226  $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
227  $tbl->setData($f_result);
228  $tbl->render();
229 
230  $this->tpl->setVariable('COUPONS_TABLE', $tbl->tpl->get());
231 
232  return true;
233  }
234 
235  function saveCouponForm()
236  {
237  $this->error = '';
238 
239  if ($_POST['title'] == '') $this->error .= 'paya_coupons_title,';
240  if ($_POST['type'] == '') $this->error .= 'paya_coupons_type,';
241  if ($_POST['value'] == '') $this->error .= 'paya_coupons_value,';
242  else $_POST['value'] = ilFormat::checkDecimal($_POST['value']);
243 
244  $this->coupon_obj->setId($_GET['coupon_id']);
245  $this->coupon_obj->setCouponUser($this->user_obj->getId());
246  $this->coupon_obj->setTitle(ilUtil::stripSlashes($_POST['title']));
247  $this->coupon_obj->setDescription(ilUtil::stripSlashes($_POST['description']));
248  $this->coupon_obj->setType(ilUtil::stripSlashes($_POST['type']));
249  $this->coupon_obj->setValue(ilUtil::stripSlashes($_POST['value']));
250  $this->coupon_obj->setFromDate( date("Y-m-d",mktime(0,0,0,$_POST['from']['m'],$_POST['from']['d'],$_POST['from']['y'])));
251  $this->coupon_obj->setTillDate( date("Y-m-d",mktime(0,0,0,$_POST['till']['m'],$_POST['till']['d'],$_POST['till']['y'])));
252  $this->coupon_obj->setFromDateEnabled(ilUtil::stripSlashes($_POST['pc_from_enabled']));
253  $this->coupon_obj->setTillDateEnabled(ilUtil::stripSlashes($_POST['pc_till_enabled']));
254  $this->coupon_obj->setUses((int)ilUtil::stripSlashes($_POST['usage']));
255  $this->coupon_obj->setChangeDate(date('Y-m-d H:i:s'));
256 
257  if ($this->error == '')
258  {
259  if ($_GET['coupon_id'] != "")
260  {
261  $this->coupon_obj->update();
262  }
263  else
264  {
265  $_GET['coupon_id'] = $this->coupon_obj->add();
266  }
267 
268  ilUtil::sendInfo($this->lng->txt('saved_successfully'));
269  }
270  else
271  {
272  if (is_array($e = explode(',', $this->error)))
273  {
274  $mandatory = '';
275  for ($i = 0; $i < count($e); $i++)
276  {
277  $e[$i] = trim($e[$i]);
278  if ($e[$i] != '')
279  {
280  $mandatory .= $this->lng->txt($e[$i]);
281  if (array_key_exists($i + 1, $e) && $e[$i + 1] != '') $mandatory .= ', ';
282  }
283  }
284  ilUtil::sendInfo($this->lng->txt('fill_out_all_required_fields') . ': ' . $mandatory);
285  }
286  }
287 
288  $this->addCoupon();
289 
290  return true;
291  }
292 
293  function addCoupon()
294  {
295  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_add.html','payment');
296 
297  if (isset($_GET['coupon_id']))
298  {
299  if ($this->error == '') $this->coupon_obj->getCouponById($_GET['coupon_id']);
300 
301  $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
302 
303  $this->__showButtons();
304  }
305 
306  $this->tpl->setVariable('FORMACTION', $this->ctrl->getFormAction($this));
307 
308  $this->tpl->setVariable('TXT_HEADLINE', ($this->coupon_obj->getId() != '' ? $this->lng->txt('paya_coupons_edit') : $this->lng->txt('paya_coupons_add')));
309  $this->tpl->setVariable('TXT_TITLE', $this->lng->txt('paya_coupons_title'));
310  $this->tpl->setVariable('TXT_DESCRIPTION', $this->lng->txt('paya_coupons_description'));
311  $this->tpl->setVariable('TXT_TYPE', $this->lng->txt('paya_coupons_type'));
312  $this->tpl->setVariable('TXT_FIX', $this->lng->txt('paya_coupons_fix'));
313  $this->tpl->setVariable('TXT_PERCENTAGED', $this->lng->txt('paya_coupons_percentaged'));
314  $this->tpl->setVariable('TXT_VALUE', $this->lng->txt('paya_coupons_value'));
315  $this->tpl->setVariable('TXT_ENABLED', $this->lng->txt('enabled'));
316  $this->tpl->setVariable('TXT_VALID_FROM', $this->lng->txt('paya_coupons_from'));
317  $this->tpl->setVariable('TXT_VADID_TILL', $this->lng->txt('paya_coupons_till'));
318  $this->tpl->setVariable('TXT_USAGE', $this->lng->txt('paya_coupons_availability'));
319 
320  $this->tpl->setVariable('TITLE', ilUtil::prepareFormOutput($this->coupon_obj->getTitle() , true));
321  $this->tpl->setVariable('DESCRIPTION', ilUtil::prepareFormOutput($this->coupon_obj->getDescription(), true));
322  $this->tpl->setVariable('TYPE_' . strtoupper($this->coupon_obj->getType()), " selected=\"selected\"");
323  $this->tpl->setVariable('VALUE', ilUtil::prepareFormOutput($this->coupon_obj->getValue(), true));
324  $this->tpl->setVariable('USAGE', ilUtil::prepareFormOutput($this->coupon_obj->getUses(), true));
325 
326  $from_date = explode('-', $this->coupon_obj->getFromDate());
327  $from_day = $from_date[2] != '00' ? $from_date[2] : '';
328  $from_month = $from_date[1] != '00' ? $from_date[1] : '';
329  $from_year = $from_date[0] != '0000' ? $from_date[0] : '';
330 
331  $till_date = explode('-', $this->coupon_obj->getTillDate());
332  $till_day = $till_date[2] != '00' ? $till_date[2] : '';
333  $till_month = $till_date[1] != '00' ? $till_date[1] : '';
334  $till_year = $till_date[0] != '0000' ? $till_date[0] : '';
335 
336  if ($this->coupon_obj->getFromDateEnabled()) $this->tpl->setVariable('CHECKED_STARTING_DATE', " checked=\"checked\"");
337  if ($this->coupon_obj->getTillDateEnabled()) $this->tpl->setVariable('CHECKED_ENDING_DATE', " checked=\"checked\"");
338 
339  $this->tpl->setVariable('DATE_FROM', ilUtil::makeDateSelect("from", $from_year, $from_month, $from_day, 2004));
340  $this->tpl->setVariable('DATE_TILL', ilUtil::makeDateSelect("till", $till_year, $till_month, $till_day, 2004));
341 
342  $this->tpl->setVariable('TXT_REQUIRED_FIELDS', $this->lng->txt('required_field'));
343  $this->tpl->setVariable('TXT_SAVE',$this->lng->txt('save'));
344  $this->tpl->setVariable('TXT_CANCEL',$this->lng->txt('cancel'));
345  $this->tpl->setVariable('COUPONS', 'showCoupons');
346  }
347 
348  function deleteAllCodes()
349  {
350  $this->showCodes("all");
351 
352  return true;
353  }
354 
356  {
357  $this->coupon_obj->deleteAllCodesByCouponId($_GET['coupon_id']);
358 
359  $this->showCodes();
360 
361  return true;
362  }
363 
364  function deleteCodes()
365  {
366  $_SESSION['paya_delete_codes'] = $_POST['codes'];
367 
368  if (!is_array($_POST['codes']))
369  {
370  ilUtil::sendInfo($this->lng->txt('paya_coupons_no_codes_selected'));
371 
372  $this->showCodes();
373 
374  return true;
375  }
376 
377  $this->showCodes("selected");
378 
379  return true;
380  }
381 
383  {
384  if (is_array($_SESSION['paya_delete_codes']))
385  {
386  foreach($_SESSION['paya_delete_codes'] as $id)
387  {
388  $this->coupon_obj->deleteCode($id);
389  }
390  }
391  unset($_SESSION['paya_delete_codes']);
392  ilUtil::sendInfo($this->lng->txt('paya_coupons_code_deleted_successfully'));
393 
394  $this->showCodes();
395 
396  return true;
397  }
398 
399  function cancelDelete()
400  {
401  unset($_SESSION['paya_delete_codes']);
402 
403  $this->showCodes();
404 
405  return true;
406  }
407 
408  function showCodes($a_show_delete = "")
409  {
410  $this->coupon_obj->setId($_GET['coupon_id']);
411 
412  if (!count($codes = $this->coupon_obj->getCodesByCouponId($_GET['coupon_id'])))
413  {
414  ilUtil::sendInfo($this->lng->txt('paya_coupons_codes_not_available'));
415 
416  $this->generateCodes();
417 
418  return true;
419  }
420 
421  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
422 
423  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
424  $this->__showButtons();
425 
426  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_codes.html','payment');
427 
428  if ($a_show_delete == "all")
429  {
430  ilUtil::sendInfo($this->lng->txt('paya_coupons_sure_delete_all_codes'));
431  $this->tpl->setCurrentBlock("confirm_delete_all");
432  $this->tpl->setVariable("CONFIRM_ALL_FORMACTION",$this->ctrl->getFormAction($this));
433  $this->tpl->setVariable("TXT_CANCEL_ALL",$this->lng->txt('cancel'));
434  $this->tpl->setVariable("TXT_CONFIRM_ALL",$this->lng->txt('delete_all'));
435  $this->tpl->parseCurrentBlock();
436  }
437  if ($a_show_delete == "selected")
438  {
439  ilUtil::sendInfo($this->lng->txt('paya_coupons_sure_delete_selected_codes'));
440  $this->tpl->setCurrentBlock("confirm_delete");
441  $this->tpl->setVariable("CONFIRM_FORMACTION",$this->ctrl->getFormAction($this));
442  $this->tpl->setVariable("TXT_CANCEL",$this->lng->txt('cancel'));
443  $this->tpl->setVariable("TXT_CONFIRM",$this->lng->txt('delete'));
444  $this->tpl->parseCurrentBlock();
445  }
446 
447  $_SESSION['paya_delete_codes'] = $_SESSION['paya_delete_codes'] ? $_SESSION['paya_delete_codes'] : array();
448 
449  $counter = 0;
450  foreach ($codes as $code)
451  {
452  $f_result[$counter][] = ilUtil::formCheckbox(in_array($code['pcc_pk'], $_SESSION['paya_delete_codes']) ? 1 : 0,
453  "codes[]",
454  $code['pcc_pk']);
455  $f_result[$counter][] = $code['pcc_code'];
456  $f_result[$counter][] = $code['pcc_used']." ".strtolower($this->lng->txt('of'))." ".$this->coupon_obj->getUses();
457 
458  ++$counter;
459  }
460 
461  $tbl =& $this->initTableGUI();
462  $tpl =& $tbl->getTemplateObject();
463 
464 
465 
466  $tpl->setCurrentBlock("tbl_form_header");
467  $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
468  $tpl->parseCurrentBlock();
469  $tbl->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_codes"), "icon_pays.gif", $this->lng->txt("paya_coupons_codes"));
470  $tbl->setHeaderNames(array('', $this->lng->txt("paya_coupons_code"), $this->lng->txt('paya_coupons_usage_of_codes')));
471  $this->ctrl->setParameter($this, "cmd", "showCodes");
472  $header_params = $this->ctrl->getParameterArray($this, '');
473  $tbl->setHeaderVars(array('', 'pcc_code', 'paya_coupons_usage_of_codes'), $header_params);
474  $offset = $_GET['offset'];
475  $order = $_GET['sort_by'];
476  $direction = $_GET['sort_order'] ? $_GET['sort_order'] : 'desc';
477  $tbl->setOrderColumn($order,'pcc_code');
478  $tbl->setOrderDirection($direction);
479  $tbl->setOffset($offset);
480  $tbl->setLimit($_GET['limit']);
481  $tbl->setMaxCount(count($f_result));
482  $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
483  $tbl->setData($f_result);
484  $tpl->setVariable('COLUMN_COUNTS', 3);
485 
486  $tbl->enable('select_all');
487  $tbl->setFormName('cmd');
488  $tbl->setSelectAllCheckbox('codes');
489 
490  $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
491 
492  $tpl->setCurrentBlock("tbl_action_button");
493  $tpl->setVariable("BTN_NAME","deleteCodes");
494  $tpl->setVariable("BTN_VALUE",$this->lng->txt("delete"));
495  $tpl->parseCurrentBlock();
496 
497  $tpl->setCurrentBlock('plain_button');
498  $tpl->setVariable('PBTN_NAME', 'generateCodes');
499  $tpl->setVariable('PBTN_VALUE', $this->lng->txt('paya_coupons_generate_codes'));
500  $tpl->parseCurrentBlock();
501 
502  $tpl->setCurrentBlock('plain_button');
503  $tpl->setVariable('PBTN_NAME', 'deleteAllCodes');
504  $tpl->setVariable('PBTN_VALUE', $this->lng->txt('delete_all'));
505  $tpl->parseCurrentBlock();
506 
507  $tpl->setCurrentBlock("plain_button");
508  $tpl->setVariable("PBTN_NAME","exportCodes");
509  $tpl->setVariable("PBTN_VALUE",$this->lng->txt("export"));
510  $tpl->parseCurrentBlock();
511 
512  $tbl->setColumnWidth(array("10%","90%"));
513  $tbl->render();
514 
515  $this->tpl->setVariable('CODES_TABLE', $tbl->tpl->get());
516 
517  return true;
518  }
519 
520  function exportCodes()
521  {
522  $codes = $this->coupon_obj->getCodesByCouponId($_GET["coupon_id"]);
523 
524  if (is_array($codes))
525  {
526  include_once('./Services/Utilities/classes/class.ilCSVWriter.php');
527 
528  $csv = new ilCSVWriter();
529  $csv->setDelimiter("");
530 
531  foreach($codes as $data)
532  {
533  if ($data["pcc_code"])
534  {
535  $csv->addColumn($data["pcc_code"]);
536  $csv->addRow();
537  }
538  }
539 
540  ilUtil::deliverData($csv->getCSVString(), "code_export_".date("Ymdhis").".csv");
541  }
542 
543  $this->showCodes();
544 
545  return true;
546  }
547 
548  function saveCodeForm()
549  {
550  if (isset($_POST["generate"]["length"])) $_SESSION["pay_coupons"]["code_length"] = $_POST["generate"]["length"];
551  else $_POST["generate"]["length"] = $_SESSION["pay_coupons"]["code_length"];
552 
553  if (isset($_POST["generate"]["type"])) $_SESSION["pay_coupons"]["code_type"] = $_POST["generate"]["type"];
554  else $_POST["generate"]["type"] = $_SESSION["pay_coupons"]["code_type"];
555 
556  if ($_POST["generate"]["type"] == "self")
557  {
558  if ($_GET["coupon_id"] && is_array($_POST["codes"]))
559  {
560  $count_inserts = 0;
561 
562  for ($i = 0; $i < count($_POST["codes"]); $i++)
563  {
564  $_POST["codes"][$i] = trim($_POST["codes"][$i]);
565 
566  if ($_POST["codes"][$i] != "")
567  {
568  $code = $this->__makeCode($_POST["codes"][$i], $_SESSION["pay_coupons"]["code_length"]);
569 
570  if ($code != "")
571  {
572  if ($this->coupon_obj->addCode(ilUtil::stripSlashes($code), $_GET["coupon_id"]))
573  {
574  ++$count_inserts;
575  }
576  }
577  }
578  }
579 
580  if ($count_inserts)
581  {
582  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
583  $this->showCodes();
584  }
585  else
586  {
587  ilUtil::sendInfo($this->lng->txt("paya_coupons_no_codes_generated"));
588  $this->generateCodes();
589  }
590  }
591  else if (!is_numeric($_POST["generate"]["number"]) || $_POST["generate"]["number"] <= 0)
592  {
593  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
594 
595  $this->generateCodes();
596  }
597  else
598  {
599  $this->generateCodes("input");
600  }
601  }
602  else if ($_POST["generate"]["type"] == "auto")
603  {
604  if ($_GET["coupon_id"] && is_numeric($_POST["generate"]["number"]) && $_POST["generate"]["number"] > 0)
605  {
606  for ($i = 0; $i < $_POST["generate"]["number"]; $i++)
607  {
608  $code = $this->__makeCode("", $_SESSION["pay_coupons"]["code_length"]);
609 
610  if ($code != "")
611  {
612  $this->coupon_obj->addCode($code, $_GET["coupon_id"]);
613  }
614  }
615 
616  ilUtil::sendInfo($this->lng->txt("saved_successfully"));
617 
618  $this->showCodes();
619  }
620  else
621  {
622  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
623 
624  $this->generateCodes();
625  }
626  }
627  }
628 
629  function __makeCode($a_code = "", $a_length = 32)
630  {
631  if ($a_code == "") $a_code = md5(uniqid(rand()));
632 
633  if (strlen($a_code) > $a_length)
634  {
635  $a_code = substr($a_code, 0, $a_length);
636  }
637 
638  return $a_code;
639  }
640 
641  function generateCodes($view = "choice")
642  {
643  $this->coupon_obj->setId($_GET['coupon_id']);
644 
645  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
646  $this->__showButtons();
647 
648  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_codes_generate.html','payment');
649 
650  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
651 
652  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
653  $this->tpl->setVariable("TXT_HEADLINE", $this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_code_generation'));
654 
655  if ($view == "choice")
656  {
657  $this->tpl->setCurrentBlock("code_choice");
658 
659  $this->tpl->setVariable("TXT_TYPE", $this->lng->txt('paya_coupons_generate_codes'));
660  $this->tpl->setVariable("TXT_AUTO", $this->lng->txt('paya_coupons_type_auto'));
661  $this->tpl->setVariable("TXT_SELF", $this->lng->txt('paya_coupons_type_self'));
662  $this->tpl->setVariable("TXT_NUMBER_OF_CODES", $this->lng->txt('paya_coupons_number_of_codes'));
663  $this->tpl->setVariable("TXT_LENGTH", $this->lng->txt('paya_coupons_code_length'));
664  $this->tpl->setVariable("TXT_CHARS", $this->lng->txt('paya_coupons_code_chars'));
665 
666  $this->tpl->setVariable("TYPE_".strtoupper(isset($_POST["generate"]["type"]) ? $_POST["generate"]["type"] : "auto"), " checked=\"checked\"");
667  $this->tpl->setVariable("LENGTH", ilUtil::prepareFormOutput($_POST["generate"]["length"] ? $_POST["generate"]["length"] : 12, true));
668 
669  $this->tpl->setVariable("TXT_REQUIRED_FIELDS", $this->lng->txt("required_field"));
670 
671  $this->tpl->parseCurrentBlock();
672  }
673  else if ($view == "input")
674  {
675  $this->tpl->setCurrentBlock("code_input");
676 
677  if (is_numeric($_POST["generate"]["number"]) && $_POST["generate"]["number"] > 0)
678  {
679  for ($i = 0; $i < $_POST["generate"]["number"]; $i++)
680  {
681  $this->tpl->setCurrentBlock("loop");
682  $this->tpl->setVariable("LOOP_CODE_INDEX", $i + 1);
683  $this->tpl->parseCurrentBlock();
684  }
685  }
686 
687  $this->tpl->parseCurrentBlock();
688  }
689 
690  $this->tpl->setVariable("TXT_SEND",$this->lng->txt('send'));
691 
692  $this->tpl->setVariable("IMPORT_ACTION", $this->ctrl->getFormAction($this));
693  $this->tpl->setVariable("TXT_IMPORT",$this->lng->txt('import'));
694 
695  return true;
696  }
697 
698  function assignObjects()
699  {
700  if (is_array($_POST['objects']))
701  {
702  $this->coupon_obj->setId($_GET["coupon_id"]);
703  foreach($_POST['objects'] as $id)
704  {
705  if ($id)
706  {
707  $this->coupon_obj->assignObjectToCoupon($id);
708  }
709  }
710  }
711 
712  $this->showObjects();
713 
714  return true;
715  }
716 
717  function unassignObjects()
718  {
719  if (is_array($_POST['objects']))
720  {
721  $this->coupon_obj->setId($_GET["coupon_id"]);
722  foreach($_POST['objects'] as $id)
723  {
724  if ($id)
725  {
726  $this->coupon_obj->unassignObjectFromCoupon($id);
727  }
728  }
729  }
730 
731  $this->showObjects();
732 
733  return true;
734  }
735 
736  function showObjects()
737  {
738  $this->coupon_obj->setId($_GET['coupon_id']);
739 
740  $this->__initPaymentObject();
741 
742  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
743  $this->__showButtons();
744 
745  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_objects.html','payment');
746 
747  $objects = $this->pobject->_getObjectsData($this->user_obj->getId());
748 
749  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
750 
751  $counter_assigned = 0;
752  $counter_unassigned = 0;
753  $f_result_assigned = array();
754  $f_result_unassigned = array();
755  foreach($objects as $data)
756  {
757  if ($this->coupon_obj->isObjectAssignedToCoupon($data['ref_id']))
758  {
759  $p_counter =& $counter_assigned;
760  $p_result =& $f_result_assigned;
761  }
762  else
763  {
764  $p_counter =& $counter_unassigned;
765  $p_result =& $f_result_unassigned;
766  }
767 
768  $tmp_obj =& ilObjectFactory::getInstanceByRefId($data['ref_id'], false);
769 
770  $p_result[$p_counter][] = ilUtil::formCheckbox(0, 'objects[]', $data['ref_id']);
771  if($tmp_obj)
772  {
773  $p_result[$p_counter][] = $tmp_obj->getTitle();
774  }
775  else
776  {
777  $p_result[$p_counter][] = $this->lng->txt('object_not_found');
778  }
779 
780  switch($data['status'])
781  {
782  case $this->pobject->STATUS_BUYABLE:
783  $p_result[$p_counter][] = $this->lng->txt('paya_buyable');
784  break;
785 
786  case $this->pobject->STATUS_NOT_BUYABLE:
787  $p_result[$p_counter][] = $this->lng->txt('paya_not_buyable');
788  break;
789 
790  case $this->pobject->STATUS_EXPIRES:
791  $p_result[$p_counter][] = $this->lng->txt('paya_expires');
792  break;
793  }
794  switch($data['pay_method'])
795  {
796  case $this->pobject->PAY_METHOD_NOT_SPECIFIED:
797  $p_result[$p_counter][] = $this->lng->txt('paya_pay_method_not_specified');
798  break;
799 
800  case $this->pobject->PAY_METHOD_BILL:
801  $p_result[$p_counter][] = $this->lng->txt('pays_bill');
802  break;
803 
804  case $this->pobject->PAY_METHOD_BMF:
805  $p_result[$p_counter][] = $this->lng->txt('pays_bmf');
806  break;
807 
808  case $this->pobject->PAY_METHOD_PAYPAL:
809  $p_result[$p_counter][] = $this->lng->txt('pays_paypal');
810  break;
811  }
812 
813  ++$p_counter;
814 
815  unset($tmp_obj);
816  }
817 
818  $this->ctrl->setParameter($this, "cmd", "showObjects");
819 
820  if (count($f_result_assigned) > 0)
821  {
822  $tbl =& $this->initTableGUI();
823  $tpl =& $tbl->getTemplateObject();
824  $tbl->setPrefix('assigned');
825 
826  $tpl->setCurrentBlock("tbl_form_header");
827  $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
828  $tpl->parseCurrentBlock();
829  $tbl->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_assigned_objects"),"icon_pays.gif",$this->lng->txt("paya_coupons_assigned_objects"));
830  $tbl->setHeaderNames(array("",
831  $this->lng->txt("title"),
832  $this->lng->txt("status"),
833  $this->lng->txt("paya_pay_method")));
834  $header_params = $this->ctrl->getParameterArray($this,'');
835  $tbl->setHeaderVars(array("",
836  "title",
837  "status",
838  "pay_method"),$header_params);
839  $offset = $_GET['assignedoffset'];
840  $order = $_GET['assignedsort_by'];
841  $direction = $_GET['assignedsort_order'] ? $_GET['assignedsort_order'] : 'desc';
842  $tbl->setOrderColumn($order,'title');
843  $tbl->setOrderDirection($direction);
844  $tbl->setOffset($offset);
845  $tbl->setLimit($_GET['limit']);
846  $tbl->setMaxCount(count($f_result_assigned));
847  $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
848  $tbl->setData($f_result_assigned);
849  $tpl->setVariable('COLUMN_COUNTS', 4);
850  $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
851  $tpl->setCurrentBlock("tbl_action_button");
852  $tpl->setVariable("BTN_NAME","unassignObjects");
853  $tpl->setVariable("BTN_VALUE",$this->lng->txt("remove"));
854  $tpl->parseCurrentBlock();
855  $tbl->setColumnWidth(array("10%","20%","20%","20%"));
856  $tbl->render();
857 
858  $this->tpl->setVariable('LINKED_OBJECTS_TABLE', $tbl->tpl->get());
859  }
860 
861  if (count($f_result_unassigned) > 0)
862  {
863  $tbl =& $this->initTableGUI();
864  $tpl =& $tbl->getTemplateObject();
865  $tpl->setCurrentBlock("tbl_form_header");
866  $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
867  $tpl->parseCurrentBlock();
868  $tbl->setTitle($this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt("paya_coupons_unassigned_objects"),"icon_pays.gif",$this->lng->txt("paya_coupons_unassigned_objects"));
869  $tbl->setHeaderNames(array("",
870  $this->lng->txt("title"),
871  $this->lng->txt("status"),
872  $this->lng->txt("paya_pay_method")));
873  $header_params = $this->ctrl->getParameterArray($this,'');
874  $tbl->setHeaderVars(array("",
875  "title_ua",
876  "status_ua",
877  "pay_method_ua"),$header_params);
878  $offset = $_GET['offset'];
879  $order = $_GET['sort_by'];
880  $direction = $_GET['sort_order'] ? $_GET['sort_order'] : 'desc';
881  $tbl->setOrderColumn($order,'title_ua');
882  $tbl->setOrderDirection($direction);
883  $tbl->setOffset($offset);
884  $tbl->setLimit($_GET['limit']);
885  $tbl->setMaxCount(count($f_result_unassigned));
886  $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
887  $tbl->setData($f_result_unassigned);
888  $tpl->setVariable('COLUMN_COUNTS', 4);
889  $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
890  $tpl->setCurrentBlock("tbl_action_button");
891  $tpl->setVariable("BTN_NAME","assignObjects");
892  $tpl->setVariable("BTN_VALUE",$this->lng->txt("add"));
893  $tpl->parseCurrentBlock();
894  $tbl->setColumnWidth(array("10%","20%","20%","20%"));
895  $tbl->render();
896 
897  $this->tpl->setVariable('UNLINKED_OBJECTS_TABLE', $tbl->tpl->get());
898  }
899 
900  return true;
901  }
902 
903  function importCodes()
904  {
905  include_once('./Services/Utilities/classes/class.ilCSVReader.php');
906 
907  if ($_FILES["codesfile"]["tmp_name"] != "")
908  {
909  $csv = new ilCSVReader();
910  $csv->setDelimiter("");
911 
912  if ($csv->open($_FILES["codesfile"]["tmp_name"]))
913  {
914  $data = $csv->getDataArrayFromCSVFile();
915 
916  for ($i = 0; $i < count($data); $i++)
917  {
918  for ($j = 0; $j < count($data[$i]); $j++)
919  {
920  $this->coupon_obj->addCode(ilUtil::stripSlashes($data[$i][$j]), $_GET["coupon_id"]);
921  }
922  }
923 
924  ilUtil::sendInfo($this->lng->txt("paya_coupon_codes_import_successful"));
925 
926  $this->showCodes();
927  }
928  else
929  {
930  ilUtil::sendInfo($this->lng->txt("paya_coupons_import_error_opening_file"));
931 
932  $this->showCodeImport();
933  }
934  }
935  else
936  {
937  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
938 
939  $this->showCodeImport();
940  }
941 
942  return true;
943  }
944 
945  function showCodeImport()
946  {
947  $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
948  $this->__showButtons();
949 
950  $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
951 
952  $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_codes_import.html','payment');
953 
954  $this->tpl->setVariable('TXT_IMPORT_HINT', $this->lng->txt('import_use_only_textfile'));
955 
956  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
957  $this->tpl->setVariable("TXT_HEADLINE", $this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_codes_import'));
958 
959  $this->tpl->setVariable("TXT_FILE",$this->lng->txt('file'));
960 
961  $this->tpl->setVariable("TXT_UPLOAD",$this->lng->txt('upload'));
962  $this->tpl->setVariable("TXT_REQUIRED_FIELDS", $this->lng->txt("required_field"));
963 
964  return true;
965  }
966 
967  function __showButtons()
968  {
969  $this->showButton('addCoupon', $this->lng->txt('paya_coupons_edit'));
970  $this->showButton('showCodes', $this->lng->txt('paya_coupons_edit_codes'));
971  $this->showButton('showObjects', $this->lng->txt('paya_coupons_edit_objects'));
972 
973  return true;
974  }
975 
976  function __initPaymentObject($a_pobject_id = 0)
977  {
978  include_once './payment/classes/class.ilPaymentObject.php';
979 
980  $this->pobject =& new ilPaymentObject($this->user_obj, $a_pobject_id);
981 
982  return true;
983  }
984 
986  {
987  include_once './payment/classes/class.ilPaymentCoupons.php';
988 
989  $this->coupon_obj =& new ilPaymentCoupons($this->user_obj, true);
990 
991  return true;
992  }
993 }
994 ?>