PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/application/modules/admin/controllers/catalog/category.php

http://comet.googlecode.com/
PHP | 211 lines | 180 code | 16 blank | 15 comment | 18 complexity | 004d40b202cb2d788ad0f23c1a85ddea MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. if (! defined ( 'BASEPATH' ))
  3. exit ( 'No direct script access allowed' );
  4. require_once ('./application/controllers/authenticate.php');
  5. class Category extends Authenticate {
  6. function __construct() {
  7. parent::__construct ();
  8. $this->load->model ( 'category_model' );
  9. $this->load->library ( 'html/control/fx_control' );
  10. $this->load->library ( 'html/control/fx_filter' );
  11. $this->load->library ( 'html/control/fx_sort' );
  12. $this->category_model->addAttributeOrder ( 'id', 'desc' );
  13. }
  14. protected function _eventLoadLayout() {
  15. $this->page ['title'] = 'Catalog Category Management';
  16. $this->module ['title'] = 'Catalog Category Management';
  17. $this->module ['grid'] ['records'] = 20;
  18. $this->module ['grid'] ['page_range'] = 5;
  19. }
  20. function index() {
  21. $data = $this->_initGridView ();
  22. $this->setPageContent ( 'catalog/category/grid', $data );
  23. $this->renderLayout ();
  24. }
  25. protected function _initGridView() {
  26. // Get Paginator Params
  27. $page = $this->input->getParam ( 'page' );
  28. $recordPerPage = $this->input->getParam ( 'recordPerPage' );
  29. if (! $recordPerPage) {
  30. $recordPerPage = $this->module ['grid'] ['records'];
  31. }
  32. // Get Filter Params
  33. $filters = $this->input->post ( 'categoryFilter' );
  34. // Filter
  35. $this->fx_filter->filter ( $filters, $this->category_model );
  36. // Get Order Params
  37. $order = $this->input->post ( 'categoryOrder' );
  38. // Sort
  39. $this->fx_sort->sort ( $order, $this->category_model );
  40. // Get total found records
  41. $found = $this->category_model->countTotal ();
  42. // Paginate
  43. $this->load->library ( "tree/paginator/engine/st_paginator_collection" );
  44. $this->load->library ( "tree/paginator/st_paginator" );
  45. $this->st_paginator_collection->setModel ( $this->category_model );
  46. $this->st_paginator->setConfig ( array ('engine' => $this->st_paginator_collection, 'pageRange' => $this->module ['grid'] ['page_range'], 'itemCountPerPage' => $recordPerPage, 'currentPageNumber' => $page ) );
  47. // Paginate Collection
  48. $collection = $this->st_paginator->getItemsByPage ( $page );
  49. if (empty ( $collection )) {
  50. $collection = array ();
  51. }
  52. $data = array ();
  53. $data ['found'] = $found;
  54. $data ['sorter'] = $this->fx_sort;
  55. $data ['filters'] = $this->fx_filter;
  56. $data ['paginator'] = $this->st_paginator;
  57. $data ['category_ids'] = $this->input->post ( 'category_ids' );
  58. $data ['category_ids_array'] = explode ( ',', $this->input->post ( 'category_ids' ) );
  59. $data ['collection'] = $collection;
  60. return $data;
  61. }
  62. function edit() {
  63. try {
  64. $id = $this->input->getParam ( 'id' );
  65. if ($id) {
  66. $this->category_model->loadByPK ( $id );
  67. }
  68. $data ['model'] = $this->category_model;
  69. $data ['parents'] = $this->category_model->getCollection ();
  70. $this->setPageContent ( 'catalog/category/edit', $data );
  71. $this->renderLayout ( 'layouts/ajax' );
  72. } catch ( Exception $ex ) {
  73. echo $ex->getMessage ();
  74. }
  75. }
  76. function save() {
  77. try {
  78. $id = $this->input->post ( 'category_id' );
  79. if ($id) {
  80. $this->category_model->loadByPK ( $id );
  81. }
  82. $parent = $this->input->post ( 'category_parent' );
  83. if (! $parent) {
  84. $parent = 0;
  85. } else {
  86. if (! $this->category_model->isLoaded ()) {
  87. $this->category_model->setCreatedAt ( "NOW()" );
  88. }
  89. }
  90. // Upload File
  91. $config ['upload_path'] = './media/category/banners/';
  92. $config ['allowed_types'] = 'gif|jpg|png';
  93. $this->load->library ( 'upload', $config );
  94. $this->upload->do_upload ( 'category_banner' );
  95. // Get Params
  96. $this->category_model->setUpdatedAt ( "NOW()" );
  97. $dataUpload = $this->upload->data ();
  98. $name = $this->input->post ( 'category_name' );
  99. $code = $this->input->post ( 'category_code' );
  100. $summary = $this->input->post ( 'category_summary' );
  101. $status = $this->input->post ( 'category_status' );
  102. $meta_title = $this->input->post ( 'category_meta_title' );
  103. $meta_keywords = $this->input->post ( 'category_meta_keywords' );
  104. $meta_description = $this->input->post ( 'category_meta_desc' );
  105. // General Information
  106. $this->category_model->setUserId ( 1 );
  107. $this->category_model->setEntityTypeId ( 1 );
  108. $this->category_model->setParentId ( $parent );
  109. $this->category_model->setName ( $name );
  110. $this->category_model->setCode ( $code );
  111. $bannerPath = 'media/category/banners/' . $dataUpload ['file_name'];
  112. if (strlen ( $dataUpload ['file_name'] ) > 0) {
  113. $this->category_model->setBanner ( $bannerPath );
  114. }
  115. $this->category_model->setSummary ( $summary );
  116. $this->category_model->setIsActive ( $status );
  117. // Meta Information
  118. $this->category_model->setMetaTitle ( $meta_title );
  119. $this->category_model->setMetaKeywords ( $meta_keywords );
  120. $this->category_model->setMetaDesc ( $meta_description );
  121. // Save Information
  122. $this->category_model->save ();
  123. $this->category_model->resetFilteredList ()->addAttributeToFilter ( 'code', $code, 'LIKE' )->loadByFilter ();
  124. $newId = $this->category_model->getId ();
  125. if ($parent == 0) {
  126. $this->category_model->setLevel ( 1 );
  127. $this->category_model->setPath ( '/0/' . $newId );
  128. } else {
  129. $parentCategory = new category_model ();
  130. $parentCategory->loadByPK ( $parent );
  131. if ($parentCategory->isLoaded ()) {
  132. $parentLevel = $parentCategory->getLevel ();
  133. $parentPath = $parentCategory->getPath ();
  134. $this->category_model->setLevel ( ++ $parentLevel );
  135. $this->category_model->setPath ( $parentPath . '/' . $newId );
  136. }
  137. }
  138. $this->category_model->save ();
  139. if ($newId) {
  140. $this->st_registry->register ( 'admin/catalog/category/message', 'Update successful' );
  141. redirect ( site_url ( "admin/catalog/category/index/id/" . $newId, true ) );
  142. } else {
  143. $this->st_registry->register ( 'admin/catalog/category/message', 'Add new category successful' );
  144. redirect ( site_url ( "admin/catalog/category/index", true ) );
  145. }
  146. } catch ( Exception $ex ) {
  147. echo $ex->getMessage ();
  148. }
  149. }
  150. function delete() {
  151. try {
  152. $id = $this->input->getParam ( 'id' );
  153. if ($id) {
  154. $this->category_model->loadByPK ( $id );
  155. $name = $this->category_model->getName ();
  156. $this->category_model->delete ();
  157. $this->st_registry->register ( 'admin/catalog/category/message', "Delete category $name successful" );
  158. redirect ( site_url ( 'admin/catalog/category/index', true ) );
  159. }
  160. } catch ( Exception $ex ) {
  161. $this->st_registry->register ( 'admin/catalog/category/error', $ex->getMessage () );
  162. redirect ( site_url ( 'admin/catalog/category/index', true ) );
  163. }
  164. }
  165. function massActive() {
  166. try {
  167. // Load Category Model
  168. $ids = $this->input->getMassValues ( 'category_ids' );
  169. if ($ids) {
  170. $this->category_model->query ( "update catalog_category set is_active = 1 where id in ($ids)" );
  171. }
  172. $countItem = count ( explode ( ',', $ids ) );
  173. $this->st_registry->register ( 'admin/catalog/category/message', "Active [$countItem] categories successfully" );
  174. redirect ( site_url ( "admin/catalog/category/index", true ) );
  175. } catch ( Exception $ex ) {
  176. $this->st_registry->register ( 'admin/catalog/category/error', $ex->getMessage () );
  177. redirect ( site_url ( "admin/catalog/category/index", true ) );
  178. }
  179. }
  180. function massDeactive() {
  181. try {
  182. // Load Category Model
  183. $ids = $this->input->getMassValues ( 'category_ids' );
  184. if ($ids) {
  185. $this->category_model->query ( "update catalog_category set is_active = 0 where id in ($ids)" );
  186. }
  187. $countItem = count ( explode ( ',', $ids ) );
  188. $this->st_registry->register ( 'admin/catalog/category/message', "Active [$countItem] categories successfully" );
  189. redirect ( site_url ( "admin/catalog/category/index", true ) );
  190. } catch ( Exception $ex ) {
  191. $this->st_registry->register ( 'admin/catalog/category/error', $e->getMessage () );
  192. redirect ( site_url ( "admin/catalog/category/index", true ) );
  193. }
  194. }
  195. }