public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isCountryReady()) {
         if (isset($_POST['country_update'])) {
             pjCountryModel::factory()->where('id', $_POST['id'])->limit(1)->modifyAll($_POST);
             if (isset($_POST['i18n'])) {
                 pjMultiLangModel::factory()->updateMultiLang($_POST['i18n'], $_POST['id'], 'pjCountry');
             }
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjCountry&action=pjActionIndex&err=PCY01");
         } else {
             $arr = pjCountryModel::factory()->find($_GET['id'])->getData();
             if (count($arr) === 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjCountry&action=pjActionIndex&err=PCY08");
             }
             $arr['i18n'] = pjMultiLangModel::factory()->getMultiLang($arr['id'], 'pjCountry');
             $this->set('arr', $arr);
             $locale_arr = pjLocaleModel::factory()->select('t1.*, t2.file')->join('pjLocaleLanguage', 't2.iso=t1.language_iso', 'left')->where('t2.file IS NOT NULL')->orderBy('t1.sort ASC')->findAll()->getData();
             $lp_arr = array();
             foreach ($locale_arr as $item) {
                 $lp_arr[$item['id'] . "_"] = $item['file'];
                 //Hack for jquery $.extend, to prevent (re)order of numeric keys in object
             }
             $this->set('lp_arr', $locale_arr);
             $this->set('locale_str', pjAppController::jsonEncode($lp_arr));
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('jquery.multilang.js', PJ_FRAMEWORK_LIBS_PATH . 'pj/js/');
             $this->appendJs('jquery.tipsy.js', PJ_THIRD_PARTY_PATH . 'tipsy/');
             $this->appendCss('jquery.tipsy.css', PJ_THIRD_PARTY_PATH . 'tipsy/');
             $this->appendJs('pjCountry.js', $this->getConst('PLUGIN_JS_PATH'));
             $this->appendJs('index.php?controller=pjAdmin&action=pjActionMessages', PJ_INSTALL_URL, true);
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         if (isset($_POST['order_update'])) {
             $pjOrderModel = pjOrderModel::factory();
             $pjOrderItemModel = pjOrderItemModel::factory();
             $pjProductPriceModel = pjProductPriceModel::factory();
             $pjExtraModel = pjExtraModel::factory();
             $pjProductModel = pjProductModel::factory();
             $arr = $pjOrderModel->find($_POST['id'])->getData();
             if (empty($arr)) {
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminOrders&action=pjActionIndex&err=AR08");
             }
             if (isset($_POST['product_id']) && count($_POST['product_id']) > 0) {
                 $keys = array_keys($_POST['product_id']);
                 $pjOrderItemModel->reset()->where('order_id', $_POST['id'])->whereNotIn('hash', $keys)->eraseAll();
                 $pjOrderItemModel->reset()->where('order_id', $_POST['id'])->where('type', 'extra')->eraseAll();
                 foreach ($_POST['product_id'] as $k => $pid) {
                     $product = $pjProductModel->reset()->find($pid)->getData();
                     $price = 0;
                     $price_id = ":NULL";
                     if ($product['set_different_sizes'] == 'T') {
                         $price_id = $_POST['price_id'][$k];
                         $price_arr = $pjProductPriceModel->reset()->find($price_id)->getData();
                         if ($price_arr) {
                             $price = $price_arr['price'];
                         }
                     } else {
                         $price = $product['price'];
                     }
                     if (strpos($k, 'new_') === 0) {
                         $hash = md5(uniqid(rand(), true));
                         $oid = $pjOrderItemModel->reset()->setAttributes(array('order_id' => $_POST['id'], 'foreign_id' => $pid, 'type' => 'product', 'hash' => $hash, 'price_id' => $price_id, 'price' => $price, 'cnt' => $_POST['cnt'][$k]))->insert()->getInsertId();
                         if ($oid !== false && (int) $oid > 0) {
                             if (isset($_POST['extra_id']) && isset($_POST['extra_id'][$k])) {
                                 foreach ($_POST['extra_id'][$k] as $i => $eid) {
                                     $extra_price = 0;
                                     $extra_arr = $pjExtraModel->reset()->find($eid)->getData();
                                     if (!empty($extra_arr) && !empty($extra_arr['price'])) {
                                         $extra_price = $extra_arr['price'];
                                     }
                                     $pjOrderItemModel->reset()->setAttributes(array('order_id' => $_POST['id'], 'foreign_id' => $eid, 'type' => 'extra', 'hash' => $hash, 'price_id' => ':NULL', 'price' => $extra_price, 'cnt' => $_POST['extra_cnt'][$k][$i]))->insert();
                                 }
                             }
                         }
                     } else {
                         $pjOrderItemModel->reset()->where('hash', $k)->where('type', 'product')->limit(1)->modifyAll(array('foreign_id' => $pid, 'cnt' => $_POST['cnt'][$k], 'price_id' => $price_id, 'price' => $price));
                         if (isset($_POST['extra_id']) && isset($_POST['extra_id'][$k])) {
                             foreach ($_POST['extra_id'][$k] as $i => $eid) {
                                 $extra_price = 0;
                                 $extra_arr = $pjExtraModel->reset()->find($eid)->getData();
                                 if (!empty($extra_arr) && !empty($extra_arr['price'])) {
                                     $extra_price = $extra_arr['price'];
                                 }
                                 $pjOrderItemModel->reset()->setAttributes(array('order_id' => $_POST['id'], 'foreign_id' => $eid, 'type' => 'extra', 'hash' => $k, 'price_id' => ':NULL', 'price' => $extra_price, 'cnt' => $_POST['extra_cnt'][$k][$i]))->insert();
                             }
                         }
                     }
                 }
             }
             $data = array();
             $data['ip'] = pjUtil::getClientIp();
             switch ($_POST['type']) {
                 case 'pickup':
                     if (!empty($_POST['p_dt'])) {
                         $date_time = $_POST['p_dt'];
                         if (count(explode(" ", $date_time)) == 3) {
                             list($_date, $_time, $_period) = explode(" ", $date_time);
                             $time = pjUtil::formatTime($_time . ' ' . $_period, $this->option_arr['o_time_format']);
                         } else {
                             list($_date, $_time) = explode(" ", $date_time);
                             $time = pjUtil::formatTime($_time, $this->option_arr['o_time_format']);
                         }
                         unset($_POST['p_dt']);
                         unset($_POST['d_dt']);
                         $data['p_dt'] = pjUtil::formatDate($_date, $this->option_arr['o_date_format']) . ' ' . $time;
                     }
                     if (isset($_POST['p_location_id']) && (int) $_POST['p_location_id'] > 0) {
                         $data['location_id'] = $_POST['p_location_id'];
                     }
                     break;
                 case 'delivery':
                     if (!empty($_POST['d_dt'])) {
                         $date_time = $_POST['d_dt'];
                         if (count(explode(" ", $date_time)) == 3) {
                             list($_date, $_time, $_period) = explode(" ", $date_time);
                             $time = pjUtil::formatTime($_time . ' ' . $_period, $this->option_arr['o_time_format']);
                         } else {
                             list($_date, $_time) = explode(" ", $date_time);
                             $time = pjUtil::formatTime($_time, $this->option_arr['o_time_format']);
                         }
                         unset($_POST['p_dt']);
                         unset($_POST['d_dt']);
                         $data['d_dt'] = pjUtil::formatDate($_date, $this->option_arr['o_date_format']) . ' ' . $time;
                     }
                     if (isset($_POST['d_location_id']) && (int) $_POST['d_location_id'] > 0) {
                         $data['location_id'] = $_POST['d_location_id'];
                     }
                     break;
             }
             if ($_POST['payment_method'] == 'creditcard') {
                 $data['cc_exp'] = $_POST['cc_exp_month'] . "/" . $_POST['cc_exp_year'];
             }
             $pjOrderModel->reset()->where('id', $_POST['id'])->limit(1)->modifyAll(array_merge($_POST, $data));
             $err = 'AR01';
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminOrders&action=pjActionIndex&err={$err}");
         } else {
             $arr = pjOrderModel::factory()->join('pjClient', "t2.id=t1.client_id", 'left outer')->select('t1.*,t2.c_name as client_name')->find($_GET['id'])->getData();
             if (count($arr) <= 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminOrders&action=pjActionIndex&err=AR08");
             }
             $this->set('arr', $arr);
             $country_arr = pjCountryModel::factory()->select('t1.id, t2.content AS country_title')->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $this->getLocaleId() . "'", 'left outer')->orderBy('`country_title` ASC')->findAll()->getData();
             $this->set('country_arr', $country_arr);
             $product_arr = pjProductModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjProduct' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->select("t1.*, t2.content AS name, (SELECT GROUP_CONCAT(extra_id SEPARATOR '~:~') FROM `" . pjProductExtraModel::factory()->getTable() . "` WHERE product_id = t1.id GROUP BY product_id LIMIT 1) AS allowed_extras ")->orderBy("name ASC")->findAll()->toArray('allowed_extras', '~:~')->getData();
             $this->set('product_arr', $product_arr);
             $location_arr = pjLocationModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjLocation' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->select("t1.*, t2.content AS name")->orderBy("name ASC")->findAll()->getData();
             $this->set('location_arr', $location_arr);
             $extra_arr = pjExtraModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjExtra' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->select("t1.*, t2.content AS name")->orderBy("name ASC")->findAll()->getData();
             $this->set('extra_arr', $extra_arr);
             $pjProductPriceModel = pjProductPriceModel::factory();
             $oi_arr = array();
             $_oi_arr = pjOrderItemModel::factory()->where('t1.order_id', $arr['id'])->findAll()->getData();
             foreach ($_oi_arr as $item) {
                 if ($item['type'] == 'product') {
                     $item['price_arr'] = $pjProductPriceModel->reset()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjProductPrice' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'price_name'", 'left')->select("t1.*, t2.content AS price_name")->where('product_id', $item['foreign_id'])->findAll()->getData();
                 }
                 $oi_arr[] = $item;
             }
             $this->set('oi_arr', $oi_arr);
             $this->appendJs('chosen.jquery.js', PJ_THIRD_PARTY_PATH . 'harvest/chosen/');
             $this->appendCss('chosen.css', PJ_THIRD_PARTY_PATH . 'harvest/chosen/');
             $this->appendJs('jquery-ui-sliderAccess.js', PJ_THIRD_PARTY_PATH . 'timepicker/');
             $this->appendJs('jquery-ui-timepicker-addon.js', PJ_THIRD_PARTY_PATH . 'timepicker/');
             $this->appendCss('jquery-ui-timepicker-addon.css', PJ_THIRD_PARTY_PATH . 'timepicker/');
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('jquery.noty.packaged.min.js', PJ_THIRD_PARTY_PATH . 'noty/packaged/');
             $this->appendJs('pjAdminOrders.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isAdmin()) {
         if (isset($_POST['client_update'])) {
             pjClientModel::factory()->where('id', $_POST['id'])->limit(1)->modifyAll($_POST);
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminClients&action=pjActionIndex&err=AC01");
         } else {
             $arr = pjClientModel::factory()->find($_GET['id'])->getData();
             if (count($arr) === 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminClients&action=pjActionIndex&err=AC08");
             }
             $this->set('arr', $arr);
             $this->set('role_arr', pjRoleModel::factory()->orderBy('t1.id ASC')->findAll()->getData());
             $country_arr = pjCountryModel::factory()->select('t1.id, t2.content AS country_title')->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $this->getLocaleId() . "'", 'left outer')->orderBy('`country_title` ASC')->findAll()->getData();
             $this->set('country_arr', $country_arr);
             $this->appendJs('chosen.jquery.js', PJ_THIRD_PARTY_PATH . 'harvest/chosen/');
             $this->appendCss('chosen.css', PJ_THIRD_PARTY_PATH . 'harvest/chosen/');
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('pjAdminClients.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionPreview()
 {
     $this->setAjax(true);
     if ($this->isXHR() || isset($_GET['_escaped_fragment_'])) {
         if (isset($_SESSION[$this->defaultStore]) && count($_SESSION[$this->defaultStore]) > 0) {
             $country_arr = array();
             if (isset($_SESSION[$this->defaultForm]['c_country']) && !empty($_SESSION[$this->defaultForm]['c_country'])) {
                 $country_arr = pjCountryModel::factory()->select('t1.id, t2.content AS country_title')->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $this->getLocaleId() . "'", 'left outer')->find($_SESSION[$this->defaultForm]['c_country'])->getData();
             }
             $this->set('country_arr', $country_arr);
             $this->set('status', 'OK');
         } else {
             $this->set('status', 'ERR');
         }
         $this->set('ldata', $this->_get('p_location_id'));
         $this->set('cart_box', $this->getCartInfo());
     }
 }
 public function getTokens($option_arr, $data, $salt, $locale_id)
 {
     $c_country = NULL;
     $d_country = NULL;
     if (isset($data['c_country']) && !empty($data['c_country'])) {
         $pjCountryModel = pjCountryModel::factory();
         $country_arr = pjCountryModel::factory()->select('t1.id, t2.content AS country_title')->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $locale_id . "'", 'left outer')->find($data['c_country'])->getData();
         if (!empty($country_arr)) {
             $c_country = $country_arr['country_title'];
         }
         $country_arr = pjCountryModel::factory()->reset()->select('t1.id, t2.content AS country_title')->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $locale_id . "'", 'left outer')->find($data['d_country_id'])->getData();
         if (!empty($country_arr)) {
             $d_country = $country_arr['country_title'];
         }
     }
     $row = array();
     if (isset($data['product_arr'])) {
         foreach ($data['product_arr'] as $v) {
             $extra = array();
             foreach ($v['extra_arr'] as $e) {
                 $extra[] = stripslashes(sprintf("%u x %s", $e['cnt'], $e['name']));
             }
             $row[] = stripslashes(sprintf("%u x %s", $v['cnt'], $v['name'])) . (count($extra) > 0 ? sprintf(" (%s)", join("; ", $extra)) : NULL);
         }
     }
     $order_data = count($row) > 0 ? join("\n", $row) : NULL;
     $discount = NULL;
     if (!empty($data['voucher_code'])) {
         $voucher_arr = pjVoucherModel::factory()->where('t1.code', $data['voucher_code'])->limit(1)->findAll()->getData();
         if (!empty($voucher_arr)) {
             $voucher_arr = $voucher_arr[0];
             switch ($voucher_arr['type']) {
                 case "amount":
                     $discount = pjUtil::formatCurrencySign($voucher_arr['discount'], $option_arr['o_currency']);
                     break;
                 case "percent":
                     $discount = $voucher_arr['discount'] . '%';
                     break;
             }
         }
     }
     $subtotal = pjUtil::formatCurrencySign($data['subtotal'], $option_arr['o_currency']);
     $price_delivery = pjUtil::formatCurrencySign($data['price_delivery'], $option_arr['o_currency']);
     $total = pjUtil::formatCurrencySign($data['total'], $option_arr['o_currency']);
     $cancelURL = PJ_INSTALL_URL . 'index.php?controller=pjFront&action=pjActionCancel&id=' . @$data['id'] . '&hash=' . sha1(@$data['id'] . @$data['created'] . $salt);
     $search = array('{Country}', '{City}', '{State}', '{Notes}', '{Zip}', '{Address1}', '{Address2}', '{Name}', '{Email}', '{Phone}', '{dCountry}', '{dCity}', '{dState}', '{dZip}', '{dAddress1}', '{dAddress2}', '{CCType}', '{CCNum}', '{CCExp}', '{CCSec}', '{PaymentMethod}', '{DateTime}', '{Subtotal}', '{Delivery}', '{Discount}', '{Total}', '{dNotes}', '{Location}', '{OrderID}', '{CancelURL}', '{OrderDetails}');
     $replace = array($c_country, @$data['c_city'], @$data['c_state'], @$data['c_notes'], @$data['c_zip'], @$data['c_address_1'], @$data['c_address_2'], @$data['c_name'], @$data['c_email'], @$data['c_phone'], $d_country, @$data['d_city'], $data['d_state'], @$data['d_zip'], @$data['d_address_1'], @$data['d_address_2'], @$data['cc_type'], @$data['cc_num'], @$data['payment_method'] == 'creditcard' ? @$data['cc_exp'] : NULL, @$data['cc_code'], @$data['payment_method'], date($option_arr['o_datetime_format'], strtotime(@$data['type'] == 'pickup' ? @$data['p_dt'] : @$data['d_dt'])), $subtotal, $price_delivery, @$discount, $total, @$data['d_notes'], @$data['location'], @$data['uuid'], $cancelURL, $order_data);
     return compact('search', 'replace');
 }