/trunk/chimbien/app/controllers/travels_controller.php
PHP | 224 lines | 201 code | 19 blank | 4 comment | 34 complexity | da2cc22482fbb2f8a963c6580014ff0d MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, BSD-3-Clause
- <?php
- class TravelsController extends AppController {
- var $name = 'Travels';
-
- function _doUpload($file) {
- $this->autoRender = false;
- Configure::write('debug', 0);
- $uploadsDir = 'uploads/tours';
- if ($this->params['action'] == 'admin_edit') {
- if ($this->data['Travel']['delete_image'] == 1) {
- $this->Upload->delete_image($uploadsDir, $this->data['Travel']['value_image']);
- $this->Upload->delete_image($uploadsDir . '/thumb/', $this->data['Travel']['value_image']);
- $this->data['Travel']['image']='';
- }else{
- unset($this->data['Travel']['image']);
- }
- }
- if (!empty($file['name'])) {
- $file = $this->Upload->uploadImage($uploadsDir, $file, '230x0');
- if (array_key_exists('filename', $file)) {
- $this->data['Travel']['image'] = $file['filename'];
- } else {
- return false;
- }
- }
- }
- function admin_index() {
- //filter
- $keyword = trim($this->data['Search']['keyword']);
- $category_id = $this->data['Search']['category_id'];
- $conds = array();
- if(!empty($category_id)){
- $conds['Travel.category_id']=$category_id;
- }
- $conds['Travel.option']=0;
- if (!empty($keyword)) {
- $conds['OR'] = array(
- 'lower(CONVERT(I18n__name.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(I18n__times.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(I18n__transport.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(I18n__price.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(I18n__description.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- );
- }
- //End filter
- $this->Travel->setLanguage();
- $travels = $this->Travel->find('all', array('conditions' => $conds, 'recursive' => 0, 'order' => 'Travel.created desc'));
- $categories = $this->listTour('all');
- $this->set(compact('travels','categories'));
- }
- function admin_view($id = null) {
- $this->layout= 'ajax';
- if (!$id) {
- $this->Session->setFlash(__('Invalid travel', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- $this->set('travel', $this->Travel->read(null, $id));
- }
- function admin_add() {
- if (!empty($this->data)) {
- $this->_doUpload($this->data['Travel']['image']);
- $this->data['Travel']['option'] = 0;
- $this->Travel->create();
- if ($this->Travel->save($this->data)) {
- $this->Session->setFlash(__('The travel has been saved', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The travel could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
- }
- }
- $categories = $this->listTour();
- $this->set(compact('categories'));
- }
- function admin_edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid travel', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- if (!empty($this->data)) {
- $this->_doUpload($this->data['Travel']['image']);
- if ($this->Travel->save($this->data)) {
- $this->Session->setFlash(__('The travel has been saved', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The travel could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
- }
- }
- if (empty($this->data)) {
- $this->Travel->setLanguage();
- $this->data = $this->Travel->read(null, $id);
- }
- $categories = $this->listTour();
- $this->set(compact('categories'));
- }
- function admin_delete($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid id for travel', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- if ($this->Travel->delete($id)) {
- $this->Session->setFlash(__('Travel deleted', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- }
- $this->Session->setFlash(__('Travel was not deleted', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- function admin_deleteAll($ids = null) {
- if (!empty($ids)) {
- $id = explode('-', $ids);
- //delete image
- $travels = $this->Travel->find('all', array('conditions' => array('Travel.id' => $id)));
- foreach ($travels as $travel) {
- $this->Upload->delete_image('uploads/tours/', $travel['Travel']['image']);
- $this->Upload->delete_image('uploads/tours/thumb/', $travel['Travel']['image']);
- }
- //end delete image
- $this->Travel->deleteAll(array('Travel.id' => $id));
- $this->Session->setFlash(__('Travels deleted', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- }
- $this->redirect(array('action' => 'index'));
- }
-
- function admin_changeStatusAll($ids = null, $status=null) {
- if (!empty($ids)) {
- $id = explode('-', $ids);
- $this->Travel->updateAll(array('Travel.hot' => $status), array('Travel.id' => $id));
- }
- $this->Session->setFlash(sprintf(__('%s Change hot tour Successfull.', true), 'Travel'), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- }
- function admin_changestatus() {
- Configure::write('debug', 0);
- $this->autoRender = false;
- $id = $this->params['form']['id'];
- $status = $this->params['form']['status'];
- $this->Travel->updateAll(array('Travel.hot' => $status), array('Travel.id' => $id));
- $return = array();
- $return['id'] = $id;
- $return['newstatus'] = ($status == 0) ? 1 : 0;
- echo json_encode($return);
- }
-
- function showTour($hot=false){
- $this->Travel->setLanguage();
- $this->Travel->recursive = -1;
- if($hot==false){
- $tour = $this->Travel->find('all',array('conditions'=>array('Travel.option'=>0),'order'=>'Travel.created desc'));
- }else{
- $tour = $this->Travel->find('all',array('conditions'=>array('Travel.hot'=>1,'Travel.option'=>0),'order'=>'Travel.created desc','limit'=>6));
- }
- return $tour;
- }
-
- function category(){
- if(!empty($this->params['id'])){
- $this->Travel->Category->setLanguage();
- $category = $this->Travel->Category->find('first',array('conditions'=>array('Category.id'=>$this->params['id']),'recursive' => -1));
- $this->paginate = array('conditions'=>array('Travel.category_id'=>$this->params['id']),'recursive' => -1,'limit'=>1);
- $travels = $this->paginate();
- if(count($travels)>0){
- $category['Category']['folder'] = $this->checkFolderImage($travels[0]['Travel']['option']);
- }
- $this->set(compact('category'));
- $this->set(compact('travels'));
- }
- }
-
- function checkFolderImage($option=null){
- if($option==0){
- return 'tours';
- }else if($option==1){
- return 'hotels';
- }else if($option==2){
- return 'services';
- }
- return 'tours';
- }
-
- function listTour($all=null){
- $this->Travel->Category->setLanguage();
- $categories1 = $this->Travel->Category->children(1);
- if($categories1==false){
- $categories1 = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(1))));
- }
- $categories3 = $this->Travel->Category->children(3);
- if($categories3==false){
- $categories3 = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(3))));
- }
- $categories4 = $this->Travel->Category->children(4);
- if($categories4==false){
- $categories4 = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(4))));
- }
- $categories = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(1,3,4))));
- foreach($categories1 as $key=>$value){
- $categories[$key] = $value;
- }
- foreach($categories3 as $key=>$value){
- $categories[$key] = $value;
- }
- foreach($categories4 as $key=>$value){
- $categories[$key] = $value;
- }
- if(!empty($all)){
- $categories[0] = 'Tá??t cá??';;
- }
- ksort($categories);
- return $categories;
- }
- }
- ?>