public function pjActionGetExtra()
    {
        $this->setAjax(true);
        if ($this->isXHR()) {
            $pjExtraModel = pjExtraModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjExtra' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->where('user_id', $_SESSION['admin_user']['id']);
            if (isset($_GET['q']) && !empty($_GET['q'])) {
                $q = pjObject::escapeString($_GET['q']);
                $pjExtraModel->where('t2.content LIKE', "%{$q}%");
            }
            $column = 'name';
            $direction = 'ASC';
            if (isset($_GET['direction']) && isset($_GET['column']) && in_array(strtoupper($_GET['direction']), array('ASC', 'DESC'))) {
                $column = $_GET['column'];
                $direction = strtoupper($_GET['direction']);
            }
            $total = $pjExtraModel->findCount()->getData();
            $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 20;
            $pages = ceil($total / $rowCount);
            $page = isset($_GET['page']) && (int) $_GET['page'] > 0 ? intval($_GET['page']) : 1;
            $offset = ((int) $page - 1) * $rowCount;
            if ($page > $pages) {
                $page = $pages;
            }
            $data = $pjExtraModel->select('t1.*, t2.content AS name, 
						  (SELECT COUNT(t3.product_id) FROM `' . pjProductExtraModel::factory()->getTable() . '` AS t3 WHERE t3.extra_id=t1.id) as products')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
            foreach ($data as $k => $v) {
                $v['price'] = pjUtil::formatCurrencySign($v['price'], $this->option_arr['o_currency']);
                $data[$k] = $v;
            }
            pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
        }
        exit;
    }
 public function pjActionGetExtras()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         if (isset($_GET['product_id']) && (int) $_GET['product_id'] > 0) {
             $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")->where("t1.id IN (SELECT TPE.extra_id FROM `" . pjProductExtraModel::factory()->getTable() . "` AS TPE WHERE TPE.product_id=" . $_GET['product_id'] . ")")->orderBy("name ASC")->findAll()->getData();
             $this->set('extra_arr', $extra_arr);
         }
     }
 }
 public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         $post_max_size = pjUtil::getPostMaxSize();
         if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > $post_max_size) {
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminProducts&action=pjActionIndex&err=AP06");
         }
         if (isset($_POST['product_update'])) {
             $pjProductModel = pjProductModel::factory();
             $err = 'AP01';
             $arr = $pjProductModel->find($_POST['id'])->getData();
             if (empty($arr)) {
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminProducts&action=pjActionIndex&err=AP08");
             }
             $data = array();
             if (isset($_FILES['image'])) {
                 if ($_FILES['image']['error'] == 0) {
                     if (getimagesize($_FILES['image']["tmp_name"]) != false) {
                         if (!empty($arr['image'])) {
                             @unlink(PJ_INSTALL_PATH . $arr['image']);
                         }
                         $Image = new pjImage();
                         if ($Image->getErrorCode() !== 200) {
                             $Image->setAllowedTypes(array('image/png', 'image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg'));
                             if ($Image->load($_FILES['image'])) {
                                 $resp = $Image->isConvertPossible();
                                 if ($resp['status'] === true) {
                                     $hash = md5(uniqid(rand(), true));
                                     $image_path = PJ_UPLOAD_PATH . 'products/' . $_POST['id'] . '_' . $hash . '.' . $Image->getExtension();
                                     $Image->loadImage($_FILES['image']["tmp_name"]);
                                     $Image->setFillColor($this->imageFillColor)->resize(116, 87);
                                     $Image->saveImage($image_path);
                                     $data['image'] = $image_path;
                                 }
                             }
                         }
                     } else {
                         $err = 'AP10';
                     }
                 } else {
                     if ($_FILES['image']['error'] != 4) {
                         $err = 'AP10';
                     }
                 }
             }
             if (isset($_POST['is_featured'])) {
                 $data['is_featured'] = 1;
                 unset($_POST['is_featured']);
             } else {
                 $data['is_featured'] = 0;
             }
             $pjProductModel->reset()->where('id', $_POST['id'])->limit(1)->modifyAll(array_merge($_POST, $data));
             if (isset($_POST['i18n'])) {
                 pjMultiLangModel::factory()->updateMultiLang($_POST['i18n'], $_POST['id'], 'pjProduct', 'data');
                 $pjMultiLangModel = pjMultiLangModel::factory();
                 $pjProductPriceModel = pjProductPriceModel::factory();
                 if ($_POST['set_different_sizes'] == 'T') {
                     if (isset($_POST['index_arr']) && $_POST['index_arr'] != '') {
                         $index_arr = explode("|", $_POST['index_arr']);
                         foreach ($index_arr as $k => $v) {
                             if (strpos($v, 'fd') !== false) {
                                 $p_data = array();
                                 $p_data['product_id'] = $_POST['id'];
                                 $p_data['price'] = $_POST['product_price'][$v];
                                 $price_id = $pjProductPriceModel->reset()->setAttributes($p_data)->insert()->getInsertId();
                                 if ($price_id !== false && (int) $price_id > 0) {
                                     foreach ($_POST['i18n'] as $locale => $locale_arr) {
                                         foreach ($locale_arr as $field => $content) {
                                             if (is_array($content)) {
                                                 $insert_id = $pjMultiLangModel->reset()->setAttributes(array('foreign_id' => $price_id, 'model' => 'pjProductPrice', 'locale' => $locale, 'field' => $field, 'content' => $content[$v], 'source' => 'data'))->insert()->getInsertId();
                                             }
                                         }
                                     }
                                 }
                             } else {
                                 $p_data = array();
                                 $p_data['price'] = $_POST['product_price'][$v];
                                 $pjProductPriceModel->reset()->where('id', $v)->limit(1)->modifyAll($p_data);
                                 foreach ($_POST['i18n'] as $locale => $locale_arr) {
                                     foreach ($locale_arr as $field => $content) {
                                         if (is_array($content)) {
                                             $sql = sprintf("INSERT INTO `%1\$s` (`id`, `foreign_id`, `model`, `locale`, `field`, `content`, `source`)\n\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES (NULL, :foreign_id, :model, :locale, :field, :update_content, :source)\n\t\t\t\t\t\t\t\t\t\t\t\t\tON DUPLICATE KEY UPDATE `content` = :update_content, `source` = :source;", $pjMultiLangModel->getTable());
                                             $foreign_id = $v;
                                             $model = 'pjProductPrice';
                                             $source = 'data';
                                             $update_content = $content[$v];
                                             $modelObj = $pjMultiLangModel->reset()->prepare($sql)->exec(compact('foreign_id', 'model', 'locale', 'field', 'update_content', 'source'));
                                             if ($modelObj->getAffectedRows() > 0 || $modelObj->getInsertId() > 0) {
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     if (isset($_POST['remove_arr']) && $_POST['remove_arr'] != '') {
                         $remove_arr = explode("|", $_POST['remove_arr']);
                         $pjMultiLangModel->reset()->where('model', 'pjProductPrice')->whereIn('foreign_id', $remove_arr)->eraseAll();
                         $pjProductPriceModel->reset()->whereIn('id', $remove_arr)->eraseAll();
                     }
                     $pjProductModel->reset()->where('id', $_POST['id'])->limit(1)->modifyAll(array('price' => ':NULL'));
                 } else {
                     $id_arr = $pjProductPriceModel->where('product_id', $_POST['id'])->findAll()->getDataPair("id", "id");
                     $pjMultiLangModel->reset()->where('model', 'pjProductPrice')->whereIn('foreign_id', $id_arr);
                     $pjProductPriceModel->reset()->where('product_id', $_POST['id'])->eraseAll();
                 }
             }
             $pjProductCategoryModel = pjProductCategoryModel::factory();
             $pjProductCategoryModel->where('product_id', $_POST['id'])->eraseAll();
             if (isset($_POST['category_id']) && is_array($_POST['category_id']) && count($_POST['category_id']) > 0) {
                 $pjProductCategoryModel->reset()->begin();
                 foreach ($_POST['category_id'] as $category_id) {
                     $pjProductCategoryModel->reset()->set('product_id', $_POST['id'])->set('category_id', $category_id)->insert();
                 }
                 $pjProductCategoryModel->commit();
             }
             $pjProductExtraModel = pjProductExtraModel::factory();
             $pjProductExtraModel->where('product_id', $_POST['id'])->eraseAll();
             if (isset($_POST['extra_id']) && is_array($_POST['extra_id']) && count($_POST['extra_id']) > 0) {
                 $pjProductExtraModel->reset()->begin();
                 foreach ($_POST['extra_id'] as $extra_id) {
                     $pjProductExtraModel->reset()->set('product_id', $_POST['id'])->set('extra_id', $extra_id)->insert();
                 }
                 $pjProductExtraModel->commit();
             }
             if ($err == 'AP01') {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminProducts&action=pjActionIndex&err=AP01");
             } else {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminProducts&action=pjActionUpdate&id=" . $_POST['id'] . "&err=AP10");
             }
         } else {
             $pjMultiLangModel = pjMultiLangModel::factory();
             $arr = pjProductModel::factory()->find($_GET['id'])->getData();
             if (count($arr) === 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminProducts&action=pjActionIndex&err=AP08");
             }
             $arr['i18n'] = $pjMultiLangModel->getMultiLang($arr['id'], 'pjProduct');
             $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'];
             }
             $this->set('lp_arr', $locale_arr);
             $this->set('locale_str', pjAppController::jsonEncode($lp_arr));
             $this->set('category_arr', pjCategoryModel::factory()->select('t1.*, t2.content AS name')->join('pjMultiLang', "t2.model='pjCategory' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $this->getLocaleId() . "'", 'left outer')->where('t1.status', 'T')->orderBy('`order` ASC')->findAll()->getData());
             $this->set('extra_arr', pjExtraModel::factory()->select('t1.*, t2.content AS name')->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . $this->getLocaleId() . "'", 'left outer')->orderBy('name ASC')->findAll()->getData());
             $this->set('category_id_arr', pjProductCategoryModel::factory()->where("product_id", $_GET['id'])->findAll()->getDataPair("category_id", "category_id"));
             $this->set('extra_id_arr', pjProductExtraModel::factory()->where("product_id", $_GET['id'])->findAll()->getDataPair("extra_id", "extra_id"));
             if ($arr['set_different_sizes'] == 'T') {
                 $size_arr = pjProductPriceModel::factory()->where('product_id', $_GET['id'])->findAll()->getData();
                 foreach ($size_arr as $k => $v) {
                     $size_arr[$k]['i18n'] = pjMultiLangModel::factory()->getMultiLang($v['id'], 'pjProductPrice');
                 }
                 $this->set('size_arr', $size_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('jquery.multiselect.min.js', PJ_THIRD_PARTY_PATH . 'multiselect/');
             $this->appendCss('jquery.multiselect.css', PJ_THIRD_PARTY_PATH . 'multiselect/');
             $this->appendJs('pjAdminProducts.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 private function getCategories($get)
 {
     $user_id = '';
     //check front layout order data set or not.
     if (isset($_SESSION['order_data'])) {
         $user_id = $_SESSION['order_data']['o_user_id'];
     } else {
         $user_id = $_SESSION['admin_user']['id'];
     }
     $category_arr = '';
     //Get categories from DB.
     $category_arr = pjCategoryModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjCategory' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->select("t1.*, t2.content as name")->where('t1.status', 'T')->where('t1.location_id', $_SESSION['order_data']['o_location_id'])->where('t1.user_id', $user_id)->orderBy("`order` ASC")->findAll()->getData();
     $open_id = null;
     $product_arr = array();
     if (isset($get['category_id'])) {
         $open_id = $get['category_id'];
         $this->_set('open_id', $open_id);
     } elseif (!empty($lc)) {
         $open_id = $category_arr[0]['id'];
         $this->_set('open_id', $open_id);
     } else {
         if (!$this->_is('open_id')) {
             foreach ($category_arr as $k => $v) {
                 if ($k == 0) {
                     $open_id = $v['id'];
                 }
             }
         } else {
             $open_id = $this->_get('open_id');
         }
     }
     if ($open_id != null) {
         $pjExtraModel = pjExtraModel::factory();
         $pjProductExtraTable = pjProductExtraModel::factory()->getTable();
         $pjProductPriceModel = pjProductPriceModel::factory();
         $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')->join('pjMultiLang', "t3.foreign_id = t1.id AND t3.model = 'pjProduct' AND t3.locale = '" . $this->getLocaleId() . "' AND t3.field = 'description'", 'left')->select("t1.*, t2.content AS name, t3.content AS description")->where("(t1.id IN (SELECT TPC.product_id FROM `" . pjProductCategoryModel::factory()->getTable() . "` AS TPC WHERE TPC.category_id='{$open_id}'))")->orderBy("t1.is_featured DESC, t2.content ASC")->findAll()->getData();
         foreach ($product_arr as $k => $product) {
             $product['price_arr'] = array();
             $product['extra_arr'] = array();
             $product['extra_arr'] = $pjExtraModel->reset()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjExtra' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left')->where("(t1.id IN (SELECT TPE.extra_id FROM `" . $pjProductExtraTable . "` AS TPE WHERE TPE.product_id='" . $product['id'] . "'))")->select("t1.*, t2.content AS name")->orderBy("name ASC")->findAll()->getData();
             if ($product['set_different_sizes'] == 'T') {
                 $product['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')->where('t1.product_id', $product['id'])->select("t1.*, t2.content AS price_name")->findAll()->getData();
             }
             $product_arr[$k] = $product;
         }
     }
     return compact('category_arr', 'product_arr', 'open_id', 'mlocation_arr', 'lc');
 }