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 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 pjActionGetLocale()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         if (isset($_GET['locale']) && (int) $_GET['locale'] > 0) {
             pjAppController::setFields($_GET['locale']);
             $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='" . pjObject::escapeString($_GET['locale']) . "'", '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='" . pjObject::escapeString($_GET['locale']) . "'", 'left outer')->orderBy('name ASC')->findAll()->getData());
         }
     }
 }
 public function pjActionGetLocation()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjLocationModel = 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')->where('user_id', $_SESSION['admin_user']['id']);
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = pjObject::escapeString($_GET['q']);
             $pjLocationModel->where('t2.content LIKE', "%{$q}%");
             $pjLocationModel->orWhere('t3.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 = $pjLocationModel->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 = $pjLocationModel->select('t1.*, t2.content AS name, t3.content AS address')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 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 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 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 pjActionGetOrder()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjOrderModel = pjOrderModel::factory()->join('pjClient', "t2.id=t1.client_id", 'left outer')->where('t1.user_id', $_SESSION['admin_user']['id']);
         if (isset($_GET['q']) && !empty($_GET['q'])) {
             $q = pjObject::escapeString($_GET['q']);
             $pjOrderModel->where("(t1.id = '{$q}' OR t1.uuid = '{$q}' OR t1.c_name LIKE '%{$q}%' OR t1.c_email LIKE '%{$q}%')");
         }
         $lc_arr = explode(',', $_GET['location_id']);
         if (empty($_GET['fromDate']) && empty($_GET['toDate'])) {
             if (isset($_GET['status']) && !empty($_GET['status']) && in_array($_GET['status'], array('confirmed', 'cancelled', 'pending'))) {
                 $pjOrderModel->where('t1.status', $_GET['status']);
             }
             if (isset($_GET['client_id']) && (int) $_GET['client_id'] > 0) {
                 $pjOrderModel->where('t1.client_id', $_GET['client_id']);
             }
             if (isset($_GET['type']) && !empty($_GET['type'])) {
                 $pjOrderModel->where('t1.type', $_GET['type']);
             }
             if (isset($_GET['location_id']) && !empty($_GET['location_id'])) {
                 $condition = '';
                 foreach ($lc_arr as $row) {
                     $condition .= 't1.location_id=' . $row . ' OR ';
                 }
                 $condition = substr($condition, 0, strlen($condition) - 4);
                 $pjOrderModel->where($condition);
             }
         } else {
             $formDate = date('Y-m-d', strtotime($_GET['fromDate']));
             $toDate = date('Y-m-d', strtotime($_GET['toDate']));
             if (isset($_GET['status']) && !empty($_GET['status'])) {
                 $pjOrderModel->where('t1.status', $_GET['status']);
                 $pjOrderModel->where("date(t1.p_dt) BETWEEN '" . $formDate . "' AND '" . $toDate . "'");
             }
             if (isset($_GET['client_id']) && (int) $_GET['client_id'] > 0) {
                 $pjOrderModel->where('t1.client_id', $_GET['client_id']);
                 $pjOrderModel->where("date(t1.p_dt) BETWEEN '" . $formDate . "' AND '" . $toDate . "'");
             }
             if (isset($_GET['type']) && !empty($_GET['type'])) {
                 $pjOrderModel->where('t1.type', $_GET['type']);
                 $pjOrderModel->where("date(t1.p_dt) BETWEEN '" . $formDate . "' AND '" . $toDate . "'");
             }
             if (isset($_GET['location_id']) && !empty($_GET['location_id'])) {
                 $condition = '';
                 foreach ($lc_arr as $row) {
                     $condition .= 't1.location_id=' . $row . ' OR ';
                 }
                 $condition = substr($condition, 0, strlen($condition) - 4);
                 $pjOrderModel->where($condition);
                 $pjOrderModel->where("date(t1.p_dt) BETWEEN '" . $formDate . "' AND '" . $toDate . "'");
             }
         }
         $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 = $pjOrderModel->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 = $pjOrderModel->select('t1.*, t2.c_name as client_name')->orderBy("{$column} {$direction}")->limit($rowCount, $offset)->findAll()->getData();
         foreach ($data as $k => $v) {
             $data[$k]['total'] = pjUtil::formatCurrencySign($v['total'], $this->option_arr['o_currency']);
             if ($v['type'] == 'delivery') {
                 $data[$k]['datetime'] = pjUtil::formatDate(date("Y-m-d", strtotime($v['d_dt'])), "Y-m-d", $this->option_arr['o_date_format']) . ', ' . pjUtil::formatTime(date("H:i:s", strtotime($v['d_dt'])), "H:i:s", $this->option_arr['o_time_format']);
             } else {
                 if ($v['type'] == 'pickup') {
                     $data[$k]['datetime'] = pjUtil::formatDate(date("Y-m-d", strtotime($v['p_dt'])), "Y-m-d", $this->option_arr['o_date_format']) . ', ' . pjUtil::formatTime(date("H:i:s", strtotime($v['p_dt'])), "H:i:s", $this->option_arr['o_time_format']);
                 }
             }
         }
         pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));
     }
     exit;
 }
 public function pjActionCheckLogin()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $pjClientModel = pjClientModel::factory();
         $client = $pjClientModel->where('t1.c_email', $_POST['login_email'])->where(sprintf("t1.c_password = AES_ENCRYPT('%s', '%s')", pjObject::escapeString($_POST['login_password']), PJ_SALT))->limit(1)->findAll()->getData();
         $resp = array();
         if (count($client) != 1) {
             $resp['code'] = 100;
         } else {
             if ($client[0]['status'] != 'T') {
                 $resp['code'] = 101;
             } else {
                 $last_login = date("Y-m-d H:i:s");
                 $_SESSION[$this->defaultClient] = $client[0];
                 $data = array();
                 $data['last_login'] = $last_login;
                 $pjClientModel->reset()->setAttributes(array('id' => $client[0]['id']))->modify($data);
                 $resp['code'] = 200;
             }
         }
         pjAppController::jsonResponse($resp);
     }
     exit;
 }
 public function pjActionLogin()
 {
     $this->setLayout('pjActionAdminLogin');
     if (isset($_POST['login_user'])) {
         if (!isset($_POST['login_email']) || !isset($_POST['login_password']) || !pjValidation::pjActionNotEmpty($_POST['login_email']) || !pjValidation::pjActionNotEmpty($_POST['login_password']) || !pjValidation::pjActionEmail($_POST['login_email'])) {
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionLogin&err=4");
         }
         $pjUserModel = pjUserModel::factory();
         $user = $pjUserModel->where('t1.email', $_POST['login_email'])->where(sprintf("t1.password = AES_ENCRYPT('%s', '%s')", pjObject::escapeString($_POST['login_password']), PJ_SALT))->limit(1)->findAll()->getData();
         if (count($user) != 1) {
             pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionLogin&err=1");
         } else {
             $user = $user[0];
             unset($user['password']);
             if (!in_array($user['role_id'], array(1, 2, 3))) {
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionLogin&err=2");
             }
             if ($user['role_id'] == 3 && $user['is_active'] == 'F') {
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionLogin&err=2");
             }
             if ($user['status'] != 'T') {
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionLogin&err=3");
             }
             $last_login = date("Y-m-d H:i:s");
             $_SESSION[$this->defaultUser] = $user;
             $data = array();
             $data['last_login'] = $last_login;
             $pjUserModel->reset()->setAttributes(array('id' => $user['id']))->modify($data);
             if ($this->isAdmin() || $this->isEditor()) {
                 //Let's check get data from clover.
                 if (array_key_exists('cloverData', $_SESSION)) {
                     pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminSignUp&action=pjActionMain&case=login");
                 }
                 pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdmin&action=pjActionIndex");
             }
         }
     } else {
         $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
         $this->appendJs('pjAdmin.js');
     }
 }