public function pjActionGetMerchant()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjMerchantModel = pjMerchantModel::factory()->where('user_id', $_SESSION['admin_user']['id']);
         $column = 'merchant_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 = $pjMerchantModel->findCount()->getData();
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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 = array();
         $data = $pjMerchantModel->select('t1.merchant_id, t1.merchant_name, t1.merchant_address')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 public function pjActionGetSms()
 {
     $this->setAjax(true);
     if ($this->isXHR() && $this->isLoged() && $this->isAdmin()) {
         $pjSmsModel = pjSmsModel::factory();
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = $pjSmsModel->escapeStr($_GET['q']);
             $q = str_replace(array('%', '_'), array('\\%', '\\_'), $q);
             $pjSmsModel->where("(t1.number LIKE '%{$q}%' OR t1.text LIKE '%{$q}%')");
         }
         $column = 'created';
         $direction = 'DESC';
         if (isset($_GET['direction']) && isset($_GET['column']) && in_array(strtoupper($_GET['direction']), array('ASC', 'DESC'))) {
             $column = $_GET['column'];
             $direction = strtoupper($_GET['direction']);
         }
         $total = $pjSmsModel->findCount()->getData();
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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 = $pjSmsModel->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 public function pjActionDeleteImage()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $response = array();
         $pjProductModel = pjProductModel::factory();
         $arr = $pjProductModel->find($_GET['id'])->getData();
         if (!empty($arr)) {
             if (!empty($arr['image'])) {
                 @unlink(PJ_INSTALL_PATH . $arr['image']);
             }
             $data = array();
             $data['image'] = ':NULL';
             $pjProductModel->reset()->where(array('id' => $_GET['id']))->limit(1)->modifyAll($data);
             $response['code'] = 200;
         } else {
             $response['code'] = 100;
         }
         pjAppController::jsonResponse($response);
     }
 }
 public function pjActionGetVoucher()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjVoucherModel = pjVoucherModel::factory()->where('user_id', $_SESSION['admin_user']['id']);
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = pjObject::escapeString($_GET['q']);
             $pjVoucherModel->where('t1.code LIKE', "%{$q}%");
         }
         $column = 'code';
         $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 = $pjVoucherModel->findCount()->getData();
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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 = array();
         $data = $pjVoucherModel->select('t1.*')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         foreach ($data as $k => $v) {
             if ($v['type'] == 'percent') {
                 $v['discount'] = $v['discount'] . '%';
             } else {
                 $v['discount'] = pjUtil::formatCurrencySign($v['discount'], $this->option_arr['o_currency']);
             }
             $v['datetime_valid'] = '';
             switch ($v['valid']) {
                 case 'fixed':
                     $v['datetime_valid'] = date($this->option_arr['o_date_format'], strtotime($v['date_from'])) . ' ' . __('lblFrom', true) . ' ' . date($this->option_arr['o_time_format'], strtotime($v['time_from'])) . ' ' . __('lblTo', true) . ' ' . date($this->option_arr['o_time_format'], strtotime($v['time_to']));
                     break;
                 case 'period':
                     $v['datetime_valid'] = __('lblFrom', true) . ' ' . date($this->option_arr['o_date_format'], strtotime($v['date_from'])) . ' ' . __('lblTo', true) . ' ' . date($this->option_arr['o_date_format'], strtotime($v['date_to']));
                     break;
                 case 'recurring':
                     $days = __('voucher_days', true, false);
                     $v['datetime_valid'] = __('lblEvery', true) . ' ' . $days[$v['every']] . ' ' . __('lblFrom', true) . ' ' . date($this->option_arr['o_time_format'], strtotime($v['time_from'])) . ' ' . __('lblTo', true) . ' ' . date($this->option_arr['o_time_format'], strtotime($v['time_to']));
                     break;
             }
             $data[$k] = $v;
         }
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 public function pjActionGetCoords()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $data = pjAppController::getCoords($_POST['i18n'][$this->getLocaleId()]['address']);
         if (is_array($data['lat']) && $data['lat'][0] == 'NULL' && is_array($data['lng']) && $data['lng'][0] == 'NULL') {
             $data = array();
         }
         pjAppController::jsonResponse($data);
     }
     exit;
 }
 public function pjActionSecureGetUpdate()
 {
     $this->setAjax(true);
     if ($this->isXHR() && $this->isLoged()) {
         $data = array();
         if (isset($_GET['module'])) {
             switch ($_GET['module']) {
                 case 'plugin':
                     if (isset($GLOBALS['CONFIG']['plugins'])) {
                         if (!is_array($GLOBALS['CONFIG']['plugins'])) {
                             $GLOBALS['CONFIG']['plugins'] = array($GLOBALS['CONFIG']['plugins']);
                         }
                         foreach ($GLOBALS['CONFIG']['plugins'] as $plugin) {
                             $data = array_merge($data, self::pjActionGetUpdates(PJ_PLUGINS_PATH . $plugin . '/config/updates', array('plugin' => $plugin)));
                         }
                     }
                     break;
                 case 'script':
                     $data = self::pjActionGetUpdates();
                     break;
             }
         }
         $total = count($data);
         $rowCount = $total;
         $pages = 1;
         $page = 1;
         $offset = 0;
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 public function pjActionDeleteDate()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $response = array();
         if (pjDateModel::factory()->setAttributes(array('id' => $_GET['id']))->erase()->getAffectedRows() == 1) {
             $response['code'] = 200;
         } else {
             $response['code'] = 100;
         }
         pjAppController::jsonResponse($response);
     }
     exit;
 }
 public function pjActionGetUser()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjUserModel = pjUserModel::factory()->where('t1.user_id', $_SESSION['admin_user']['id'])->orWhere('t1.id', $_SESSION['admin_user']['id']);
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = pjObject::escapeString($_GET['q']);
             $pjUserModel->where('t1.email LIKE', "%{$q}%");
             $pjUserModel->orWhere('t1.name LIKE', "%{$q}%");
         }
         if (isset($_GET['status']) && !empty($_GET['status']) && in_array($_GET['status'], array('T', 'F'))) {
             $pjUserModel->where('t1.status', $_GET['status']);
         }
         $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 = $pjUserModel->findCount()->getData();
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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 = array();
         $data = $pjUserModel->select('t1.id, t1.email, t1.name, t1.created, t1.status, t1.is_active, t1.role_id, t2.role')->join('pjRole', 't2.id=t1.role_id', 'left')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         foreach ($data as $k => $v) {
             $v['created'] = date($this->option_arr['o_date_format'], strtotime($v['created'])) . ', ' . date($this->option_arr['o_time_format'], strtotime($v['created']));
             $data[$k] = $v;
         }
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
    public function pjActionGetCategory()
    {
        $this->setAjax(true);
        if ($this->isXHR()) {
            $pjCategoryModel = pjCategoryModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjCategory' 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']);
                $pjCategoryModel->where('t2.content LIKE', "%{$q}%");
            }
            if (isset($_GET['status']) && !empty($_GET['status']) && in_array($_GET['status'], array('T', 'F'))) {
                $pjCategoryModel->where('t1.status', $_GET['status']);
            }
            $column = 'order';
            $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 = $pjCategoryModel->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 = $pjCategoryModel->select('t1.*, t2.content AS name, 
						  (SELECT COUNT(TPC.product_id) FROM `' . pjProductCategoryModel::factory()->getTable() . '` AS TPC WHERE TPC.category_id=t1.id) AS cnt_products')->orderBy("`{$column}` {$direction}")->limit($rowCount, $offset)->findAll()->getData();
            pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
        }
        exit;
    }
 public function pjActionSaveOrder()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $cart = $this->_get('cart');
         $pjOrderModel = pjOrderModel::factory();
         $STORAGE = $_SESSION[$this->defaultStore];
         $FORM = $_SESSION[$this->defaultForm];
         $data = array();
         $data['status'] = $this->option_arr['o_booking_status'];
         $data['price'] = $this->_get('price');
         $data['price_delivery'] = $this->_get('delivery');
         $data['discount'] = $this->_get('discount');
         $data['subtotal'] = $this->_get('subtotal');
         $data['tax'] = $this->_get('tax');
         $data['total'] = $this->_get('total');
         $data['uuid'] = time();
         $data['ip'] = $_SERVER['REMOTE_ADDR'];
         $data['user_id'] = $this->_get('user_id');
         $data['location_id'] = $this->_get('p_location_id');
         switch ($this->_get('type')) {
             case 'pickup':
                 $data['p_dt'] = pjUtil::formatDate($this->_get('p_date'), $this->option_arr['o_date_format']) . " " . $this->_get('p_hour') . ":" . $this->_get('p_minute') . ":00";
                 unset($STORAGE['d_address_1']);
                 unset($STORAGE['d_address_2']);
                 unset($STORAGE['d_country_id']);
                 unset($STORAGE['d_state']);
                 unset($STORAGE['d_city']);
                 unset($STORAGE['d_zip']);
                 unset($STORAGE['d_notes']);
                 unset($STORAGE['d_date']);
                 unset($STORAGE['d_hour']);
                 unset($STORAGE['d_minute']);
                 break;
             case 'delivery':
                 $data['d_dt'] = pjUtil::formatDate($this->_get('d_date'), $this->option_arr['o_date_format']) . " " . $this->_get('d_hour') . ":" . $this->_get('d_minute') . ":00";
                 unset($STORAGE['p_date']);
                 unset($STORAGE['p_hour']);
                 unset($STORAGE['p_minute']);
                 break;
         }
         unset($STORAGE['cart']);
         unset($STORAGE['subtotal']);
         unset($STORAGE['total']);
         unset($STORAGE['delivery']);
         $payment = 'none';
         if (isset($FORM['payment_method'])) {
             if ($FORM['payment_method'] == 'creditcard') {
                 $data['cc_exp'] = $FORM['cc_exp_month'] . "/" . $FORM['cc_exp_year'];
             } else {
                 unset($FORM['cc_type']);
                 unset($FORM['cc_num']);
                 unset($FORM['cc_exp_month']);
                 unset($FORM['cc_exp_year']);
                 unset($FORM['cc_code']);
             }
             $payment = $FORM['payment_method'];
         }
         $is_new_client = false;
         $update_client = false;
         $pjClientModel = pjClientModel::factory();
         $data['client_id'] = ':NULL';
         if ($this->isFrontLogged()) {
             $cnt = $pjClientModel->where('t1.id', $this->getClientId())->findCount()->getData();
             if ($cnt == 0) {
                 $is_new_client = true;
             } else {
                 $update_client = true;
             }
         } else {
             $is_new_client = true;
         }
         if ($is_new_client == true) {
             $c_data = array();
             $c_data['status'] = 'T';
             $c_data['user_id'] = $this->_get('user_id');
             $c_data['c_password'] = pjUtil::getRandomPassword(6);
             $c_data = array_merge($FORM, $c_data);
             $client_id = $pjClientModel->reset()->setAttributes($c_data)->insert()->getInsertId();
             if ($client_id !== false && (int) $client_id > 0) {
                 $data['client_id'] = $client_id;
                 if ($this->isFrontLogged()) {
                     $client = $pjClientModel->reset()->find($client_id)->getData();
                     unset($_SESSION[$this->defaultClient]);
                     $_SESSION[$this->defaultClient] = $client;
                 }
                 pjFront::pjActionConfirmSend($this->option_arr, $c_data, PJ_SALT, 'account');
             }
         }
         if ($update_client == true) {
             if (isset($FORM['update_address'])) {
                 $c_data = array();
                 if (isset($FORM['c_address_1'])) {
                     $c_data['c_address_1'] = $FORM['c_address_1'];
                 }
                 if (isset($FORM['c_address_2'])) {
                     $c_data['c_address_1'] = $FORM['c_address_1'];
                 }
                 if (isset($FORM['c_country'])) {
                     $c_data['c_country'] = $FORM['c_country'];
                 }
                 if (isset($FORM['c_state'])) {
                     $c_data['c_state'] = $FORM['c_state'];
                 }
                 if (isset($FORM['c_city'])) {
                     $c_data['c_city'] = $FORM['c_city'];
                 }
                 if (isset($FORM['c_zip'])) {
                     $c_data['c_zip'] = $FORM['c_zip'];
                 }
                 $pjClientModel->reset()->where('id', $this->getClientId())->limit(1)->modifyAll($c_data);
             }
             if (isset($FORM['update_details'])) {
                 $c_data = array();
                 if (isset($FORM['c_title'])) {
                     $c_data['c_title'] = $FORM['c_title'];
                 }
                 if (isset($FORM['c_name'])) {
                     $c_data['c_name'] = $FORM['c_name'];
                 }
                 if (isset($FORM['c_email'])) {
                     $c_data['c_email'] = $FORM['c_email'];
                 }
                 if (isset($FORM['c_phone'])) {
                     $c_data['c_phone'] = $FORM['c_phone'];
                 }
                 if (isset($FORM['c_company'])) {
                     $c_data['c_company'] = $FORM['c_company'];
                 }
                 if (isset($FORM['c_notes'])) {
                     $c_data['c_notes'] = $FORM['c_notes'];
                 }
                 $pjClientModel->reset()->where('id', $this->getClientId())->limit(1)->modifyAll($c_data);
             }
             $client = $pjClientModel->reset()->find($this->getClientId())->getData();
             unset($_SESSION[$this->defaultClient]);
             $_SESSION[$this->defaultClient] = $client;
             $data['client_id'] = $this->getClientId();
         }
         $data = array_merge($STORAGE, $FORM, $data);
         $order_id = $pjOrderModel->setAttributes($data)->insert()->getInsertId();
         if ($order_id !== false && (int) $order_id > 0) {
             $pjOrderItemModel = pjOrderItemModel::factory();
             $pjProductPriceModel = pjProductPriceModel::factory();
             $pjProductModel = pjProductModel::factory();
             $pjExtraModel = pjExtraModel::factory();
             foreach ($cart as $item) {
                 $price_id = ':NULL';
                 $price = 0;
                 if (!empty($item['price_id'])) {
                     $price_arr = $pjProductPriceModel->find($item['price_id'])->getData();
                     if ($price_arr) {
                         $price_id = $price_arr['id'];
                         $price = $price_arr['price'];
                     }
                 } else {
                     $price_arr = $pjProductModel->reset()->find($item['product_id'])->getData();
                     if (!empty($price_arr)) {
                         $price = $price_arr['price'];
                     }
                 }
                 $hash = md5(uniqid(rand(), true));
                 $oid = $pjOrderItemModel->reset()->setAttributes(array('order_id' => $order_id, 'foreign_id' => $item['product_id'], 'type' => 'product', 'price_id' => $price_id, 'price' => $price, 'hash' => $hash, 'cnt' => $item['cnt']))->insert();
                 foreach ($item['extras'] as $extra_id => $extra_cnt) {
                     if ($extra_cnt > 0) {
                         $extra_price = 0;
                         $extra_arr = $pjExtraModel->reset()->find($extra_id)->getData();
                         if (!empty($extra_arr) && !empty($extra_arr['price'])) {
                             $extra_price = $extra_arr['price'];
                         }
                         $pjOrderItemModel->reset()->setAttributes(array('order_id' => $order_id, 'foreign_id' => $extra_id, 'type' => 'extra', 'price_id' => ':NULL', 'price' => $extra_price, 'hash' => $hash, 'cnt' => $extra_cnt))->insert();
                     }
                 }
             }
             $order_arr = $pjOrderModel->reset()->join('pjClient', "t2.id=t1.client_id", 'left outer')->select('t1.*, t2.c_title, t2.c_email, t2.c_name, t2.c_phone, t2.c_company, t2.c_address_1, t2.c_address_2, t2.c_country, t2.c_state, t2.c_city, t2.c_zip, t2.c_notes')->find($order_id)->getData();
             $pdata = array();
             $pdata['order_id'] = $order_id;
             $pdata['payment_method'] = $payment;
             $pdata['payment_type'] = 'online';
             $pdata['amount'] = $order_arr['total'];
             $pdata['status'] = 'notpaid';
             pjOrderPaymentModel::factory()->setAttributes($pdata)->insert();
             pjAppController::addOrderDetails($order_arr, $this->getLocaleId());
             pjFront::pjActionConfirmSend($this->option_arr, $order_arr, PJ_SALT, 'confirm');
             unset($_SESSION[$this->defaultStore]);
             unset($_SESSION[$this->defaultForm]);
             unset($_SESSION[$this->defaultClient]);
             //Redirect to Credit card payment url.
             if ($payment == 'creditcard') {
                 $cardData = $_SESSION['cardData'];
                 $params = 'amount=' . base64_encode($cardData['total']) . '&oid=' . $cardData['clover_order_id'] . '&mid=' . $cardData['clover_mid'] . '&at=' . $cardData['clover_access_token'] . '&uid=' . base64_encode($cardData['o_user_id']) . '&mname=' . base64_encode($cardData['o_m_name']);
                 $url = PJ_INSTALL_URL . 'payment/creditcard.php?' . $params;
                 $json = array('code' => 200, 'text' => '', 'order_id' => $order_id, 'payment' => $payment, 'path' => $url);
             } else {
                 $json = array('code' => 200, 'text' => '', 'order_id' => $order_id, 'payment' => $payment, 'path' => 'cash');
             }
         } else {
             $json = array('code' => 100, 'text' => '');
         }
         pjAppController::jsonResponse($json);
     }
 }
 public function pjActionGetClient()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $client_arr = pjClientModel::factory()->find($_GET['id'])->getData();
         pjAppController::jsonResponse($client_arr);
     }
     exit;
 }
 public function pjActionSave()
 {
     $this->setAjax(true);
     if ($this->isXHR() && $this->isLoged() && $this->isOneAdminReady()) {
         if (isset($_GET['id']) && (int) $_GET['id'] > 0 && isset($_POST['column'], $_POST['value'])) {
             pjOneAdminModel::factory()->set('id', $_GET['id'])->modify(array($_POST['column'] => $_POST['value']));
             pjAppController::jsonResponse(array('status' => 'OK', 'code' => 201, 'text' => 'Item have been updated.'));
         } else {
             $insert_id = pjOneAdminModel::factory(array('name' => 'Script name', 'url' => 'http://www.example.com/'))->insert()->getInsertId();
             if ($insert_id !== false && (int) $insert_id > 0) {
                 pjAppController::jsonResponse(array('status' => 'OK', 'code' => 200, 'text' => 'Item have been saved.', 'id' => $insert_id));
             }
             pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Item have not been saved'));
         }
     }
     exit;
 }
<?php

$category = '<select name="category_id[]" id="category_id" multiple="multiple" size="5" class="pj-form-field required w300">';
foreach ($tpl['category_arr'] as $v) {
    $category .= sprintf('<option value="%u">%s</option>', $v['id'], stripslashes($v['name']));
}
$category .= '</select>';
$extra = '<select name="extra_id[]" id="extra_id" multiple="multiple" size="5" class="pj-form-field w300">';
foreach ($tpl['extra_arr'] as $v) {
    $extra .= sprintf('<option value="%u">%s</option>', $v['id'], stripslashes($v['name']));
}
$extra .= '</select>';
pjAppController::jsonResponse(compact('category', 'extra'));
 public function pjActionGetClient()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjClientModel = pjClientModel::factory()->where('user_id', $_SESSION['admin_user']['id']);
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = pjObject::escapeString($_GET['q']);
             $pjClientModel->where('t1.c_email LIKE', "%{$q}%");
             $pjClientModel->orWhere('t1.c_name LIKE', "%{$q}%");
         }
         if (isset($_GET['status']) && !empty($_GET['status']) && in_array($_GET['status'], array('T', 'F'))) {
             $pjClientModel->where('t1.status', $_GET['status']);
         }
         $column = 'c_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 = $pjClientModel->findCount()->getData();
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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 = array();
         $data = $pjClientModel->select("t1.id, t1.c_email, t1.c_name, t1.status, (SELECT COUNT(TO.client_id) FROM `" . pjOrderModel::factory()->getTable() . "` AS `TO` WHERE `TO`.client_id=t1.id) AS cnt_orders")->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 public function pjActionSaveLocale()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $response = array();
         if (isset($_GET['id']) && (int) $_GET['id'] > 0) {
             pjLocaleModel::factory()->where('id', $_GET['id'])->limit(1)->modifyAll(array($_POST['column'] => $_POST['value']));
             $response['code'] = 201;
         } else {
             $pjLocaleModel = pjLocaleModel::factory();
             $arr = $pjLocaleModel->select('t1.sort')->orderBy('t1.sort DESC')->limit(1)->findAll()->getData();
             $sort = 1;
             if (count($arr) === 1) {
                 $sort = (int) $arr[0]['sort'] + 1;
             }
             $lang = pjLocaleLanguageModel::factory()->where(sprintf("t1.iso NOT IN (SELECT `language_iso` FROM `%s`)", $pjLocaleModel->getTable()))->where('t1.file IS NOT NULL')->orderBy('t1.title ASC')->limit(1)->findAll()->getDataPair(null, 'iso');
             $insert_id = pjLocaleModel::factory(array('sort' => $sort, 'is_default' => '0', 'language_iso' => @$lang[0]))->insert()->getInsertId();
             if ($insert_id !== false && (int) $insert_id > 0) {
                 $response['code'] = 200;
                 $response['id'] = $insert_id;
                 $locale_id = NULL;
                 $arr = $pjLocaleModel->reset()->findAll()->getData();
                 foreach ($arr as $locale) {
                     if ($locale['language_iso'] == 'en') {
                         $locale_id = $locale['id'];
                         break;
                     }
                 }
                 if (is_null($locale_id) && count($arr) > 0) {
                     $locale_id = $arr[0]['id'];
                 }
                 if (!is_null($locale_id)) {
                     $sql = sprintf("INSERT INTO `%1\$s` (`foreign_id`, `model`, `locale`, `field`, `content`)\n\t\t\t\t\t\t\tSELECT t1.foreign_id, t1.model, :insert_id, t1.field, t1.content\n\t\t\t\t\t\t\tFROM `%1\$s` AS t1\n\t\t\t\t\t\t\tWHERE t1.locale = :locale", pjMultiLangModel::factory()->getTable());
                     pjMultiLangModel::factory()->prepare($sql)->exec(array('insert_id' => $insert_id, 'locale' => (int) $locale_id));
                     $this->pjActionUpdateFieldsIndex();
                 }
             } else {
                 $response['code'] = 100;
             }
         }
         pjAppController::jsonResponse($response);
     }
     exit;
 }
 public function pjActionGetCountry()
 {
     $this->setAjax(true);
     if ($this->isXHR() && $this->isLoged()) {
         $pjCountryModel = pjCountryModel::factory()->join('pjMultiLang', "t2.foreign_id = t1.id AND t2.model = 'pjCountry' AND t2.locale = '" . $this->getLocaleId() . "' AND t2.field = 'name'", 'left');
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = $pjCountryModel->escapeString($_GET['q']);
             $q = str_replace(array('%', '_'), array('\\%', '\\_'), $q);
             $pjCountryModel->where(sprintf("(t1.alpha_2 LIKE '%1\$s' OR t1.alpha_3 LIKE '%1\$s' OR t2.content LIKE '%1\$s')", "%{$q}%"));
         }
         if (isset($_GET['status']) && in_array($_GET['status'], array('T', 'F'))) {
             $pjCountryModel->where('t1.status', $_GET['status']);
         }
         $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 = (int) $pjCountryModel->findCount()->getData();
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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 = $pjCountryModel->select('t1.*, t2.content AS name')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
    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 pjActionGet()
 {
     $this->setAjax(true);
     if ($this->isXHR() && $this->isLoged() && $this->isAdmin()) {
         $column = 'created';
         $direction = 'DESC';
         if (isset($_GET['direction']) && isset($_GET['column']) && in_array(strtoupper($_GET['direction']), array('ASC', 'DESC'))) {
             $column = $_GET['column'];
             $direction = strtoupper($_GET['direction']);
         }
         $data = $id = $created = $type = array();
         if ($handle = opendir(PJ_WEB_PATH . 'backup')) {
             $i = 0;
             while (false !== ($entry = readdir($handle))) {
                 preg_match('/(database-backup|files-backup)-(\\d{10})\\.(sql|zip)/', $entry, $m);
                 if (isset($m[2])) {
                     $id[$i] = $entry;
                     $created[$i] = date($this->option_arr['o_date_format'] . ", H:i", $m[2]);
                     $type[$i] = $m[1] == 'database-backup' ? 'database' : 'files';
                     $data[$i]['id'] = $id[$i];
                     $data[$i]['created'] = $created[$i];
                     $data[$i]['type'] = $type[$i];
                     $i++;
                 }
             }
             closedir($handle);
         }
         switch ($column) {
             case 'created':
                 array_multisort($created, $direction == 'ASC' ? SORT_ASC : SORT_DESC, $id, SORT_DESC, $type, SORT_ASC, $data);
                 break;
             case 'type':
                 array_multisort($type, $direction == 'ASC' ? SORT_ASC : SORT_DESC, $id, SORT_DESC, $created, SORT_DESC, $data);
                 break;
             case 'id':
                 array_multisort($id, $direction == 'ASC' ? SORT_ASC : SORT_DESC, $type, SORT_ASC, $created, SORT_DESC, $data);
                 break;
         }
         $total = count($data);
         $rowCount = isset($_GET['rowCount']) && (int) $_GET['rowCount'] > 0 ? (int) $_GET['rowCount'] : 10;
         $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;
         }
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }