Esempio n. 1
0
 /**
  * @param $id
  * @param array $params
  * @return shopCouponPluginCoupon
  */
 public static function gen($id, $params = array())
 {
     $m = new shopCouponModel();
     $tm = new shopCouponPluginTemplateModel();
     if (!($gen = $tm->getById($id))) {
         return new shopCouponPluginCoupon();
     }
     if (!($candidates = self::_candidates($gen))) {
         return new shopCouponPluginCoupon();
     }
     // Кто-то читает код? :)
     // Стоит проверять в цикле, чтоб наверняка?
     //do{
     $exists = $m->select('code')->where('code IN(?)', $candidates)->fetchAll(null, true);
     $candidates = array_diff($candidates, $exists);
     $code = reset($candidates);
     //} while(empty($code));
     $comment = $gen['comment'];
     if (!empty($params['contact_id'])) {
         $contact = new waContact($params['contact_id']);
         if ($contact->exists()) {
             $comment .= "\n" . _wp('for') . ' ' . $contact->getName();
         }
         $comment = trim($comment);
     }
     try {
         $code = mb_substr($code, 0, 32);
         $coupon = array('code' => $code, 'type' => $gen['type'], 'limit' => $gen['limit'], 'value' => $gen['value'], 'comment' => $comment, 'expire_datetime' => $gen['expire_hours'] ? date('Y-m-d H:i:s', time() + $gen['expire_hours'] * 3600) : null, 'create_datetime' => date('Y-m-d H:i:s'), 'create_contact_id' => $gen['create_contact_id']);
         $m->insert($coupon);
     } catch (waDbException $e) {
         $coupon = array();
     }
     return new shopCouponPluginCoupon($coupon);
 }
 public function execute()
 {
     /**
      * @var shopCouponPlugin $plugin
      */
     $plugin = wa()->getPlugin('coupon');
     $m = new shopCouponPluginTemplateModel();
     $generators = $m->getAll();
     $this->view->assign('generators', $generators);
     $this->view->assign('remove_expired', $plugin->getSettings('remove_expired'));
 }
 public function execute()
 {
     $id = waRequest::request('id', 0, 'int');
     $m = new shopCouponPluginTemplateModel();
     $gen = $m->getById($id);
     if ($gen) {
         $gen['value'] = (double) $gen['value'];
         if (waRequest::request('delete')) {
             $m->deleteById($id);
             exit;
         }
     } else {
         if ($id) {
             throw new waException('Generator not found.', 404);
         } else {
             // show form to create new coupon
             $gen = $m->getEmptyRow();
         }
     }
     $is_new = false;
     if (waRequest::post()) {
         $post_gen = waRequest::post('gen');
         if (is_array($post_gen)) {
             $post_gen = array_intersect_key($post_gen, $gen) + array('type' => '%');
             if (empty($post_gen['limit'])) {
                 $post_gen['limit'] = null;
             }
             if (!empty($post_gen['value'])) {
                 $post_gen['value'] = (double) str_replace(',', '.', $post_gen['value']);
             }
             if ($post_gen['type'] == '%') {
                 $post_gen['value'] = min(max($post_gen['value'], 0), 100);
             }
             if (empty($post_gen['expire_hours'])) {
                 $post_gen['expire_hours'] = null;
             }
             foreach (array('expire_hours', 'num', 'latin_lowercase', 'latin_uppercase', 'cyr_lowercase', 'cyr_uppercase', 'other') as $k) {
                 if (empty($post_gen[$k])) {
                     $post_gen[$k] = null;
                 }
             }
             if ($id) {
                 $m->updateById($id, $post_gen);
                 $gen = $m->getById($id);
             } else {
                 $post_gen['create_contact_id'] = wa()->getUser()->getId();
                 try {
                     $id = $m->insert($post_gen);
                     $gen = $m->getById($id);
                     $is_new = true;
                 } catch (waDbException $e) {
                     // Duplicate code. Show error in form.
                     $gen = $post_gen + $gen;
                 }
             }
         }
     }
     // Coupon types
     $curm = new shopCurrencyModel();
     $currencies = $curm->getAll('code');
     $types = self::getTypes($currencies);
     $this->view->assign('types', $types);
     $this->view->assign('gen', $gen);
     $this->view->assign('is_new', $is_new);
     $this->view->assign('formatted_value', shopCouponPlugin::formatValue($gen, $currencies));
 }