PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/geocontexter/controllers/KeywordEditController.php

http://geocontexter.googlecode.com/
PHP | 303 lines | 185 code | 58 blank | 60 comment | 17 complexity | c1b79da858ea2f4d94229a63ff8637f5 MD5 | raw file
  1. <?php
  2. /**
  3. * GeoContexter
  4. * @link http://code.google.com/p/geocontexter/
  5. * @package GeoContexter
  6. */
  7. /**
  8. * Edit keyword
  9. *
  10. * @package GeoContexter
  11. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  12. * @author Armand Turpel <geocontexter@gmail.com>
  13. * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (Sun, 27 Feb 2011) $ / $Author: armand.turpel $
  14. */
  15. class Geocontexter_KeywordEditController extends Mozend_Controller_Action_AbstractAdmin
  16. {
  17. public function preDispatch()
  18. {
  19. // set view. we keep allways the index view
  20. //
  21. $this->_helper->viewRenderer->setScriptAction('index');
  22. $this->id_keyword =$this->request->getParam('id_keyword');
  23. if((null === $this->id_keyword) || (0 == $this->id_keyword))
  24. {
  25. return $this->error( '0 or no id_keyword request parameter defined.', __file__, __line__ );
  26. }
  27. $this->view->partialData['active_page'] = 'edit';
  28. $this->view->partialData['id_keyword'] = $this->id_keyword;
  29. $this->view->id_keyword = $this->id_keyword;
  30. $this->view->keyword_branch_result = array();
  31. $this->view->attribute_groups = array();
  32. $this->view->attribute_id_group = '';
  33. $this->view->keyword_name = '';
  34. $this->view->keyword_description = '';
  35. $this->view->keyword_id_status = 100;
  36. $this->view->keyword_lang = 'en';
  37. $this->view->tabindex_after_attributes = 8;
  38. $this->view->error = array();
  39. $session = Zend_Registry::get('session');
  40. $this->register_model_callbacks( $session );
  41. }
  42. public function indexAction()
  43. {
  44. $this->init_data();
  45. }
  46. private function init_data()
  47. {
  48. // get keyword branch
  49. //
  50. $keyword_branch = new Geocontexter_Model_KeywordGetBranch;
  51. $params = array('id_keyword' => $this->id_keyword) ;
  52. $result_branch = $keyword_branch->get( $params );
  53. if($result_branch instanceof Mozend_ModelError)
  54. {
  55. return $this->error( $result_branch->getErrorString(), __file__, __line__ );
  56. }
  57. else
  58. {
  59. $this->view->keyword_branch_result = $result_branch;
  60. }
  61. // get availaible languages
  62. //
  63. $languages = new Geocontexter_Model_LanguagesGet;
  64. // optional
  65. $params = array('enable' => 'true');
  66. $lang_result = $languages->get( $params );
  67. if($lang_result instanceof Mozend_ModelError)
  68. {
  69. return $this->error( $lang_result->getErrorString(), __file__, __line__ );
  70. }
  71. else
  72. {
  73. $this->view->languages = $lang_result;
  74. }
  75. // get all keyword related attribute groups
  76. //
  77. $attribute_groups = new Geocontexter_Model_AttributeGroupsGet;
  78. $params = array('id_table' => 4);
  79. $attribute_result = $attribute_groups->get( $params );
  80. if($attribute_result instanceof Mozend_ModelError)
  81. {
  82. return $this->error( $attribute_result->getErrorString(), __file__, __line__ );
  83. }
  84. else
  85. {
  86. $this->view->attribute_groups = $attribute_result;
  87. }
  88. // get keyword
  89. //
  90. $keyword = new Geocontexter_Model_KeywordGet;
  91. $params = array('id_keyword' => $this->id_keyword);
  92. $result = $keyword->get( $params );
  93. if($result instanceof Mozend_ModelError)
  94. {
  95. return $this->error( $result->getErrorString(), __file__, __line__ );
  96. }
  97. else
  98. {
  99. $this->view->result = $result;
  100. // assign html head title
  101. //
  102. $this->view->headTitle('Edit keyword ' . $result['title'], 'PREPEND');
  103. }
  104. // Assign additional attributes with values
  105. //
  106. if($result['id_attribute_group'] != null)
  107. {
  108. $attributes_values = new Geocontexter_Model_AttributeAssignKeyValue;
  109. // we pass this array to the partial view helper "_additional_attributes.phtml"
  110. //
  111. $this->view->additionalAttributes = array();
  112. $this->view->additionalAttributes['attributes'] = $attributes_values->get( $result );
  113. $this->view->additionalAttributes['id_attribute_group'] = $result['id_attribute_group'];
  114. $this->view->additionalAttributes['tabindex'] = 7;
  115. // get tabindex after the aaditional attribute fields
  116. //
  117. $this->view->tabindex_after_attributes += count($this->view->additionalAttributes['attributes']) - 1;
  118. }
  119. }
  120. /**
  121. * update keyword
  122. */
  123. public function updateAction()
  124. {
  125. // check on cancel action
  126. //
  127. $cancel = $this->request->getPost('cancel');
  128. if($cancel !== null)
  129. {
  130. $this->_redirect($this->view->adminAreaToken .
  131. '/geocontexter/keyword/index/id_keyword/' . $this->request->getPost('keyword_id_parent'));
  132. }
  133. $params = array();
  134. $attribute_id_group = $this->request->getPost('attribute_group');
  135. // fetch addditional attributes values if set
  136. //
  137. if(!empty($attribute_id_group))
  138. {
  139. $attributes_values = new Geocontexter_Model_AttributeAssignKeyValue;
  140. $this->view->attribute_id_group = $attribute_id_group;
  141. // we pass this array to the partial view helper "_additional_attributes.phtml"
  142. //
  143. $this->view->additionalAttributes = array();
  144. $_attr_result = $this->view->additionalAttributes['attributes']
  145. = $attributes_values->getAttributesWithValuesByOrder( $attribute_id_group, $this->request->getPost() );
  146. if($attr_result instanceof Mozend_ModelError)
  147. {
  148. return $this->error( $attr_result->getErrorString(), __file__, __line__ );
  149. }
  150. else
  151. {
  152. $this->view->additionalAttributes['id_attribute_group'] = $attribute_id_group;
  153. $this->view->additionalAttributes['tabindex'] = 7;
  154. // get tabindex after the additional attribute fields
  155. //
  156. $this->view->tabindex_after_attributes += count($this->view->additionalAttributes['attributes']) - 1;
  157. }
  158. // fetch attribute values and prepare/convert it in postgresql array format for insert as new keyword
  159. //
  160. $assign_result = $attributes_values->assign( $attribute_id_group, $this->request->getPost() );
  161. if($assign_result instanceof Mozend_ModelError)
  162. {
  163. return $this->error( $assign_result->getErrorString(), __file__, __line__ );
  164. }
  165. else
  166. {
  167. $params['data'] = $assign_result;
  168. $params['data']['id_attribute_group'] = $attribute_id_group;
  169. }
  170. }
  171. else
  172. {
  173. // remove reference to attribute group
  174. // and delete any additional attribute values
  175. //
  176. $params['data']['attribute_value'] = null;
  177. }
  178. $this->view->error = array();
  179. $this->view->result = array();
  180. $params['id_keyword'] = $this->id_keyword;
  181. $this->view->result['title'] =
  182. $params['data']['title'] = $this->request->getPost('keyword_title');
  183. $this->view->result['description'] =
  184. $params['data']['description'] = $this->request->getPost('keyword_description');
  185. $this->view->result['id_parent'] =
  186. $params['data']['id_parent'] = $this->request->getPost('keyword_id_parent');
  187. $this->view->result['id_status'] =
  188. $params['data']['id_status'] = $this->request->getPost('keyword_id_status');
  189. $this->view->result['lang'] =
  190. $params['data']['lang'] = $this->request->getPost('keyword_lang');
  191. if(empty($params['data']['title']))
  192. {
  193. $this->view->error[] = 'Keyword title is empty';
  194. $this->view->headTitle('Error: keyword title field is empty', 'PREPEND');
  195. }
  196. if(count($this->view->error) > 0)
  197. {
  198. $this->init_data();
  199. return;
  200. }
  201. // add item attribute
  202. //
  203. $keyword = new Geocontexter_Model_KeywordUpdate;
  204. $result = $keyword->update( $params );
  205. if($result instanceof Mozend_ModelError)
  206. {
  207. return $this->error( $result->getErrorString(), __file__, __line__ );
  208. }
  209. else
  210. {
  211. $this->_redirect($this->view->adminAreaToken .
  212. '/geocontexter/keyword/index/id_keyword/' . $this->view->result['id_parent']);
  213. }
  214. }
  215. /**
  216. * Register of model callback classes
  217. *
  218. * @param object $session
  219. */
  220. private function register_model_callbacks( & $session )
  221. {
  222. $action_callback = new Geocontexter_Model_ModelCallback( $session );
  223. // url to reload after a model callback was done
  224. //
  225. $this->opener_url = $this->view->baseUrl().'/'.$this->view->adminAreaToken.'/geocontexter/keyword-edit/index/id_keyword/'.$this->id_keyword;
  226. //
  227. // item_keywords
  228. //
  229. $params_keyword =
  230. array('model_class' => 'Geocontexter_Model_KeywordUpdate',
  231. 'model_class_methode' => 'updateKeywordParent',
  232. 'model_field' => 'id_parent',
  233. 'check_circular' => true,
  234. 'id_name' => 'id_keyword',
  235. 'id_value' => $this->id_keyword,
  236. 'input_type' => 'radio',
  237. 'opener_url' => $this->opener_url);
  238. $callback_number = $action_callback->register( $params_keyword );
  239. if($callback_number instanceof Mozend_ModelError)
  240. {
  241. return $this->error( $callback_number->getErrorString(), __file__, __line__ );
  242. }
  243. $this->view->keyword_callback_number = $callback_number;
  244. }
  245. }