public function pjActionCreate()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         if (isset($_POST['order_create'])) {
             $pjOrderModel = pjOrderModel::factory();
             $data = array();
             $data['uuid'] = time();
             $data['ip'] = pjUtil::getClientIp();
             if (!isset($_POST['client_id']) || isset($_POST['client_id']) && $_POST['client_id'] == '') {
                 $c_data = array();
                 $c_data['c_title'] = isset($_POST['c_title']) ? $_POST['c_title'] : ':NULL';
                 $c_data['c_name'] = isset($_POST['c_name']) ? $_POST['c_name'] : ':NULL';
                 $c_data['c_email'] = isset($_POST['c_email']) ? $_POST['c_email'] : ':NULL';
                 $c_data['c_password'] = pjUtil::getRandomPassword(6);
                 $c_data['c_phone'] = isset($_POST['c_phone']) ? $_POST['c_phone'] : ':NULL';
                 $c_data['c_address_1'] = isset($_POST['c_address_1']) ? $_POST['c_address_1'] : ':NULL';
                 $c_data['c_address_2'] = isset($_POST['c_address_2']) ? $_POST['c_address_2'] : ':NULL';
                 $c_data['c_city'] = isset($_POST['c_city']) ? $_POST['c_city'] : ':NULL';
                 $c_data['c_state'] = isset($_POST['c_state']) ? $_POST['c_state'] : ':NULL';
                 $c_data['c_zip'] = isset($_POST['c_zip']) ? $_POST['c_zip'] : ':NULL';
                 $c_data['c_country'] = isset($_POST['c_country']) ? $_POST['c_country'] : ':NULL';
                 $c_data['status'] = 'T';
                 if ($c_data['c_email'] != ':NULL') {
                     $pjClientModel = pjClientModel::factory();
                     $client_id = $pjClientModel->setAttributes($c_data)->insert()->getInsertId();
                     if ($client_id !== false && (int) $client_id > 0) {
                         $data['client_id'] = $client_id;
                         $client_arr = $pjClientModel->reset()->find($client_id)->getData();
                         $tokens = pjAppController::getClientTokens($this->option_arr, $client_arr, PJ_SALT, $this->getLocaleId());
                         $pjMultiLangModel = pjMultiLangModel::factory();
                         $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $this->getLocaleId())->where('t1.field', 'o_email_account_message')->limit(0, 1)->findAll()->getData();
                         $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $this->getLocaleId())->where('t1.field', 'o_email_account_subject')->limit(0, 1)->findAll()->getData();
                         if (count($lang_message) === 1 && count($lang_subject) === 1) {
                             $message = str_replace($tokens['search'], $tokens['replace'], $lang_message[0]['content']);
                             $Email = new pjEmail();
                             if ($this->option_arr['o_send_email'] == 'smtp') {
                                 $Email->setTransport('smtp')->setSmtpHost($this->option_arr['o_smtp_host'])->setSmtpPort($this->option_arr['o_smtp_port'])->setSmtpUser($this->option_arr['o_smtp_user'])->setSmtpPass($this->option_arr['o_smtp_pass']);
                             }
                             $Email->setContentType('text/html');
                             $Email->setTo($c_data['c_email'])->setFrom($this->getAdminEmail())->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
                         }
                     }
                 }
             }
             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']);
                         $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['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'];
             }
             $id = pjOrderModel::factory(array_merge($_POST, $data))->insert()->getInsertId();
             if ($id !== false && (int) $id > 0) {
                 if (isset($_POST['product_id']) && count($_POST['product_id']) > 0) {
                     $pjOrderItemModel = pjOrderItemModel::factory();
                     $pjProductPriceModel = pjProductPriceModel::factory();
                     $pjProductModel = pjProductModel::factory();
                     $pjExtraModel = pjExtraModel::factory();
                     foreach ($_POST['product_id'] as $k => $pid) {
                         $product = $pjProductModel->reset()->find($pid)->getData();
                         if (strpos($k, 'new_') === 0) {
                             $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'];
                             }
                             $hash = md5(uniqid(rand(), true));
                             $oid = $pjOrderItemModel->reset()->setAttributes(array('order_id' => $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' => $id, 'foreign_id' => $eid, 'type' => 'extra', 'hash' => $hash, 'price_id' => ':NULL', 'price' => $extra_price, 'cnt' => $_POST['extra_cnt'][$k][$i]))->insert();
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $err = 'AR03';
             } else {
                 $err = 'AR04';
             }
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminOrders&action=pjActionIndex&err={$err}");
         } else {
             $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")->orderBy("name ASC")->findAll()->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);
             $client_arr = pjClientModel::factory()->where('t1.status', 'T')->orderBy('t1.c_name ASC')->findAll()->getData();
             $this->set('client_arr', $client_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('pjAdminOrders.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionConfirmSend($option_arr, $data, $salt, $opt)
 {
     $Email = new pjEmail();
     if ($option_arr['o_send_email'] == 'smtp') {
         $Email->setTransport('smtp')->setSmtpHost($option_arr['o_smtp_host'])->setSmtpPort($option_arr['o_smtp_port'])->setSmtpUser($option_arr['o_smtp_user'])->setSmtpPass($option_arr['o_smtp_pass']);
     }
     $Email->setContentType('text/html');
     $pjMultiLangModel = pjMultiLangModel::factory();
     $admin_email = $this->getAdminEmail();
     $admin_phone = $this->getAdminPhone();
     $from_email = $admin_email;
     if (!empty($option_arr['o_sender_email'])) {
         $from_email = $option_arr['o_sender_email'];
     }
     $locale_id = isset($booking_arr['locale_id']) && (int) $booking_arr['locale_id'] > 0 ? (int) $booking_arr['locale_id'] : $this->getLocaleId();
     if ($opt == 'account' || $opt == 'forgot') {
         $tokens = pjAppController::getClientTokens($option_arr, $data, PJ_SALT, $this->getLocaleId());
         $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_' . $opt . '_message')->limit(0, 1)->findAll()->getData();
         $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_' . $opt . '_subject')->limit(0, 1)->findAll()->getData();
         if (count($lang_message) === 1 && count($lang_subject) === 1) {
             $message = str_replace($tokens['search'], $tokens['replace'], $lang_message[0]['content']);
             $Email->setTo($data['c_email'])->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
         }
     } else {
         $tokens = pjAppController::getTokens($option_arr, $data, PJ_SALT, $this->getLocaleId());
         if ($option_arr['o_email_confirmation'] == 1 && $opt == 'confirm') {
             $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_confirmation_message')->limit(0, 1)->findAll()->getData();
             $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_confirmation_subject')->limit(0, 1)->findAll()->getData();
             if (count($lang_message) === 1 && count($lang_subject) === 1) {
                 if ($data['type'] == 'delivery') {
                     $message = str_replace(array('[Delivery]', '[/Delivery]'), array('', ''), $lang_message[0]['content']);
                 } else {
                     $message = preg_replace('/\\[Delivery\\].*\\[\\/Delivery\\]/s', '', $lang_message[0]['content']);
                 }
                 $message = str_replace($tokens['search'], $tokens['replace'], $message);
                 $Email->setTo($data['c_email'])->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
             }
         }
         if ($option_arr['o_admin_email_confirmation'] == 1 && $opt == 'confirm') {
             $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_email_confirmation_message')->limit(0, 1)->findAll()->getData();
             $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_email_confirmation_subject')->limit(0, 1)->findAll()->getData();
             if (count($lang_message) === 1 && count($lang_subject) === 1) {
                 if ($data['type'] == 'delivery') {
                     $message = str_replace(array('[Delivery]', '[/Delivery]'), array('', ''), $lang_message[0]['content']);
                 } else {
                     $message = preg_replace('/\\[Delivery\\].*\\[\\/Delivery\\]/s', '', $lang_message[0]['content']);
                 }
                 $message = str_replace($tokens['search'], $tokens['replace'], $message);
                 $Email->setTo($admin_email)->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
             }
             if (!empty($admin_phone)) {
                 $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_sms_confirmation_message')->limit(0, 1)->findAll()->getData();
                 if (count($lang_message) === 1) {
                     $message = str_replace($tokens['search'], $tokens['replace'], $lang_message[0]['content']);
                     $params = array('text' => $message, 'key' => md5($option_arr['private_key'] . PJ_SALT));
                     $params['number'] = $admin_phone;
                     $this->requestAction(array('controller' => 'pjSms', 'action' => 'pjActionSend', 'params' => $params), array('return'));
                 }
             }
         }
         if ($option_arr['o_email_payment'] == 1 && $opt == 'payment') {
             $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_payment_message')->limit(0, 1)->findAll()->getData();
             $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_payment_subject')->limit(0, 1)->findAll()->getData();
             if (count($lang_message) === 1 && count($lang_subject) === 1) {
                 if ($data['type'] == 'delivery') {
                     $message = str_replace(array('[Delivery]', '[/Delivery]'), array('', ''), $lang_message[0]['content']);
                 } else {
                     $message = preg_replace('/\\[Delivery\\].*\\[\\/Delivery\\]/s', '', $lang_message[0]['content']);
                 }
                 $message = str_replace($tokens['search'], $tokens['replace'], $message);
                 $Email->setTo($data['c_email'])->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
             }
         }
         if ($option_arr['o_admin_email_payment'] == 1 && $opt == 'payment') {
             $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_email_payment_message')->limit(0, 1)->findAll()->getData();
             $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_email_payment_subject')->limit(0, 1)->findAll()->getData();
             if (count($lang_message) === 1 && count($lang_subject) === 1) {
                 if ($data['type'] == 'delivery') {
                     $message = str_replace(array('[Delivery]', '[/Delivery]'), array('', ''), $lang_message[0]['content']);
                 } else {
                     $message = preg_replace('/\\[Delivery\\].*\\[\\/Delivery\\]/s', '', $lang_message[0]['content']);
                 }
                 $message = str_replace($tokens['search'], $tokens['replace'], $message);
                 $Email->setTo($admin_email)->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
             }
             if (!empty($admin_phone)) {
                 $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_sms_payment_message')->limit(0, 1)->findAll()->getData();
                 if (count($lang_message) === 1) {
                     $message = str_replace($tokens['search'], $tokens['replace'], $lang_message[0]['content']);
                     $params = array('text' => $message, 'key' => md5($option_arr['private_key'] . PJ_SALT));
                     $params['number'] = $admin_phone;
                     $this->requestAction(array('controller' => 'pjSms', 'action' => 'pjActionSend', 'params' => $params), array('return'));
                 }
             }
         }
         if ($option_arr['o_email_cancel'] == 1 && $opt == 'cancel') {
             $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_cancel_message')->limit(0, 1)->findAll()->getData();
             $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_email_cancel_subject')->limit(0, 1)->findAll()->getData();
             if (count($lang_message) === 1 && count($lang_subject) === 1) {
                 if ($data['type'] == 'delivery') {
                     $message = str_replace(array('[Delivery]', '[/Delivery]'), array('', ''), $lang_message[0]['content']);
                 } else {
                     $message = preg_replace('/\\[Delivery\\].*\\[\\/Delivery\\]/s', '', $lang_message[0]['content']);
                 }
                 $message = str_replace($tokens['search'], $tokens['replace'], $message);
                 $Email->setTo($data['c_email'])->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
             }
         }
         if ($option_arr['o_admin_email_cancel'] == 1 && $opt == 'cancel') {
             $lang_message = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_email_cancel_message')->limit(0, 1)->findAll()->getData();
             $lang_subject = $pjMultiLangModel->reset()->select('t1.*')->where('t1.model', 'pjOption')->where('t1.locale', $locale_id)->where('t1.field', 'o_admin_email_cancel_subject')->limit(0, 1)->findAll()->getData();
             if (count($lang_message) === 1 && count($lang_subject) === 1) {
                 if ($data['type'] == 'delivery') {
                     $message = str_replace(array('[Delivery]', '[/Delivery]'), array('', ''), $lang_message[0]['content']);
                 } else {
                     $message = preg_replace('/\\[Delivery\\].*\\[\\/Delivery\\]/s', '', $lang_message[0]['content']);
                 }
                 $message = str_replace($tokens['search'], $tokens['replace'], $message);
                 $Email->setTo($admin_email)->setFrom($from_email)->setSubject($lang_subject[0]['content'])->send(pjUtil::textToHtml($message));
             }
         }
     }
 }