PageRenderTime 29ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/chimbien/app/controllers/categories_controller.php

http://goldcat.googlecode.com/
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
  1. <?php
  2. class CategoriesController extends AppController {
  3. var $name = 'Categories';
  4. function admin_test() {
  5. $categories = $this->Category->find('threaded',array('recursive' => -1));
  6. $this->set(compact('categories'));
  7. $this->set('len',count($categories));
  8. }
  9. function admin_moveup($id=null) {
  10. $move= $this->Category->moveUp($id,1);
  11. $this->redirect(array('action'=>'index'));
  12. }
  13. function admin_movedown($id=null) {
  14. $move= $this->Category->moveDown($id,1);
  15. $this->redirect(array('action'=>'index'));
  16. }
  17. function admin_index() {
  18. $this->Category->setLanguage();
  19. $categories = $this->Category->find('threaded',array('recursive' => -1));
  20. $this->set(compact('categories'));
  21. $this->set('len',count($categories));
  22. }
  23. function admin_view($id = null) {
  24. if (!$id) {
  25. $this->Session->setFlash(__('Invalid category', true), 'admin/flasherror', '', 'error');
  26. $this->redirect(array('action' => 'index'));
  27. }
  28. $this->set('category', $this->Category->read(null, $id));
  29. }
  30. function admin_add() {
  31. if (!empty($this->data)) {
  32. $this->data['Category']['link'] = Inflector::slug(strtolower($this->data['Category']['link']),'-');
  33. $this->Category->create();
  34. if ($this->Category->save($this->data)) {
  35. $this->Session->setFlash(__('The category has been saved', true), 'admin/flashsuccess', '', 'success');
  36. $this->redirect(array('action' => 'index'));
  37. } else {
  38. $this->Session->setFlash(__('The category could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
  39. }
  40. }
  41. $this->Category->setLanguage('vie');
  42. $parents = $this->Category->find('list',array('conditions'=>array('parent_id'=>0),'recursive' => -1));
  43. //$parents[0] = 'Menu Cha';
  44. ksort($parents);
  45. //
  46. $this->loadModel('Page');
  47. $this->Page->setLanguage('eng');
  48. $pages = $this->Page->find('list',array('fields'=>array('Page.name')));
  49. $links = array();
  50. foreach($pages as $page){
  51. $links[Inflector::slug(strtolower($page),'-')] = $page;
  52. }
  53. $this->set(compact('parents','links'));
  54. }
  55. function admin_edit($id = null) {
  56. if (!$id && empty($this->data)) {
  57. $this->Session->setFlash(__('Invalid category', true), 'admin/flasherror', '', 'error');
  58. $this->redirect(array('action' => 'index'));
  59. }
  60. if (!empty($this->data)) {
  61. if ($this->Category->save($this->data)) {
  62. $this->Category->updateAll(array('Category.parent_id'=>0),array('Category.parent_id'=>NULL));
  63. $this->Session->setFlash(__('The category has been saved', true), 'admin/flashsuccess', '', 'success');
  64. $this->redirect(array('action' => 'index'));
  65. } else {
  66. $this->Session->setFlash(__('The category could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
  67. }
  68. }
  69. $this->Category->setLanguage();
  70. if (empty($this->data)) {
  71. $this->data = $this->Category->read(null, $id);
  72. }
  73. $parents = $this->Category->find('list',array('conditions'=>array('parent_id'=>0),'recursive' => -1));
  74. $parents[0] = 'Menu Cha';
  75. ksort($parents);
  76. //
  77. $this->loadModel('Page');
  78. $this->Page->setLanguage('eng');
  79. $pages = $this->Page->find('list',array('fields'=>array('Page.name')));
  80. $links = array();
  81. foreach($pages as $page){
  82. $links[Inflector::slug(strtolower($page),'-')] = $page;
  83. }
  84. ksort($links);
  85. $this->set(compact('parents','links'));
  86. }
  87. function admin_delete($id = null) {
  88. if (!$id) {
  89. $this->Session->setFlash(__('Invalid id for category', true), 'admin/flasherror', '', 'error');
  90. $this->redirect(array('action' => 'index'));
  91. }
  92. if ($this->Category->delete($id)) {
  93. $this->Session->setFlash(__('Category deleted', true), 'admin/flashsuccess', '', 'success');
  94. $this->redirect(array('action' => 'index'));
  95. }
  96. $this->Session->setFlash(__('Category was not deleted', true), 'admin/flasherror', '', 'error');
  97. $this->redirect(array('action' => 'index'));
  98. }
  99. function admin_deleteAll($ids = null) {
  100. if (!empty($ids)) {
  101. $id = explode('-', $ids);
  102. $this->Category->deleteAll(array('Category.id' => $id));
  103. $this->Session->setFlash(__('Categories deleted', true), 'admin/flashsuccess', '', 'success');
  104. $this->redirect(array('action' => 'index'));
  105. }
  106. $this->redirect(array('action' => 'index'));
  107. }
  108. function getTree(){
  109. $this->Category->setLanguage();
  110. return $this->Category->find('threaded',array('recursive' => -1));
  111. }
  112. function category($id=null){
  113. if(!empty($id)){
  114. $this->params['id']=$id;
  115. }
  116. if(!empty($this->params['id'])){
  117. $this->Category->setLanguage();
  118. $category = $this->Category->find('first',array('conditions'=>array('Category.id'=>$this->params['id']),'recursive' => -1));
  119. $this->Category->Travel->setLanguage();
  120. $this->paginate['Travel'] = array('conditions'=>array('Travel.category_id'=>$this->params['id']),'recursive' => -1,'limit'=>10);
  121. $travels = $this->paginate('Travel');
  122. if(count($travels)>0){
  123. $category['Category']['folder'] = $this->checkFolderImage($travels[0]['Travel']['option']);
  124. }
  125. $this->set(compact('category'));
  126. $this->set(compact('travels'));
  127. $this->set('title_for_layout',$category['Category']['name']);
  128. }
  129. }
  130. function search(){
  131. $conds = array();
  132. if(!empty($this->data)){
  133. $keyword = trim($this->data['Search']['keyword']);
  134. $category = $this->data['Search']['category'];
  135. $rating = $this->data['Search']['rating'];
  136. $option = $this->data['Search']['option'];
  137. if($this->Session->check('Search')){
  138. $this->Session->delete('Search');
  139. $this->Session->write('Search.keyword',$keyword);
  140. $this->Session->write('Search.category',$category);
  141. $this->Session->write('Search.rating',$rating);
  142. $this->Session->write('Search.option',$option);
  143. }else{
  144. $this->Session->write('Search.keyword',$keyword);
  145. $this->Session->write('Search.category',$category);
  146. $this->Session->write('Search.rating',$rating);
  147. $this->Session->write('Search.option',$option);
  148. }
  149. }else{
  150. if($this->Session->check('Search')){
  151. $keyword = $this->Session->read('Search.keyword');
  152. $category = $this->Session->read('Search.category');
  153. $rating = $this->Session->read('Search.rating');
  154. $option = $this->Session->read('Search.option');
  155. }
  156. }
  157. if(!empty($option) && $option=='hotel'){
  158. //search khach san
  159. $conds['Travel.option'] = 1;
  160. if(!empty($rating)){
  161. $conds['SUBSTRING_INDEX(Travel.rating," ",1)'] = $rating;
  162. }
  163. if(!empty($keyword)){
  164. $conds['OR'] = array(
  165. 'lower(CONVERT(Travel.name USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  166. 'lower(CONVERT(Travel.description USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%'
  167. );
  168. }
  169. $this->Category->Travel->setLanguage();
  170. $this->paginate['Travel'] = array('conditions'=>$conds,'recursive' => 0,'limit'=>10);
  171. //print_r($this->paginate('Travel'));exit();
  172. }else{
  173. //search tour
  174. $conds['Travel.option'] = 0;
  175. if(!empty($category)){
  176. $conds['Travel.category_id'] = $category;
  177. }
  178. if(!empty($keyword)){
  179. $conds['OR'] = array(
  180. 'lower(CONVERT(Travel.name USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  181. 'lower(CONVERT(Travel.description USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%'
  182. );
  183. }
  184. $this->Category->Travel->setLanguage();
  185. $this->paginate['Travel'] = array('conditions'=>$conds,'recursive' => 0,'limit'=>10);
  186. //print_r($this->paginate('Travel'));exit();
  187. }
  188. $travels = $this->paginate('Travel');
  189. if(count($travels)>0){
  190. $folder = $this->checkFolderImage($travels[0]['Travel']['option']);
  191. }
  192. $this->set(compact('folder'));
  193. $this->set(compact('travels'));
  194. }
  195. function checkFolderImage($option=null){
  196. if($option==0){
  197. return 'tours';
  198. }else if($option==1){
  199. return 'hotels';
  200. }else if($option==3){
  201. return 'services';
  202. }else if($option==4){
  203. return 'transports';
  204. }
  205. return 'tours';
  206. }
  207. function detail(){
  208. if(!empty($this->params['id'])){
  209. $this->Category->Travel->setLanguage();
  210. $travel = $this->Category->Travel->find('first',array('conditions'=>array('Travel.id'=>$this->params['id'])));
  211. if($travel!=false){
  212. $travel['Travel']['folder'] = $this->checkFolderImage($travel['Travel']['option']);
  213. }
  214. $this->set(compact('travel'));
  215. $this->set('title_for_layout',$travel['Travel']['name']);
  216. }
  217. }
  218. }
  219. ?>