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