00001 <?php
00002 class ilPaymentCouponGUI extends ilPaymentBaseGUI
00003 {
00004 var $ctrl;
00005
00006 var $lng;
00007
00008 var $user_obj = null;
00009 var $coupon_obj = null;
00010 var $pobject = null;
00011
00012 function ilPaymentCouponGUI(&$user_obj)
00013 {
00014 global $ilCtrl;
00015
00016 $this->ctrl =& $ilCtrl;
00017 $this->ctrl->saveParameter($this, 'baseClass');
00018
00019 $this->ilPaymentBaseGUI();
00020
00021 $this->user_obj =& $user_obj;
00022
00023 $this->__initCouponObject();
00024 }
00025
00029 function &executeCommand()
00030 {
00031 global $tree;
00032
00033 $cmd = $this->ctrl->getCmd();
00034 switch ($this->ctrl->getNextClass($this))
00035 {
00036 default:
00037 if (!$cmd = $this->ctrl->getCmd())
00038 {
00039 $cmd = 'showCoupons';
00040 }
00041
00042 $this->$cmd();
00043 break;
00044 }
00045 }
00046
00047 function resetFilter()
00048 {
00049 unset($_POST['title_type']);
00050 unset($_POST['title_value']);
00051 unset($_POST['type']);
00052 unset($_POST['from']['d']);
00053 unset($_POST['from']['m']);
00054 unset($_POST['from']['y']);
00055 unset($_POST['till']['d']);
00056 unset($_POST['till']['m']);
00057 unset($_POST['till']['y']);
00058 unset($_POST['from_enabled']);
00059 unset($_POST['till_enabled']);
00060
00061 $this->showCoupons();
00062
00063 return true;
00064 }
00065
00066 function showCoupons()
00067 {
00068 include_once("Services/User/classes/class.ilObjUser.php");
00069
00070 $this->showButton('addCoupon', $this->lng->txt('paya_coupons_add'));
00071
00072 if ($_POST['updateView'] == '1')
00073 {
00074 $_SESSION['pay_coupons']['title_type'] = $_POST['title_type'];
00075 $_SESSION['pay_coupons']['title_value'] = $_POST['title_value'];
00076 $_SESSION['pay_coupons']['type'] = $_POST['type'];
00077 $_SESSION['pay_coupons']['from']['d'] = $_POST['from']['d'];
00078 $_SESSION['pay_coupons']['from']['m'] = $_POST['from']['m'];
00079 $_SESSION['pay_coupons']['from']['y'] = $_POST['from']['y'];
00080 $_SESSION['pay_coupons']['till']['d'] = $_POST['till']['d'];
00081 $_SESSION['pay_coupons']['till']['m'] = $_POST['till']['m'];
00082 $_SESSION['pay_coupons']['till']['y'] = $_POST['till']['y'];
00083 $_SESSION['pay_coupons']['from_enabled'] = $_POST['from_enabled'];
00084 $_SESSION['pay_coupons']['till_enabled'] = $_POST['till_enabled'];
00085 }
00086
00087 $this->coupon_obj->setSearchTitleType(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_type']));
00088 $this->coupon_obj->setSearchTitleValue(ilUtil::stripSlashes($_SESSION['pay_coupons']['title_value']));
00089 $this->coupon_obj->setSearchType(ilUtil::stripSlashes($_SESSION['pay_coupons']['type']));
00090 $this->coupon_obj->setSearchFromDay(ilUtil::stripSlashes($_SESSION['pay_coupons']['from']['d']));
00091 $this->coupon_obj->setSearchFromMonth(ilUtil::stripSlashes($_SESSION['pay_coupons']['from']['m']));
00092 $this->coupon_obj->setSearchFromYear(ilUtil::stripSlashes($_SESSION['pay_coupons']['from']['y']));
00093 $this->coupon_obj->setSearchTillDay(ilUtil::stripSlashes($_SESSION['pay_coupons']['till']['d']));
00094 $this->coupon_obj->setSearchTillMonth(ilUtil::stripSlashes($_SESSION['pay_coupons']['till']['m']));
00095 $this->coupon_obj->setSearchTillYear(ilUtil::stripSlashes($_SESSION['pay_coupons']['till']['y']));
00096 $this->coupon_obj->setSearchFromDateEnabled(ilUtil::stripSlashes($_SESSION['pay_coupons']['from_enabled']));
00097 $this->coupon_obj->setSearchTillDateEnabled(ilUtil::stripSlashes($_SESSION['pay_coupons']['till_enabled']));
00098
00099
00100 $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paya_coupons.html', 'payment');
00101
00102 $this->tpl->setVariable("TXT_FILTER", $this->lng->txt('pay_filter'));
00103 $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
00104
00105 $this->tpl->setVariable("TXT_COUPON",$this->lng->txt('paya_coupons_title'));
00106 $this->tpl->setVariable("TXT_STARTING",$this->lng->txt('pay_starting'));
00107 $this->tpl->setVariable("TXT_ENDING",$this->lng->txt('pay_ending'));
00108 $this->tpl->setVariable("TXT_TYPE",$this->lng->txt('paya_coupons_type'));
00109 $this->tpl->setVariable("TXT_TYPE_FIX",$this->lng->txt('paya_coupons_fix'));
00110 $this->tpl->setVariable("TXT_TYPE_PERCENTAGED",$this->lng->txt('paya_coupons_percentaged'));
00111 $this->tpl->setVariable("TXT_VALID_DATE_FROM",$this->lng->txt('paya_coupons_from'));
00112 $this->tpl->setVariable("TXT_VALID_DATE_TILL",$this->lng->txt('paya_coupons_till'));
00113 $this->tpl->setVariable("TXT_UPDATE_VIEW",$this->lng->txt('pay_update_view'));
00114 $this->tpl->setVariable("TXT_RESET_FILTER",$this->lng->txt('pay_reset_filter'));
00115 $this->tpl->setVariable('TXT_ENABLED', $this->lng->txt('enabled'));
00116 $this->tpl->setVariable("TITLE_TYPE_" . $this->coupon_obj->getSearchTitleType(), " selected=\"selected\"");
00117 $this->tpl->setVariable("TITLE_VALUE", ilUtil::prepareFormOutput($this->coupon_obj->getSearchTitleValue(), true));
00118 $this->tpl->setVariable("TYPE_" . strtoupper($this->coupon_obj->getSearchType()), " selected=\"selected\"");
00119
00120
00121 if ($this->coupon_obj->getSearchFromDateEnabled()) $this->tpl->setVariable('CHECKED_STARTING_DATE', " checked=\"checked\"");
00122 if ($this->coupon_obj->getSearchTillDateEnabled()) $this->tpl->setVariable('CHECKED_ENDING_DATE', " checked=\"checked\"");
00123 $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)));
00124 $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)));
00125
00126 if (!count($coupons = $this->coupon_obj->getCoupons()))
00127 {
00128 ilUtil::sendInfo($this->lng->txt('paya_coupons_not_found'));
00129
00130
00131
00132 return true;
00133 }
00134
00135 $counter = 0;
00136 foreach ($coupons as $coupon)
00137 {
00138 $f_result[$counter][] = $coupon['pc_title'];
00139 $f_result[$counter][] = $coupon['number_of_codes'];
00140 $f_result[$counter][] = $coupon['usage_of_codes'];
00141
00142 if (!empty($coupon['objects']))
00143 {
00144 $objects = "";
00145 for ($i = 0; $i < count($coupon['objects']); $i++)
00146 {
00147 $tmp_obj =& ilObjectFactory::getInstanceByRefId($coupon['objects'][$i]);
00148 $objects .= $tmp_obj->getTitle();
00149
00150 if ($i < count($coupon['objects']) - 1) $objects .= "<br />";
00151
00152 unset($tmp_obj);
00153 }
00154 }
00155 else
00156 {
00157 $objects = "";
00158 }
00159
00160 $f_result[$counter][] = $objects;
00161
00162
00163 $f_result[$counter][] = ($coupon['pc_from'] != '0000-00-00' && $coupon['pc_from_enabled'] == '1') ? ilFormat::formatDate($coupon['pc_from'], 'date') : '';
00164 $f_result[$counter][] = ($coupon['pc_till'] != '0000-00-00' && $coupon['pc_till_enabled'] == '1') ? ilFormat::formatDate($coupon['pc_till'], 'date') : '';
00165 $f_result[$counter][] =
00166 ($coupon['pc_last_changed'] != '0000-00-00 00:00:00' ? ilFormat::formatDate($coupon['pc_last_changed']) : '') .
00167 ($coupon['pc_last_change_usr_id'] != '0' ? "[" . ilObjUser::_lookupLogin($coupon['pc_last_change_usr_id']) . "]" : '');
00168 $this->ctrl->setParameter($this, 'coupon_id', $coupon['pc_pk']);
00169 $f_result[$counter][] = "<div class=\"il_ContainerItemCommands\"><a class=\"il_ContainerItemCommand\" href=\"".$this->ctrl->getLinkTarget($this, "addCoupon")."\">".$this->lng->txt("edit")."</a></div>";
00170
00171 ++$counter;
00172 }
00173
00174 return $this->__showCouponsTable($f_result);
00175 }
00176
00177 function __showCouponsTable($f_result)
00178 {
00179 $tbl =& $this->initTableGUI();
00180 $tpl =& $tbl->getTemplateObject();
00181
00182 $tpl->setCurrentBlock("tbl_form_header");
00183
00184 $tpl->setVariable("FORMACTION",$this->ctrl->getFormAction($this));
00185 $tpl->parseCurrentBlock();
00186
00187 $tbl->setTitle($this->lng->txt("paya_coupons_coupons"), "icon_pays.gif", $this->lng->txt("paya_coupons_coupons"));
00188 $tbl->setHeaderNames(array($this->lng->txt("paya_coupons_title"),
00189 $this->lng->txt("paya_coupons_number_of_codes"),
00190 $this->lng->txt("paya_coupons_usage_of_codes"),
00191 $this->lng->txt("paya_coupons_objects"),
00192 $this->lng->txt("paya_coupons_from"),
00193 $this->lng->txt("paya_coupons_till"),
00194 $this->lng->txt("last_change"), ''));
00195 $header_params = $this->ctrl->getParameterArray($this, '');
00196 $tbl->setHeaderVars(array('pc_title',
00197 'number_of_codes',
00198 'usage_of_codes',
00199 'objects',
00200 'pc_from',
00201 'pc_till',
00202 'last_changed'),
00203 $header_params
00204 );
00205 $offset = $_GET['offset'];
00206 $order = $_GET['sort_by'];
00207 $direction = $_GET['sort_order'] ? $_GET['sort_order'] : 'desc';
00208 $tbl->setOrderColumn($order,'pc_title');
00209 $tbl->setOrderDirection($direction);
00210 $tbl->setOffset($offset);
00211 $tbl->setLimit($_GET['limit']);
00212 $tbl->setMaxCount(count($f_result));
00213 $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
00214 $tbl->setData($f_result);
00215 $tbl->render();
00216
00217 $this->tpl->setVariable('COUPONS_TABLE', $tbl->tpl->get());
00218
00219 return true;
00220 }
00221
00222 function saveCouponForm()
00223 {
00224 $this->error = '';
00225
00226 if ($_POST['title'] == '') $this->error .= 'paya_coupons_title,';
00227 if ($_POST['type'] == '') $this->error .= 'paya_coupons_type,';
00228 if ($_POST['value'] == '') $this->error .= 'paya_coupons_value,';
00229 else $_POST['value'] = ilFormat::checkDecimal($_POST['value']);
00230
00231 $this->coupon_obj->setId($_GET['coupon_id']);
00232 $this->coupon_obj->setCouponUser($this->user_obj->getId());
00233 $this->coupon_obj->setTitle(ilUtil::stripSlashes($_POST['title']));
00234 $this->coupon_obj->setDescription(ilUtil::stripSlashes($_POST['description']));
00235 $this->coupon_obj->setType(ilUtil::stripSlashes($_POST['type']));
00236 $this->coupon_obj->setValue(ilUtil::stripSlashes($_POST['value']));
00237 $this->coupon_obj->setFromDate(ilUtil::stripSlashes($_POST['from']['y']."-".$_POST['from']['m']."-".$_POST['from']['d']));
00238 $this->coupon_obj->setTillDate(ilUtil::stripSlashes($_POST['till']['y']."-".$_POST['till']['m']."-".$_POST['till']['d']));
00239 $this->coupon_obj->setFromDateEnabled(ilUtil::stripSlashes($_POST['pc_from_enabled']));
00240 $this->coupon_obj->setTillDateEnabled(ilUtil::stripSlashes($_POST['pc_till_enabled']));
00241 $this->coupon_obj->setUses(ilUtil::stripSlashes($_POST['usage']));
00242 $this->coupon_obj->setChangeDate(date('Y-m-d H:i:s'));
00243
00244 if ($this->error == '')
00245 {
00246 if ($_GET['coupon_id'] != "")
00247 {
00248 $this->coupon_obj->update();
00249 }
00250 else
00251 {
00252 $_GET['coupon_id'] = $this->coupon_obj->add();
00253 }
00254
00255 ilUtil::sendInfo($this->lng->txt('saved_successfully'));
00256 }
00257 else
00258 {
00259 if (is_array($e = explode(',', $this->error)))
00260 {
00261 $mandatory = '';
00262 for ($i = 0; $i < count($e); $i++)
00263 {
00264 $e[$i] = trim($e[$i]);
00265 if ($e[$i] != '')
00266 {
00267 $mandatory .= $this->lng->txt($e[$i]);
00268 if (array_key_exists($i + 1, $e) && $e[$i + 1] != '') $mandatory .= ', ';
00269 }
00270 }
00271 ilUtil::sendInfo($this->lng->txt('fill_out_all_required_fields') . ': ' . $mandatory);
00272 }
00273 }
00274
00275 $this->addCoupon();
00276
00277 return true;
00278 }
00279
00280 function addCoupon()
00281 {
00282 $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_add.html','payment');
00283
00284 if (isset($_GET['coupon_id']))
00285 {
00286 if ($this->error == '') $this->coupon_obj->getCouponById($_GET['coupon_id']);
00287
00288 $this->ctrl->setParameter($this, 'coupon_id', $this->coupon_obj->getId());
00289
00290 $this->__showButtons();
00291 }
00292
00293 $this->tpl->setVariable('FORMACTION', $this->ctrl->getFormAction($this));
00294
00295 $this->tpl->setVariable('TXT_HEADLINE', ($this->coupon_obj->getId() != '' ? $this->lng->txt('paya_coupons_edit') : $this->lng->txt('paya_coupons_add')));
00296 $this->tpl->setVariable('TXT_TITLE', $this->lng->txt('paya_coupons_title'));
00297 $this->tpl->setVariable('TXT_DESCRIPTION', $this->lng->txt('paya_coupons_description'));
00298 $this->tpl->setVariable('TXT_TYPE', $this->lng->txt('paya_coupons_type'));
00299 $this->tpl->setVariable('TXT_FIX', $this->lng->txt('paya_coupons_fix'));
00300 $this->tpl->setVariable('TXT_PERCENTAGED', $this->lng->txt('paya_coupons_percentaged'));
00301 $this->tpl->setVariable('TXT_VALUE', $this->lng->txt('paya_coupons_value'));
00302 $this->tpl->setVariable('TXT_ENABLED', $this->lng->txt('enabled'));
00303 $this->tpl->setVariable('TXT_VALID_FROM', $this->lng->txt('paya_coupons_from'));
00304 $this->tpl->setVariable('TXT_VADID_TILL', $this->lng->txt('paya_coupons_till'));
00305 $this->tpl->setVariable('TXT_USAGE', $this->lng->txt('paya_coupons_availability'));
00306
00307 $this->tpl->setVariable('TITLE', ilUtil::prepareFormOutput($this->coupon_obj->getTitle() , true));
00308 $this->tpl->setVariable('DESCRIPTION', ilUtil::prepareFormOutput($this->coupon_obj->getDescription(), true));
00309 $this->tpl->setVariable('TYPE_' . strtoupper($this->coupon_obj->getType()), " selected=\"selected\"");
00310 $this->tpl->setVariable('VALUE', ilUtil::prepareFormOutput($this->coupon_obj->getValue(), true));
00311 $this->tpl->setVariable('USAGE', ilUtil::prepareFormOutput($this->coupon_obj->getUses(), true));
00312
00313 $from_date = explode('-', $this->coupon_obj->getFromDate());
00314 $from_day = $from_date[2] != '00' ? $from_date[2] : '';
00315 $from_month = $from_date[1] != '00' ? $from_date[1] : '';
00316 $from_year = $from_date[0] != '0000' ? $from_date[0] : '';
00317
00318 $till_date = explode('-', $this->coupon_obj->getTillDate());
00319 $till_day = $till_date[2] != '00' ? $till_date[2] : '';
00320 $till_month = $till_date[1] != '00' ? $till_date[1] : '';
00321 $till_year = $till_date[0] != '0000' ? $till_date[0] : '';
00322
00323 if ($this->coupon_obj->getFromDateEnabled()) $this->tpl->setVariable('CHECKED_STARTING_DATE', " checked=\"checked\"");
00324 if ($this->coupon_obj->getTillDateEnabled()) $this->tpl->setVariable('CHECKED_ENDING_DATE', " checked=\"checked\"");
00325
00326 $this->tpl->setVariable('DATE_FROM', ilUtil::makeDateSelect("from", $from_year, $from_month, $from_day, 2004));
00327 $this->tpl->setVariable('DATE_TILL', ilUtil::makeDateSelect("till", $till_year, $till_month, $till_day, 2004));
00328
00329 $this->tpl->setVariable('TXT_REQUIRED_FIELDS', $this->lng->txt('required_field'));
00330 $this->tpl->setVariable('TXT_SAVE',$this->lng->txt('save'));
00331 $this->tpl->setVariable('TXT_CANCEL',$this->lng->txt('cancel'));
00332 $this->tpl->setVariable('COUPONS', 'showCoupons');
00333 }
00334
00335 function deleteAllCodes()
00336 {
00337 $this->showCodes("all");
00338
00339 return true;
00340 }
00341
00342 function performDeleteAllCodes()
00343 {
00344 $this->coupon_obj->deleteAllCodesByCouponId($_GET['coupon_id']);
00345
00346 $this->showCodes();
00347
00348 return true;
00349 }
00350
00351 function deleteCodes()
00352 {
00353 $_SESSION['paya_delete_codes'] = $_POST['codes'];
00354
00355 if (!is_array($_POST['codes']))
00356 {
00357 ilUtil::sendInfo($this->lng->txt('paya_coupons_no_codes_selected'));
00358
00359 $this->showCodes();
00360
00361 return true;
00362 }
00363
00364 $this->showCodes("selected");
00365
00366 return true;
00367 }
00368
00369 function performDeleteCodes()
00370 {
00371 if (is_array($_SESSION['paya_delete_codes']))
00372 {
00373 foreach($_SESSION['paya_delete_codes'] as $id)
00374 {
00375 $this->coupon_obj->deleteCode($id);
00376 }
00377 }
00378 unset($_SESSION['paya_delete_codes']);
00379 ilUtil::sendInfo($this->lng->txt('paya_coupons_code_deleted_successfully'));
00380
00381 $this->showCodes();
00382
00383 return true;
00384 }
00385
00386 function cancelDelete()
00387 {
00388 unset($_SESSION['paya_delete_codes']);
00389
00390 $this->showCodes();
00391
00392 return true;
00393 }
00394
00395 function showCodes($a_show_delete = "")
00396 {
00397 $this->coupon_obj->setId($_GET['coupon_id']);
00398
00399 if (!count($codes = $this->coupon_obj->getCodesByCouponId($_GET['coupon_id'])))
00400 {
00401 ilUtil::sendInfo($this->lng->txt('paya_coupons_codes_not_available'));
00402
00403 $this->generateCodes();
00404
00405 return true;
00406 }
00407
00408 $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
00409
00410 $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
00411 $this->__showButtons();
00412
00413 $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_codes.html','payment');
00414
00415 if ($a_show_delete == "all")
00416 {
00417 ilUtil::sendInfo($this->lng->txt('paya_coupons_sure_delete_all_codes'));
00418 $this->tpl->setCurrentBlock("confirm_delete_all");
00419 $this->tpl->setVariable("CONFIRM_ALL_FORMACTION",$this->ctrl->getFormAction($this));
00420 $this->tpl->setVariable("TXT_CANCEL_ALL",$this->lng->txt('cancel'));
00421 $this->tpl->setVariable("TXT_CONFIRM_ALL",$this->lng->txt('delete_all'));
00422 $this->tpl->parseCurrentBlock();
00423 }
00424 if ($a_show_delete == "selected")
00425 {
00426 ilUtil::sendInfo($this->lng->txt('paya_coupons_sure_delete_selected_codes'));
00427 $this->tpl->setCurrentBlock("confirm_delete");
00428 $this->tpl->setVariable("CONFIRM_FORMACTION",$this->ctrl->getFormAction($this));
00429 $this->tpl->setVariable("TXT_CANCEL",$this->lng->txt('cancel'));
00430 $this->tpl->setVariable("TXT_CONFIRM",$this->lng->txt('delete'));
00431 $this->tpl->parseCurrentBlock();
00432 }
00433
00434 $_SESSION['paya_delete_codes'] = $_SESSION['paya_delete_codes'] ? $_SESSION['paya_delete_codes'] : array();
00435
00436 $counter = 0;
00437 foreach ($codes as $code)
00438 {
00439 $f_result[$counter][] = ilUtil::formCheckbox(in_array($code['pcc_pk'], $_SESSION['paya_delete_codes']) ? 1 : 0,
00440 "codes[]",
00441 $code['pcc_pk']);
00442 $f_result[$counter][] = $code['pcc_code'];
00443 $f_result[$counter][] = $code['pcc_used']." ".strtolower($this->lng->txt('of'))." ".$this->coupon_obj->getUses();
00444
00445 ++$counter;
00446 }
00447
00448 $tbl =& $this->initTableGUI();
00449 $tpl =& $tbl->getTemplateObject();
00450
00451
00452
00453 $tpl->setCurrentBlock("tbl_form_header");
00454 $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
00455 $tpl->parseCurrentBlock();
00456 $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"));
00457 $tbl->setHeaderNames(array('', $this->lng->txt("paya_coupons_code"), $this->lng->txt('paya_coupons_usage_of_codes')));
00458 $this->ctrl->setParameter($this, "cmd", "showCodes");
00459 $header_params = $this->ctrl->getParameterArray($this, '');
00460 $tbl->setHeaderVars(array('', 'pcc_code', 'paya_coupons_usage_of_codes'), $header_params);
00461 $offset = $_GET['offset'];
00462 $order = $_GET['sort_by'];
00463 $direction = $_GET['sort_order'] ? $_GET['sort_order'] : 'desc';
00464 $tbl->setOrderColumn($order,'pcc_code');
00465 $tbl->setOrderDirection($direction);
00466 $tbl->setOffset($offset);
00467 $tbl->setLimit($_GET['limit']);
00468 $tbl->setMaxCount(count($f_result));
00469 $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
00470 $tbl->setData($f_result);
00471 $tpl->setVariable('COLUMN_COUNTS', 3);
00472
00473 $tbl->enable('select_all');
00474 $tbl->setFormName('cmd');
00475 $tbl->setSelectAllCheckbox('codes');
00476
00477 $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00478
00479 $tpl->setCurrentBlock("tbl_action_button");
00480 $tpl->setVariable("BTN_NAME","deleteCodes");
00481 $tpl->setVariable("BTN_VALUE",$this->lng->txt("delete"));
00482 $tpl->parseCurrentBlock();
00483
00484 $tpl->setCurrentBlock('plain_button');
00485 $tpl->setVariable('PBTN_NAME', 'generateCodes');
00486 $tpl->setVariable('PBTN_VALUE', $this->lng->txt('paya_coupons_generate_codes'));
00487 $tpl->parseCurrentBlock();
00488
00489 $tpl->setCurrentBlock('plain_button');
00490 $tpl->setVariable('PBTN_NAME', 'deleteAllCodes');
00491 $tpl->setVariable('PBTN_VALUE', $this->lng->txt('delete_all'));
00492 $tpl->parseCurrentBlock();
00493
00494 $tpl->setCurrentBlock("plain_button");
00495 $tpl->setVariable("PBTN_NAME","exportCodes");
00496 $tpl->setVariable("PBTN_VALUE",$this->lng->txt("export"));
00497 $tpl->parseCurrentBlock();
00498
00499 $tbl->setColumnWidth(array("10%","90%"));
00500 $tbl->render();
00501
00502 $this->tpl->setVariable('CODES_TABLE', $tbl->tpl->get());
00503
00504 return true;
00505 }
00506
00507 function exportCodes()
00508 {
00509 $codes = $this->coupon_obj->getCodesByCouponId($_GET["coupon_id"]);
00510
00511 if (is_array($codes))
00512 {
00513 include_once('./Services/Utilities/classes/class.ilCSVWriter.php');
00514
00515 $csv = new ilCSVWriter();
00516 $csv->setDelimiter("");
00517
00518 foreach($codes as $data)
00519 {
00520 if ($data["pcc_code"])
00521 {
00522 $csv->addColumn($data["pcc_code"]);
00523 $csv->addRow();
00524 }
00525 }
00526
00527 ilUtil::deliverData($csv->getCSVString(), "code_export_".date("Ymdhis").".csv");
00528 }
00529
00530 $this->showCodes();
00531
00532 return true;
00533 }
00534
00535 function saveCodeForm()
00536 {
00537 if (isset($_POST["generate"]["length"])) $_SESSION["pay_coupons"]["code_length"] = $_POST["generate"]["length"];
00538 else $_POST["generate"]["length"] = $_SESSION["pay_coupons"]["code_length"];
00539
00540 if (isset($_POST["generate"]["type"])) $_SESSION["pay_coupons"]["code_type"] = $_POST["generate"]["type"];
00541 else $_POST["generate"]["type"] = $_SESSION["pay_coupons"]["code_type"];
00542
00543 if ($_POST["generate"]["type"] == "self")
00544 {
00545 if ($_GET["coupon_id"] && is_array($_POST["codes"]))
00546 {
00547 $count_inserts = 0;
00548
00549 for ($i = 0; $i < count($_POST["codes"]); $i++)
00550 {
00551 $_POST["codes"][$i] = trim($_POST["codes"][$i]);
00552
00553 if ($_POST["codes"][$i] != "")
00554 {
00555 $code = $this->__makeCode($_POST["codes"][$i], $_SESSION["pay_coupons"]["code_length"]);
00556
00557 if ($code != "")
00558 {
00559 if ($this->coupon_obj->addCode(ilUtil::stripSlashes($code), $_GET["coupon_id"]))
00560 {
00561 ++$count_inserts;
00562 }
00563 }
00564 }
00565 }
00566
00567 if ($count_inserts)
00568 {
00569 ilUtil::sendInfo($this->lng->txt("saved_successfully"));
00570 $this->showCodes();
00571 }
00572 else
00573 {
00574 ilUtil::sendInfo($this->lng->txt("paya_coupons_no_codes_generated"));
00575 $this->generateCodes();
00576 }
00577 }
00578 else if (!is_numeric($_POST["generate"]["number"]) || $_POST["generate"]["number"] <= 0)
00579 {
00580 ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
00581
00582 $this->generateCodes();
00583 }
00584 else
00585 {
00586 $this->generateCodes("input");
00587 }
00588 }
00589 else if ($_POST["generate"]["type"] == "auto")
00590 {
00591 if ($_GET["coupon_id"] && is_numeric($_POST["generate"]["number"]) && $_POST["generate"]["number"] > 0)
00592 {
00593 for ($i = 0; $i < $_POST["generate"]["number"]; $i++)
00594 {
00595 $code = $this->__makeCode("", $_SESSION["pay_coupons"]["code_length"]);
00596
00597 if ($code != "")
00598 {
00599 $this->coupon_obj->addCode($code, $_GET["coupon_id"]);
00600 }
00601 }
00602
00603 ilUtil::sendInfo($this->lng->txt("saved_successfully"));
00604
00605 $this->showCodes();
00606 }
00607 else
00608 {
00609 ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
00610
00611 $this->generateCodes();
00612 }
00613 }
00614 }
00615
00616 function __makeCode($a_code = "", $a_length = 32)
00617 {
00618 if ($a_code == "") $a_code = md5(uniqid(rand()));
00619
00620 if (strlen($a_code) > $a_length)
00621 {
00622 $a_code = substr($a_code, 0, $a_length);
00623 }
00624
00625 return $a_code;
00626 }
00627
00628 function generateCodes($view = "choice")
00629 {
00630 $this->coupon_obj->setId($_GET['coupon_id']);
00631
00632 $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
00633 $this->__showButtons();
00634
00635 $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_codes_generate.html','payment');
00636
00637 $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
00638
00639 $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
00640 $this->tpl->setVariable("TXT_HEADLINE", $this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_code_generation'));
00641
00642 if ($view == "choice")
00643 {
00644 $this->tpl->touchBlock("code_choice");
00645
00646 $this->tpl->setVariable("TXT_TYPE", $this->lng->txt('paya_coupons_generate_codes'));
00647 $this->tpl->setVariable("TXT_AUTO", $this->lng->txt('paya_coupons_type_auto'));
00648 $this->tpl->setVariable("TXT_SELF", $this->lng->txt('paya_coupons_type_self'));
00649 $this->tpl->setVariable("TXT_NUMBER_OF_CODES", $this->lng->txt('paya_coupons_number_of_codes'));
00650 $this->tpl->setVariable("TXT_LENGTH", $this->lng->txt('paya_coupons_code_length'));
00651 $this->tpl->setVariable("TXT_CHARS", $this->lng->txt('paya_coupons_code_chars'));
00652
00653 $this->tpl->setVariable("TYPE_".strtoupper(isset($_POST["generate"]["type"]) ? $_POST["generate"]["type"] : "auto"), " checked=\"checked\"");
00654 $this->tpl->setVariable("LENGTH", ilUtil::prepareFormOutput($_POST["generate"]["length"] ? $_POST["generate"]["length"] : 12, true));
00655
00656 $this->tpl->setVariable("TXT_REQUIRED_FIELDS", $this->lng->txt("required_field"));
00657 }
00658 else if ($view == "input")
00659 {
00660 $this->tpl->touchBlock("code_input");
00661
00662 if (is_numeric($_POST["generate"]["number"]) && $_POST["generate"]["number"] > 0)
00663 {
00664 for ($i = 0; $i < $_POST["generate"]["number"]; $i++)
00665 {
00666 $this->tpl->setCurrentBlock("loop");
00667 $this->tpl->setVariable("LOOP_CODE_INDEX", $i + 1);
00668 $this->tpl->parseCurrentBlock();
00669 }
00670 }
00671 }
00672
00673 $this->tpl->setVariable("TXT_SEND",$this->lng->txt('send'));
00674
00675 $this->tpl->setVariable("IMPORT_ACTION", $this->ctrl->getFormAction($this));
00676 $this->tpl->setVariable("TXT_IMPORT",$this->lng->txt('import'));
00677
00678 return true;
00679 }
00680
00681 function assignObjects()
00682 {
00683 if (is_array($_POST['objects']))
00684 {
00685 $this->coupon_obj->setId($_GET["coupon_id"]);
00686 foreach($_POST['objects'] as $id)
00687 {
00688 if ($id)
00689 {
00690 $this->coupon_obj->assignObjectToCoupon($id);
00691 }
00692 }
00693 }
00694
00695 $this->showObjects();
00696
00697 return true;
00698 }
00699
00700 function unassignObjects()
00701 {
00702 if (is_array($_POST['objects']))
00703 {
00704 $this->coupon_obj->setId($_GET["coupon_id"]);
00705 foreach($_POST['objects'] as $id)
00706 {
00707 if ($id)
00708 {
00709 $this->coupon_obj->unassignObjectFromCoupon($id);
00710 }
00711 }
00712 }
00713
00714 $this->showObjects();
00715
00716 return true;
00717 }
00718
00719 function showObjects()
00720 {
00721 $this->coupon_obj->setId($_GET['coupon_id']);
00722
00723 $this->__initPaymentObject();
00724
00725 $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
00726 $this->__showButtons();
00727
00728 $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_objects.html','payment');
00729
00730 $objects = $this->pobject->_getObjectsData($this->user_obj->getId());
00731
00732 $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
00733
00734 $counter_assigned = 0;
00735 $counter_unassigned = 0;
00736 $f_result_assigned = array();
00737 $f_result_unassigned = array();
00738 foreach($objects as $data)
00739 {
00740 if ($this->coupon_obj->isObjectAssignedToCoupon($data['ref_id']))
00741 {
00742 $p_counter =& $counter_assigned;
00743 $p_result =& $f_result_assigned;
00744 }
00745 else
00746 {
00747 $p_counter =& $counter_unassigned;
00748 $p_result =& $f_result_unassigned;
00749 }
00750
00751 $tmp_obj =& ilObjectFactory::getInstanceByRefId($data['ref_id']);
00752
00753 $p_result[$p_counter][] = ilUtil::formCheckbox(0, 'objects[]', $data['ref_id']);
00754 $p_result[$p_counter][] = $tmp_obj->getTitle();
00755 switch($data['status'])
00756 {
00757 case $this->pobject->STATUS_BUYABLE:
00758 $p_result[$p_counter][] = $this->lng->txt('paya_buyable');
00759 break;
00760
00761 case $this->pobject->STATUS_NOT_BUYABLE:
00762 $p_result[$p_counter][] = $this->lng->txt('paya_not_buyable');
00763 break;
00764
00765 case $this->pobject->STATUS_EXPIRES:
00766 $p_result[$p_counter][] = $this->lng->txt('paya_expires');
00767 break;
00768 }
00769 switch($data['pay_method'])
00770 {
00771 case $this->pobject->PAY_METHOD_NOT_SPECIFIED:
00772 $p_result[$p_counter][] = $this->lng->txt('paya_pay_method_not_specified');
00773 break;
00774
00775 case $this->pobject->PAY_METHOD_BILL:
00776 $p_result[$p_counter][] = $this->lng->txt('pays_bill');
00777 break;
00778
00779 case $this->pobject->PAY_METHOD_BMF:
00780 $p_result[$p_counter][] = $this->lng->txt('pays_bmf');
00781 break;
00782
00783 case $this->pobject->PAY_METHOD_PAYPAL:
00784 $p_result[$p_counter][] = $this->lng->txt('pays_paypal');
00785 break;
00786 }
00787
00788 ++$p_counter;
00789
00790 unset($tmp_obj);
00791 }
00792
00793 $this->ctrl->setParameter($this, "cmd", "showObjects");
00794
00795 if (count($f_result_assigned) > 0)
00796 {
00797 $tbl =& $this->initTableGUI();
00798 $tpl =& $tbl->getTemplateObject();
00799 $tbl->setPrefix('assigned');
00800
00801 $tpl->setCurrentBlock("tbl_form_header");
00802 $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
00803 $tpl->parseCurrentBlock();
00804 $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"));
00805 $tbl->setHeaderNames(array("",
00806 $this->lng->txt("title"),
00807 $this->lng->txt("status"),
00808 $this->lng->txt("paya_pay_method")));
00809 $header_params = $this->ctrl->getParameterArray($this,'');
00810 $tbl->setHeaderVars(array("",
00811 "title",
00812 "status",
00813 "pay_method"),$header_params);
00814 $offset = $_GET['assignedoffset'];
00815 $order = $_GET['assignedsort_by'];
00816 $direction = $_GET['assignedsort_order'] ? $_GET['assignedsort_order'] : 'desc';
00817 $tbl->setOrderColumn($order,'title');
00818 $tbl->setOrderDirection($direction);
00819 $tbl->setOffset($offset);
00820 $tbl->setLimit($_GET['limit']);
00821 $tbl->setMaxCount(count($f_result_assigned));
00822 $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
00823 $tbl->setData($f_result_assigned);
00824 $tpl->setVariable('COLUMN_COUNTS', 4);
00825 $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00826 $tpl->setCurrentBlock("tbl_action_button");
00827 $tpl->setVariable("BTN_NAME","unassignObjects");
00828 $tpl->setVariable("BTN_VALUE",$this->lng->txt("remove"));
00829 $tpl->parseCurrentBlock();
00830 $tbl->setColumnWidth(array("10%","20%","20%","20%"));
00831 $tbl->render();
00832
00833 $this->tpl->setVariable('LINKED_OBJECTS_TABLE', $tbl->tpl->get());
00834 }
00835
00836 if (count($f_result_unassigned) > 0)
00837 {
00838 $tbl =& $this->initTableGUI();
00839 $tpl =& $tbl->getTemplateObject();
00840 $tpl->setCurrentBlock("tbl_form_header");
00841 $tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
00842 $tpl->parseCurrentBlock();
00843 $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"));
00844 $tbl->setHeaderNames(array("",
00845 $this->lng->txt("title"),
00846 $this->lng->txt("status"),
00847 $this->lng->txt("paya_pay_method")));
00848 $header_params = $this->ctrl->getParameterArray($this,'');
00849 $tbl->setHeaderVars(array("",
00850 "title_ua",
00851 "status_ua",
00852 "pay_method_ua"),$header_params);
00853 $offset = $_GET['offset'];
00854 $order = $_GET['sort_by'];
00855 $direction = $_GET['sort_order'] ? $_GET['sort_order'] : 'desc';
00856 $tbl->setOrderColumn($order,'title_ua');
00857 $tbl->setOrderDirection($direction);
00858 $tbl->setOffset($offset);
00859 $tbl->setLimit($_GET['limit']);
00860 $tbl->setMaxCount(count($f_result_unassigned));
00861 $tbl->setFooter('tblfooter', $this->lng->txt('previous'), $this->lng->txt('next'));
00862 $tbl->setData($f_result_unassigned);
00863 $tpl->setVariable('COLUMN_COUNTS', 4);
00864 $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00865 $tpl->setCurrentBlock("tbl_action_button");
00866 $tpl->setVariable("BTN_NAME","assignObjects");
00867 $tpl->setVariable("BTN_VALUE",$this->lng->txt("add"));
00868 $tpl->parseCurrentBlock();
00869 $tbl->setColumnWidth(array("10%","20%","20%","20%"));
00870 $tbl->render();
00871
00872 $this->tpl->setVariable('UNLINKED_OBJECTS_TABLE', $tbl->tpl->get());
00873 }
00874
00875 return true;
00876 }
00877
00878 function importCodes()
00879 {
00880 include_once('./Services/Utilities/classes/class.ilCSVReader.php');
00881
00882 if ($_FILES["codesfile"]["tmp_name"] != "")
00883 {
00884 $csv = new ilCSVReader();
00885 $csv->setDelimiter("");
00886
00887 if ($csv->open($_FILES["codesfile"]["tmp_name"]))
00888 {
00889 $data = $csv->getDataArrayFromCSVFile();
00890
00891 for ($i = 0; $i < count($data); $i++)
00892 {
00893 for ($j = 0; $j < count($data[$i]); $j++)
00894 {
00895 $this->coupon_obj->addCode(ilUtil::stripSlashes($data[$i][$j]), $_GET["coupon_id"]);
00896 }
00897 }
00898
00899 ilUtil::sendInfo($this->lng->txt("paya_coupon_codes_import_successful"));
00900
00901 $this->showCodes();
00902 }
00903 else
00904 {
00905 ilUtil::sendInfo($this->lng->txt("paya_coupons_import_error_opening_file"));
00906
00907 $this->showCodeImport();
00908 }
00909 }
00910 else
00911 {
00912 ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields"));
00913
00914 $this->showCodeImport();
00915 }
00916
00917 return true;
00918 }
00919
00920 function showCodeImport()
00921 {
00922 $this->ctrl->setParameter($this, 'coupon_id', $_GET['coupon_id']);
00923 $this->__showButtons();
00924
00925 $this->coupon_obj->getCouponById(ilUtil::stripSlashes($_GET['coupon_id']));
00926
00927 $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.paya_coupons_codes_import.html','payment');
00928
00929 $this->tpl->setVariable('TXT_IMPORT_HINT', $this->lng->txt('import_use_only_textfile'));
00930
00931 $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
00932 $this->tpl->setVariable("TXT_HEADLINE", $this->lng->txt("paya_coupons_coupon")." ".$this->coupon_obj->getTitle().": ".$this->lng->txt('paya_coupons_codes_import'));
00933
00934 $this->tpl->setVariable("TXT_FILE",$this->lng->txt('file'));
00935
00936 $this->tpl->setVariable("TXT_UPLOAD",$this->lng->txt('upload'));
00937 $this->tpl->setVariable("TXT_REQUIRED_FIELDS", $this->lng->txt("required_field"));
00938
00939 return true;
00940 }
00941
00942 function __showButtons()
00943 {
00944 $this->showButton('addCoupon', $this->lng->txt('paya_coupons_edit'));
00945 $this->showButton('showCodes', $this->lng->txt('paya_coupons_edit_codes'));
00946 $this->showButton('showObjects', $this->lng->txt('paya_coupons_edit_objects'));
00947
00948 return true;
00949 }
00950
00951 function __initPaymentObject($a_pobject_id = 0)
00952 {
00953 include_once './payment/classes/class.ilPaymentObject.php';
00954
00955 $this->pobject =& new ilPaymentObject($this->user_obj, $a_pobject_id);
00956
00957 return true;
00958 }
00959
00960 function __initCouponObject()
00961 {
00962 include_once './payment/classes/class.ilPaymentCoupons.php';
00963
00964 $this->coupon_obj =& new ilPaymentCoupons($this->user_obj, true);
00965
00966 return true;
00967 }
00968 }
00969 ?>