PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/chimbien/app/controllers/travels_controller.php

http://goldcat.googlecode.com/
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
  1. <?php
  2. class TravelsController extends AppController {
  3. var $name = 'Travels';
  4. function _doUpload($file) {
  5. $this->autoRender = false;
  6. Configure::write('debug', 0);
  7. $uploadsDir = 'uploads/tours';
  8. if ($this->params['action'] == 'admin_edit') {
  9. if ($this->data['Travel']['delete_image'] == 1) {
  10. $this->Upload->delete_image($uploadsDir, $this->data['Travel']['value_image']);
  11. $this->Upload->delete_image($uploadsDir . '/thumb/', $this->data['Travel']['value_image']);
  12. $this->data['Travel']['image']='';
  13. }else{
  14. unset($this->data['Travel']['image']);
  15. }
  16. }
  17. if (!empty($file['name'])) {
  18. $file = $this->Upload->uploadImage($uploadsDir, $file, '230x0');
  19. if (array_key_exists('filename', $file)) {
  20. $this->data['Travel']['image'] = $file['filename'];
  21. } else {
  22. return false;
  23. }
  24. }
  25. }
  26. function admin_index() {
  27. //filter
  28. $keyword = trim($this->data['Search']['keyword']);
  29. $category_id = $this->data['Search']['category_id'];
  30. $conds = array();
  31. if(!empty($category_id)){
  32. $conds['Travel.category_id']=$category_id;
  33. }
  34. $conds['Travel.option']=0;
  35. if (!empty($keyword)) {
  36. $conds['OR'] = array(
  37. 'lower(CONVERT(I18n__name.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  38. 'lower(CONVERT(I18n__times.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  39. 'lower(CONVERT(I18n__transport.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  40. 'lower(CONVERT(I18n__price.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  41. 'lower(CONVERT(I18n__description.content USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  42. );
  43. }
  44. //End filter
  45. $this->Travel->setLanguage();
  46. $travels = $this->Travel->find('all', array('conditions' => $conds, 'recursive' => 0, 'order' => 'Travel.created desc'));
  47. $categories = $this->listTour('all');
  48. $this->set(compact('travels','categories'));
  49. }
  50. function admin_view($id = null) {
  51. $this->layout= 'ajax';
  52. if (!$id) {
  53. $this->Session->setFlash(__('Invalid travel', true), 'admin/flasherror', '', 'error');
  54. $this->redirect(array('action' => 'index'));
  55. }
  56. $this->set('travel', $this->Travel->read(null, $id));
  57. }
  58. function admin_add() {
  59. if (!empty($this->data)) {
  60. $this->_doUpload($this->data['Travel']['image']);
  61. $this->data['Travel']['option'] = 0;
  62. $this->Travel->create();
  63. if ($this->Travel->save($this->data)) {
  64. $this->Session->setFlash(__('The travel has been saved', true), 'admin/flashsuccess', '', 'success');
  65. $this->redirect(array('action' => 'index'));
  66. } else {
  67. $this->Session->setFlash(__('The travel could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
  68. }
  69. }
  70. $categories = $this->listTour();
  71. $this->set(compact('categories'));
  72. }
  73. function admin_edit($id = null) {
  74. if (!$id && empty($this->data)) {
  75. $this->Session->setFlash(__('Invalid travel', true), 'admin/flasherror', '', 'error');
  76. $this->redirect(array('action' => 'index'));
  77. }
  78. if (!empty($this->data)) {
  79. $this->_doUpload($this->data['Travel']['image']);
  80. if ($this->Travel->save($this->data)) {
  81. $this->Session->setFlash(__('The travel has been saved', true), 'admin/flashsuccess', '', 'success');
  82. $this->redirect(array('action' => 'index'));
  83. } else {
  84. $this->Session->setFlash(__('The travel could not be saved. Please, try again.', true), 'admin/flasherror', '', 'error');
  85. }
  86. }
  87. if (empty($this->data)) {
  88. $this->Travel->setLanguage();
  89. $this->data = $this->Travel->read(null, $id);
  90. }
  91. $categories = $this->listTour();
  92. $this->set(compact('categories'));
  93. }
  94. function admin_delete($id = null) {
  95. if (!$id) {
  96. $this->Session->setFlash(__('Invalid id for travel', true), 'admin/flasherror', '', 'error');
  97. $this->redirect(array('action' => 'index'));
  98. }
  99. if ($this->Travel->delete($id)) {
  100. $this->Session->setFlash(__('Travel deleted', true), 'admin/flashsuccess', '', 'success');
  101. $this->redirect(array('action' => 'index'));
  102. }
  103. $this->Session->setFlash(__('Travel was not deleted', true), 'admin/flasherror', '', 'error');
  104. $this->redirect(array('action' => 'index'));
  105. }
  106. function admin_deleteAll($ids = null) {
  107. if (!empty($ids)) {
  108. $id = explode('-', $ids);
  109. //delete image
  110. $travels = $this->Travel->find('all', array('conditions' => array('Travel.id' => $id)));
  111. foreach ($travels as $travel) {
  112. $this->Upload->delete_image('uploads/tours/', $travel['Travel']['image']);
  113. $this->Upload->delete_image('uploads/tours/thumb/', $travel['Travel']['image']);
  114. }
  115. //end delete image
  116. $this->Travel->deleteAll(array('Travel.id' => $id));
  117. $this->Session->setFlash(__('Travels deleted', true), 'admin/flashsuccess', '', 'success');
  118. $this->redirect(array('action' => 'index'));
  119. }
  120. $this->redirect(array('action' => 'index'));
  121. }
  122. function admin_changeStatusAll($ids = null, $status=null) {
  123. if (!empty($ids)) {
  124. $id = explode('-', $ids);
  125. $this->Travel->updateAll(array('Travel.hot' => $status), array('Travel.id' => $id));
  126. }
  127. $this->Session->setFlash(sprintf(__('%s Change hot tour Successfull.', true), 'Travel'), 'admin/flashsuccess', '', 'success');
  128. $this->redirect(array('action' => 'index'));
  129. }
  130. function admin_changestatus() {
  131. Configure::write('debug', 0);
  132. $this->autoRender = false;
  133. $id = $this->params['form']['id'];
  134. $status = $this->params['form']['status'];
  135. $this->Travel->updateAll(array('Travel.hot' => $status), array('Travel.id' => $id));
  136. $return = array();
  137. $return['id'] = $id;
  138. $return['newstatus'] = ($status == 0) ? 1 : 0;
  139. echo json_encode($return);
  140. }
  141. function showTour($hot=false){
  142. $this->Travel->setLanguage();
  143. $this->Travel->recursive = -1;
  144. if($hot==false){
  145. $tour = $this->Travel->find('all',array('conditions'=>array('Travel.option'=>0),'order'=>'Travel.created desc'));
  146. }else{
  147. $tour = $this->Travel->find('all',array('conditions'=>array('Travel.hot'=>1,'Travel.option'=>0),'order'=>'Travel.created desc','limit'=>6));
  148. }
  149. return $tour;
  150. }
  151. function category(){
  152. if(!empty($this->params['id'])){
  153. $this->Travel->Category->setLanguage();
  154. $category = $this->Travel->Category->find('first',array('conditions'=>array('Category.id'=>$this->params['id']),'recursive' => -1));
  155. $this->paginate = array('conditions'=>array('Travel.category_id'=>$this->params['id']),'recursive' => -1,'limit'=>1);
  156. $travels = $this->paginate();
  157. if(count($travels)>0){
  158. $category['Category']['folder'] = $this->checkFolderImage($travels[0]['Travel']['option']);
  159. }
  160. $this->set(compact('category'));
  161. $this->set(compact('travels'));
  162. }
  163. }
  164. function checkFolderImage($option=null){
  165. if($option==0){
  166. return 'tours';
  167. }else if($option==1){
  168. return 'hotels';
  169. }else if($option==2){
  170. return 'services';
  171. }
  172. return 'tours';
  173. }
  174. function listTour($all=null){
  175. $this->Travel->Category->setLanguage();
  176. $categories1 = $this->Travel->Category->children(1);
  177. if($categories1==false){
  178. $categories1 = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(1))));
  179. }
  180. $categories3 = $this->Travel->Category->children(3);
  181. if($categories3==false){
  182. $categories3 = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(3))));
  183. }
  184. $categories4 = $this->Travel->Category->children(4);
  185. if($categories4==false){
  186. $categories4 = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(4))));
  187. }
  188. $categories = $this->Travel->Category->find('list',array('conditions'=>array('Category.id'=>array(1,3,4))));
  189. foreach($categories1 as $key=>$value){
  190. $categories[$key] = $value;
  191. }
  192. foreach($categories3 as $key=>$value){
  193. $categories[$key] = $value;
  194. }
  195. foreach($categories4 as $key=>$value){
  196. $categories[$key] = $value;
  197. }
  198. if(!empty($all)){
  199. $categories[0] = 'Tá??t cá??';;
  200. }
  201. ksort($categories);
  202. return $categories;
  203. }
  204. }
  205. ?>