public function pjActionIndex()
 {
     $this->checkLogin();
     if ($this->isAdmin()) {
         if (isset($_POST['working_time'])) {
             $pjWorkingTimeModel = pjWorkingTimeModel::factory();
             $arr = $pjWorkingTimeModel->find($_POST['location_id'])->getData();
             $data = array();
             $data['location_id'] = $_POST['location_id'];
             $types = array('p_' => 'pickup', 'd_' => 'delivery');
             $weekDays = pjUtil::getWeekdays();
             foreach ($types as $prefix => $type) {
                 foreach ($weekDays as $day) {
                     if (!isset($_POST[$prefix . $day . '_dayoff'])) {
                         $data[$prefix . $day . '_from'] = $_POST[$prefix . $day . '_hour_from'] . ":" . $_POST[$prefix . $day . '_minute_from'];
                         $data[$prefix . $day . '_to'] = $_POST[$prefix . $day . '_hour_to'] . ":" . $_POST[$prefix . $day . '_minute_to'];
                     }
                 }
             }
             if (count($arr) > 0) {
                 $pjWorkingTimeModel->reset()->setAttributes(array('location_id' => $_POST['location_id']))->erase();
             }
             $pjWorkingTimeModel->reset()->setAttributes($data)->insert();
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminTime&action=pjActionIndex&id=" . $_POST['location_id'] . "&err=AT01");
         }
         if (isset($_POST['custom_time'])) {
             $date = pjUtil::formatDate($_POST['date'], $this->option_arr['o_date_format']);
             $pjDateModel = pjDateModel::factory();
             $pjDateModel->where('type', $_POST['type'])->where('date', $date)->eraseAll();
             $data = array();
             $data['location_id'] = $_POST['location_id'];
             $data['start_time'] = join(":", array($_POST['start_hour'], $_POST['start_minute']));
             $data['end_time'] = join(":", array($_POST['end_hour'], $_POST['end_minute']));
             $data['date'] = $date;
             $data['type'] = $_POST['type'];
             $data['is_dayoff'] = $_POST['is_dayoff'];
             $pjDateModel->reset()->setAttributes($data)->insert();
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminTime&action=pjActionIndex&id=" . $_POST['location_id'] . "&err=AT02&tab_id=tabs-2");
         }
         $arr = pjLocationModel::factory()->find($_GET['id'])->getData();
         if (count($arr) === 0) {
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminLocations&action=pjActionIndex&err=AL08");
         }
         $arr['i18n'] = pjMultiLangModel::factory()->getMultiLang($arr['id'], 'pjLocation');
         $this->set('arr', $arr);
         $this->set('wt_arr', pjWorkingTimeModel::factory()->find($_GET['id'])->getData());
         $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
         $this->appendJs('additional-methods.js', PJ_THIRD_PARTY_PATH . 'validate/');
         $this->appendJs('jquery.datagrid.js', PJ_FRAMEWORK_LIBS_PATH . 'pj/js/');
         $this->appendJs('pjAdminTime.js');
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionIndex()
 {
     $user_id = base64_decode($_GET['restaurants']);
     $name = urldecode($_GET['name']);
     if (is_numeric($user_id) == TRUE) {
         //Get all location from location table.
         $pjLocationModel = pjLocationModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjLocation' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'")->where('t1.user_id', $user_id)->select("t1.*, t2.content as name")->findAll()->getData();
         $this->set('location', $pjLocationModel);
         $this->set('user', $user_id);
         $this->set('name', $name);
         $this->appendJs('pjFrontIndex.js');
     } else {
         pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdmin&action=pjActionLogin");
     }
 }
 public function pjActionCreate()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         if (isset($_POST['category_create'])) {
             $pjCategoryModel = pjCategoryModel::factory();
             $data = array();
             $data['order'] = $pjCategoryModel->getLastOrder();
             $data['location_id'] = $_POST['mlocaiton_id'];
             $id = $pjCategoryModel->setAttributes(array_merge($_POST, $data))->insert()->getInsertId();
             if ($id !== false && (int) $id > 0) {
                 $err = 'ACT03';
                 if (isset($_POST['i18n'])) {
                     pjMultiLangModel::factory()->saveMultiLang($_POST['i18n'], $id, 'pjCategory', 'data');
                 }
             } else {
                 $err = 'ACT04';
             }
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminCategories&action=pjActionIndex&err={$err}");
         } else {
             $lp_arr = array();
             $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();
             $locationModel = pjLocationModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjLocation' AND t2.field = 'name'")->select("t1.*, t2.content as name")->where('t1.user_id', $_SESSION['admin_user']['id'])->where('t2.locale', $this->getLocaleId())->findAll()->getData();
             foreach ($locale_arr as $item) {
                 $lp_arr[$item['id'] . "_"] = $item['file'];
             }
             $this->set('location_arr', $locationModel);
             $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('pjAdminCategories.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         if (isset($_POST['location_update'])) {
             $data = array();
             if (empty($_POST['lat']) && empty($_POST['lng'])) {
                 $data = pjAppController::getCoords($_POST['i18n'][$this->getLocaleId()]['address']);
             }
             pjLocationModel::factory()->where('id', $_POST['id'])->limit(1)->modifyAll(array_merge($_POST, $data));
             if (isset($_POST['i18n'])) {
                 pjMultiLangModel::factory()->updateMultiLang($_POST['i18n'], $_POST['id'], 'pjLocation', 'data');
             }
             $pjLocationCoordModel = pjLocationCoordModel::factory();
             $pjLocationCoordModel->where('location_id', $_POST['id'])->eraseAll();
             if (isset($_POST['data'])) {
                 foreach ($_POST['data'] as $type => $coords) {
                     foreach ($coords as $hash => $d) {
                         $pjLocationCoordModel->reset()->setAttributes(array('location_id' => $_POST['id'], 'type' => $type, 'hash' => md5($hash), 'data' => $d))->insert();
                     }
                 }
             }
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminLocations&action=pjActionUpdate&id=" . $_POST['id'] . "&err=AL01");
         } else {
             $arr = pjLocationModel::factory()->find($_GET['id'])->getData();
             if (count($arr) === 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminLocations&action=pjActionIndex&err=AL08");
             }
             $arr['i18n'] = pjMultiLangModel::factory()->getMultiLang($arr['id'], 'pjLocation');
             $this->set('arr', $arr);
             $this->set('coord_arr', pjLocationCoordModel::factory()->where('location_id', $_GET['id'])->findAll()->getData());
             $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'];
             }
             $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('js?sensor=false&libraries=drawing', 'https://maps.googleapis.com/maps/api/', TRUE);
             $this->appendJs('pjAdminLocations.js');
         }
     } 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 pjActionGetLocations()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjLocationCoordModel = pjLocationCoordModel::factory();
         $arr = pjLocationModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjLocation' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->join('pjMultiLang', "t3.foreign_id = t1.id AND t3.model = 'pjLocation' AND t3.locale = '" . $this->getLocaleId() . "' AND t3.field = 'address'", 'left')->select('t1.*, t2.content AS name, t3.content AS address')->findAll()->getData();
         foreach ($arr as $k => $v) {
             $arr[$k]['coords'] = $pjLocationCoordModel->reset()->where('t1.location_id', $v['id'])->findAll()->getData();
         }
         pjAppController::jsonResponse($arr);
     }
     exit;
 }
 public function addOrderDetails(&$arr, $locale_id)
 {
     $l_arr = pjLocationModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjLocation' AND t2.locale = '" . $locale_id . "' AND t2.field = 'name'", 'left')->select('t1.*, t2.content as name')->find($arr['location_id'])->getData();
     if (count($l_arr) > 0) {
         $arr['location'] = $l_arr['name'];
     }
     $pjOrderItemModel = pjOrderItemModel::factory();
     $arr['product_arr'] = $pjOrderItemModel->reset()->join('pjMultiLang', "t2.foreign_id = t1.foreign_id AND t2.model = 'pjProduct' AND t2.locale = '" . $locale_id . "' AND t2.field = 'name'", 'left')->select('t1.*, t2.content as name')->where('t1.order_id', $arr['id'])->where('type', 'product')->findAll()->getData();
     foreach ($arr['product_arr'] as $k => $product) {
         $arr['product_arr'][$k]['extra_arr'] = $pjOrderItemModel->reset()->join('pjMultiLang', "t2.foreign_id = t1.foreign_id AND t2.model = 'pjExtra' AND t2.locale = '" . $locale_id . "' AND t2.field = 'name'", 'left')->select('t1.*, t2.content as name')->where('t1.order_id', $arr['id'])->where('type', 'extra')->where('hash', $product['hash'])->findAll()->getData();
     }
 }