/trunk/chimbien/app/controllers/categories_controller.php
PHP | 237 lines | 212 code | 18 blank | 7 comment | 32 complexity | e965474424ba580a4aa505887ccd46e4 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, BSD-3-Clause
- <?php
- class CategoriesController extends AppController {
- var $name = 'Categories';
- function admin_test() {
- $categories = $this->Category->find('threaded',array('recursive' => -1));
- $this->set(compact('categories'));
- $this->set('len',count($categories));
- }
- function admin_moveup($id=null) {
- $move= $this->Category->moveUp($id,1);
- $this->redirect(array('action'=>'index'));
- }
- function admin_movedown($id=null) {
- $move= $this->Category->moveDown($id,1);
- $this->redirect(array('action'=>'index'));
- }
-
- function admin_index() {
- $this->Category->setLanguage();
- $categories = $this->Category->find('threaded',array('recursive' => -1));
- $this->set(compact('categories'));
- $this->set('len',count($categories));
- }
- function admin_view($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid category', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- $this->set('category', $this->Category->read(null, $id));
- }
- function admin_add() {
- if (!empty($this->data)) {
- $this->data['Category']['link'] = Inflector::slug(strtolower($this->data['Category']['link']),'-');
- $this->Category->create();
- if ($this->Category->save($this->data)) {
- $this->Session->setFlash(__('The category has been saved', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The category could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
- }
- }
- $this->Category->setLanguage('vie');
- $parents = $this->Category->find('list',array('conditions'=>array('parent_id'=>0),'recursive' => -1));
- //$parents[0] = 'Menu Cha';
- ksort($parents);
- //
- $this->loadModel('Page');
- $this->Page->setLanguage('eng');
- $pages = $this->Page->find('list',array('fields'=>array('Page.name')));
- $links = array();
- foreach($pages as $page){
- $links[Inflector::slug(strtolower($page),'-')] = $page;
- }
- $this->set(compact('parents','links'));
- }
- function admin_edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid category', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- if (!empty($this->data)) {
- if ($this->Category->save($this->data)) {
- $this->Category->updateAll(array('Category.parent_id'=>0),array('Category.parent_id'=>NULL));
- $this->Session->setFlash(__('The category has been saved', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The category could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
- }
- }
- $this->Category->setLanguage();
- if (empty($this->data)) {
- $this->data = $this->Category->read(null, $id);
- }
- $parents = $this->Category->find('list',array('conditions'=>array('parent_id'=>0),'recursive' => -1));
- $parents[0] = 'Menu Cha';
- ksort($parents);
- //
- $this->loadModel('Page');
- $this->Page->setLanguage('eng');
- $pages = $this->Page->find('list',array('fields'=>array('Page.name')));
- $links = array();
- foreach($pages as $page){
- $links[Inflector::slug(strtolower($page),'-')] = $page;
- }
- ksort($links);
- $this->set(compact('parents','links'));
- }
- function admin_delete($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid id for category', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- if ($this->Category->delete($id)) {
- $this->Session->setFlash(__('Category deleted', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- }
- $this->Session->setFlash(__('Category was not deleted', true), 'admin/flasherror', '', 'error');
- $this->redirect(array('action' => 'index'));
- }
- function admin_deleteAll($ids = null) {
- if (!empty($ids)) {
- $id = explode('-', $ids);
- $this->Category->deleteAll(array('Category.id' => $id));
- $this->Session->setFlash(__('Categories deleted', true), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- }
- $this->redirect(array('action' => 'index'));
- }
-
- function getTree(){
- $this->Category->setLanguage();
- return $this->Category->find('threaded',array('recursive' => -1));
- }
-
- function category($id=null){
- if(!empty($id)){
- $this->params['id']=$id;
- }
- if(!empty($this->params['id'])){
- $this->Category->setLanguage();
- $category = $this->Category->find('first',array('conditions'=>array('Category.id'=>$this->params['id']),'recursive' => -1));
- $this->Category->Travel->setLanguage();
- $this->paginate['Travel'] = array('conditions'=>array('Travel.category_id'=>$this->params['id']),'recursive' => -1,'limit'=>10);
- $travels = $this->paginate('Travel');
- if(count($travels)>0){
- $category['Category']['folder'] = $this->checkFolderImage($travels[0]['Travel']['option']);
- }
- $this->set(compact('category'));
- $this->set(compact('travels'));
- $this->set('title_for_layout',$category['Category']['name']);
- }
- }
-
- function search(){
- $conds = array();
- if(!empty($this->data)){
- $keyword = trim($this->data['Search']['keyword']);
- $category = $this->data['Search']['category'];
- $rating = $this->data['Search']['rating'];
- $option = $this->data['Search']['option'];
- if($this->Session->check('Search')){
- $this->Session->delete('Search');
- $this->Session->write('Search.keyword',$keyword);
- $this->Session->write('Search.category',$category);
- $this->Session->write('Search.rating',$rating);
- $this->Session->write('Search.option',$option);
- }else{
- $this->Session->write('Search.keyword',$keyword);
- $this->Session->write('Search.category',$category);
- $this->Session->write('Search.rating',$rating);
- $this->Session->write('Search.option',$option);
- }
- }else{
- if($this->Session->check('Search')){
- $keyword = $this->Session->read('Search.keyword');
- $category = $this->Session->read('Search.category');
- $rating = $this->Session->read('Search.rating');
- $option = $this->Session->read('Search.option');
- }
- }
- if(!empty($option) && $option=='hotel'){
- //search khach san
- $conds['Travel.option'] = 1;
- if(!empty($rating)){
- $conds['SUBSTRING_INDEX(Travel.rating," ",1)'] = $rating;
- }
- if(!empty($keyword)){
- $conds['OR'] = array(
- 'lower(CONVERT(Travel.name USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(Travel.description USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%'
- );
- }
- $this->Category->Travel->setLanguage();
- $this->paginate['Travel'] = array('conditions'=>$conds,'recursive' => 0,'limit'=>10);
- //print_r($this->paginate('Travel'));exit();
- }else{
- //search tour
- $conds['Travel.option'] = 0;
- if(!empty($category)){
- $conds['Travel.category_id'] = $category;
- }
- if(!empty($keyword)){
- $conds['OR'] = array(
- 'lower(CONVERT(Travel.name USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(Travel.description USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%'
- );
- }
- $this->Category->Travel->setLanguage();
- $this->paginate['Travel'] = array('conditions'=>$conds,'recursive' => 0,'limit'=>10);
- //print_r($this->paginate('Travel'));exit();
- }
- $travels = $this->paginate('Travel');
- if(count($travels)>0){
- $folder = $this->checkFolderImage($travels[0]['Travel']['option']);
- }
- $this->set(compact('folder'));
- $this->set(compact('travels'));
- }
-
- function checkFolderImage($option=null){
- if($option==0){
- return 'tours';
- }else if($option==1){
- return 'hotels';
- }else if($option==3){
- return 'services';
- }else if($option==4){
- return 'transports';
- }
- return 'tours';
- }
-
- function detail(){
- if(!empty($this->params['id'])){
- $this->Category->Travel->setLanguage();
- $travel = $this->Category->Travel->find('first',array('conditions'=>array('Travel.id'=>$this->params['id'])));
- if($travel!=false){
- $travel['Travel']['folder'] = $this->checkFolderImage($travel['Travel']['option']);
- }
- $this->set(compact('travel'));
- $this->set('title_for_layout',$travel['Travel']['name']);
- }
- }
- }
- ?>