PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/backend/modules/gtu/actions/actions.class.php

https://github.com/PolloFox/Darwin
PHP | 288 lines | 235 code | 37 blank | 16 comment | 29 complexity | fba3d8281ca1d76e6d8d82b253918fdb MD5 | raw file
  1. <?php
  2. /**
  3. * Gtu actions.
  4. *
  5. * @package darwin
  6. * @subpackage GTU
  7. * @author DB team <collections@naturalsciences.be>
  8. * @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  9. */
  10. class gtuActions extends DarwinActions
  11. {
  12. protected $widgetCategory = 'catalogue_gtu_widget';
  13. public function preExecute()
  14. {
  15. if (strstr('purposetag,andsearch,completetag',$this->getActionName()) )
  16. {
  17. if(! $this->getUser()->isAtLeast(Users::ENCODER))
  18. {
  19. $this->forwardToSecureAction();
  20. }
  21. }
  22. }
  23. public function executeChoose(sfWebRequest $request)
  24. {
  25. $this->form = new GtuFormFilter();
  26. $this->form->addValue(0);
  27. }
  28. public function executeIndex(sfWebRequest $request)
  29. {
  30. $this->form = new GtuFormFilter();
  31. $this->form->addValue(0);
  32. }
  33. public function executeSearch(sfWebRequest $request)
  34. {
  35. $this->setCommonValues('gtu', 'code', $request);
  36. $this->form = new GtuFormFilter();
  37. $this->is_choose = ($request->getParameter('is_choose', '') == '')?0:intval($request->getParameter('is_choose'));
  38. if($request->getParameter('gtu_filters','') !== '')
  39. {
  40. $this->form->bind($request->getParameter('gtu_filters'));
  41. if ($this->form->isValid())
  42. {
  43. //@TODO: We need to refactor and avoid doing too much queries when format is xml
  44. $query = $this->form->getQuery();
  45. $query->andWhere('code=code');
  46. if($request->getParameter('format') == 'xml' || $request->getParameter('format') == 'text')
  47. {
  48. $query->orderBy($this->orderBy .' '.$this->orderDir)
  49. ->andWhere('latitude is not null');
  50. $this->setLayout(false);
  51. if($request->getParameter('format') == 'xml')
  52. {
  53. $query->Limit($this->form->getValue('rec_per_page'));
  54. $this->getResponse()->setContentType('application/xml');
  55. $this->items = $query->execute();
  56. $this->setTemplate('georss');
  57. return;
  58. }
  59. else
  60. {
  61. //return $this->renderText($this->form->getValue('rec_per_page').'/'.$query->count());
  62. sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
  63. $str = format_number_choice('[0]No Results Retrieved|[1]Your query retrieved 1 record|(1,+Inf]Your query retrieved %1% records', array('%1%' => $query->count()), $query->count());
  64. return $this->renderText($str);
  65. }
  66. }
  67. else
  68. {
  69. $query->orderBy($this->orderBy .' '.$this->orderDir);
  70. $this->pagerLayout = new PagerLayoutWithArrows(
  71. new DarwinPager(
  72. $query,
  73. $this->currentPage,
  74. $this->form->getValue('rec_per_page')
  75. ),
  76. new Doctrine_Pager_Range_Sliding(
  77. array('chunk' => $this->pagerSlidingSize)
  78. ),
  79. $this->getController()->genUrl($this->s_url.$this->o_url).'/page/{%page_number}'
  80. );
  81. // Sets the Pager Layout templates
  82. $this->setDefaultPaggingLayout($this->pagerLayout);
  83. // If pager not yet executed, this means the query has to be executed for data loading
  84. if (! $this->pagerLayout->getPager()->getExecuted())
  85. $this->items = $this->pagerLayout->execute();
  86. }
  87. $gtu_ids = array();
  88. foreach($this->items as $i)
  89. $gtu_ids[] = $i->getId();
  90. $tag_groups = Doctrine::getTable('TagGroups')->fetchByGtuRefs($gtu_ids);
  91. foreach($this->items as $i)
  92. {
  93. $i->TagGroups = new Doctrine_Collection('TagGroups');
  94. foreach($tag_groups as $t)
  95. {
  96. if( $t->getGtuRef() == $i->getId())
  97. {
  98. $i->TagGroups[]= $t;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. public function executeNew(sfWebRequest $request)
  106. {
  107. $gtu = new Gtu() ;
  108. $duplic = $request->getParameter('duplicate_id','0');
  109. $gtu = $this->getRecordIfDuplicate($duplic, $gtu);
  110. // if there is no duplicate $gtu is an empty array
  111. $this->form = new GtuForm($gtu);
  112. if ($duplic)
  113. {
  114. $Tag = Doctrine::getTable('TagGroups')->fetchTag($duplic) ;
  115. if(count($Tag))
  116. {
  117. foreach ($Tag[$duplic] as $key=>$val)
  118. {
  119. $tag = new TagGroups() ;
  120. $tag = $this->getRecordIfDuplicate($val->getId(), $tag);
  121. $this->form->addValue($key, $val->getGroupName(), $tag);
  122. }
  123. }
  124. }
  125. }
  126. public function executeCreate(sfWebRequest $request)
  127. {
  128. $this->forward404Unless($request->isMethod(sfRequest::POST));
  129. $this->form = new GtuForm();
  130. $this->processForm($request, $this->form);
  131. $this->setTemplate('new');
  132. }
  133. public function executeEdit(sfWebRequest $request)
  134. {
  135. $this->forward404Unless($gtu = Doctrine::getTable('Gtu')->findExcept($request->getParameter('id')), sprintf('Object gtu does not exist (%s).', $request->getParameter('id')));
  136. $this->no_right_col = Doctrine::getTable('Gtu')->testNoRightsCollections('gtu_ref',$request->getParameter('id'), $this->getUser()->getId());
  137. $this->form = new GtuForm($gtu);
  138. $this->loadWidgets();
  139. }
  140. public function executeUpdate(sfWebRequest $request)
  141. {
  142. $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
  143. $this->forward404Unless($gtu = Doctrine::getTable('Gtu')->findExcept($request->getParameter('id')), sprintf('Object gtu does not exist (%s).', $request->getParameter('id')));
  144. $this->no_right_col = Doctrine::getTable('Gtu')->testNoRightsCollections('gtu_ref',$request->getParameter('id'), $this->getUser()->getId());
  145. $this->form = new GtuForm($gtu);
  146. $this->processForm($request, $this->form);
  147. $this->no_right_col = Doctrine::getTable('Gtu')->testNoRightsCollections('gtu_ref',$request->getParameter('id'), $this->getUser()->getId());
  148. $this->loadWidgets();
  149. $this->setTemplate('edit');
  150. }
  151. public function executeDelete(sfWebRequest $request)
  152. {
  153. $request->checkCSRFProtection();
  154. $this->forward404Unless($unit = Doctrine::getTable('Gtu')->findExcept($request->getParameter('id')), sprintf('Object gtu does not exist (%s).', $request->getParameter('id')));
  155. if(! $request->hasParameter('confirm'))
  156. {
  157. $this->number_child = Doctrine::getTable('Gtu')->hasChildrens('Gtu',$unit->getId());
  158. if($this->number_child)
  159. {
  160. $this->link_delete = 'gtu/delete?confirm=1&id='.$unit->getId();
  161. $this->link_cancel = 'gtu/edit?id='.$unit->getId();
  162. $this->setTemplate('warndelete', 'catalogue');
  163. return;
  164. }
  165. }
  166. try
  167. {
  168. $unit->delete();
  169. $this->redirect('gtu/index');
  170. }
  171. catch(Doctrine_Exception $ne)
  172. {
  173. $e = new DarwinPgErrorParser($ne);
  174. $error = new sfValidatorError(new savedValidator(),$e->getMessage());
  175. $this->form = new GtuForm($unit);
  176. $this->form->getErrorSchema()->addError($error);
  177. $this->loadWidgets();
  178. $this->no_right_col = Doctrine::getTable('Gtu')->testNoRightsCollections('gtu_ref',$request->getParameter('id'), $this->getUser()->getId());
  179. $this->setTemplate('edit');
  180. }
  181. }
  182. protected function processForm(sfWebRequest $request, sfForm $form)
  183. {
  184. $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  185. if ($form->isValid())
  186. {
  187. try
  188. {
  189. $item = $form->save();
  190. $this->redirect('gtu/edit?id='.$item->getId());
  191. }
  192. catch(Doctrine_Exception $ne)
  193. {
  194. $e = new DarwinPgErrorParser($ne);
  195. $error = new sfValidatorError(new savedValidator(),$e->getMessage());
  196. $form->getErrorSchema()->addError($error);
  197. }
  198. }
  199. }
  200. public function executePurposeTag(sfWebRequest $request)
  201. {
  202. $this->tags = Doctrine::getTable('TagGroups')->getPropositions($request->getParameter('value'), $request->getParameter('group_name'), $request->getParameter('sub_group_name'));
  203. }
  204. public function executeAddGroup(sfWebRequest $request)
  205. {
  206. $number = intval($request->getParameter('num'));
  207. $gtu = null;
  208. if($request->hasParameter('id') && $request->getParameter('id'))
  209. $gtu = Doctrine::getTable('Gtu')->findExcept($request->getParameter('id') );
  210. $form = new GtuForm($gtu);
  211. $form->addValue($number, $request->getParameter('group'));
  212. return $this->renderPartial('taggroups',array('form' => $form['newVal'][$number]));
  213. }
  214. public function executeAndSearch(sfWebRequest $request)
  215. {
  216. $number = intval($request->getParameter('num'));
  217. $form = new GtuFormFilter();
  218. $form->addValue($number);
  219. return $this->renderPartial('andSearch',array('form' => $form['Tags'][$number], 'row_line' => $number));
  220. }
  221. /**
  222. * Return tags for a GTU without the country part
  223. */
  224. public function executeCompleteTag(sfWebRequest $request)
  225. {
  226. $gtu = false;
  227. if($request->hasParameter('id') && $request->getParameter('id'))
  228. {
  229. $spec = Doctrine::getTable('Specimens')->fetchOneWithRights($request->getParameter('id'), $this->getUser());
  230. if($spec->getHasEncodingRights() || $this->getUser()->isAtLeast(Users::ADMIN))
  231. $gtu = Doctrine::getTable('Gtu')->findExcept($spec->getGtuRef() );
  232. else
  233. $this->forwardToSecureAction();
  234. }
  235. $this->forward404Unless($gtu);
  236. $str = '<ul class="search_tags">';
  237. foreach($gtu->TagGroups as $group)
  238. {
  239. $str .= '<li><label>'.$group->getSubGroupName().'<span class="gtu_group"> - '.TagGroups::getGroup($group->getGroupName()).'</span></label>';
  240. if($request->hasParameter('view')) $str .= '<ul class="name_tags_view">' ;
  241. else $str .= '<ul class="name_tags">' ;
  242. $tags = explode(";",$group->getTagValue());
  243. foreach($tags as $value)
  244. if (strlen($value))
  245. $str .= '<li>' . trim($value).'</li>';
  246. $str .= '</ul><div class="clear" />';
  247. }
  248. $str .= '</ul><div class="clear" />';
  249. return $this->renderText($str);
  250. }
  251. }