PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/poll/controllers/config.php

http://github.com/tcm-project/tangocms
PHP | 334 lines | 241 code | 14 blank | 79 comment | 41 complexity | 81612e556b2941642cc363d3b876844d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Zula Framework Module
  4. *
  5. * @patches submit all patches to patches@tangocms.org
  6. *
  7. * @author Alex Cartwright
  8. * @copyright Copyright (C) 2007, 2008, 2009, 2010 Alex Cartwright
  9. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL 2
  10. * @package TangoCMS_Poll
  11. */
  12. class Poll_controller_config extends Zula_ControllerBase {
  13. /**
  14. * Amount of Polls to display per page
  15. */
  16. const _PER_PAGE = 12;
  17. /**
  18. * Constructor
  19. * Sets the common page links
  20. */
  21. public function __construct( $moduleDetails, $config, $sector ) {
  22. parent::__construct( $moduleDetails, $config, $sector );
  23. $this->setPageLinks( array(
  24. t('Manage polls') => $this->_router->makeUrl( 'poll', 'config' ),
  25. t('Add poll') => $this->_router->makeUrl( 'poll', 'config', 'add'),
  26. ));
  27. }
  28. /**
  29. * Displays all polls (with pagination), or deletes the selected
  30. *
  31. * @return string|bool
  32. */
  33. public function indexSection() {
  34. $this->setTitle( t('Manage polls') );
  35. $this->setOutputType( self::_OT_CONFIG );
  36. if ( $this->_input->checkToken() ) {
  37. // Attempt to delete all selected polls
  38. if ( !$this->_acl->check( 'poll_delete' ) ) {
  39. throw new Module_NoPermission;
  40. }
  41. try {
  42. $pollIds = $this->_input->post( 'poll_ids' );
  43. $count = 0;
  44. foreach( $pollIds as $pid ) {
  45. // check if user has permission to the poll
  46. $aclResource = 'poll-'.$pid;
  47. if ( $this->_acl->resourceExists( $aclResource ) && $this->_acl->check( $aclResource ) ) {
  48. $this->_model()->deletePoll( $pid );
  49. ++$count;
  50. }
  51. }
  52. if ( $count > 0 ) {
  53. $this->_event->success( t('Deleted selected polls') );
  54. }
  55. } catch ( Input_KeyNoExist $e ) {
  56. $this->_event->error( t('No polls selected') );
  57. }
  58. return zula_redirect( $this->_router->makeUrl( 'poll', 'config' ) );
  59. } else if ( $this->_acl->checkMulti( array('poll_add', 'poll_edit', 'poll_delete') ) ) {
  60. // Display certain amount of polls
  61. try {
  62. $curPage = abs( $this->_input->get('page')-1 );
  63. } catch ( Input_KeyNoExist $e ) {
  64. $curPage = 0;
  65. }
  66. $polls = $this->_model()->getAllPolls( self::_PER_PAGE, ($curPage*self::_PER_PAGE) );
  67. $pollCount = $this->_model()->getCount();
  68. if ( $pollCount > 0 ) {
  69. $pagination = new Pagination( $pollCount, self::_PER_PAGE );
  70. }
  71. $view = $this->loadView( 'config/overview.html' );
  72. $view->assign( array(
  73. 'POLLS' => $polls,
  74. 'COUNT' => $pollCount,
  75. ));
  76. $view->assignHtml( array(
  77. 'PAGINATION' => isset($pagination) ? $pagination->build() : '',
  78. 'CSRF' => $this->_input->createToken( true ),
  79. ));
  80. return $view->getOutput();
  81. } else {
  82. throw new Module_NoPermission;
  83. }
  84. }
  85. /**
  86. * Displays and handles adding of a new poll
  87. *
  88. * @return string|bool
  89. */
  90. public function addSection() {
  91. if ( !$this->_acl->check( 'poll_add' ) ) {
  92. throw new Module_NoPermission;
  93. }
  94. $this->setTitle( t('Add poll') );
  95. $this->setOutputType( self::_OT_CONFIG );
  96. // Get and check if form is valid
  97. $form = $this->buildPollForm();
  98. if ( $form->hasInput() && $form->isValid() ) {
  99. $fd = $form->getValues( 'poll' );
  100. $pid = $this->_model()->addPoll( $fd['title'], $fd['duration'], $fd['options'] );
  101. $this->_event->success( t('Added poll') );
  102. // Update the ACL resource
  103. try {
  104. $roles = $this->_input->post( 'acl_resources/poll-' );
  105. } catch ( Input_KeyNoExist $e ) {
  106. $roles = array();
  107. }
  108. $this->_acl->allowOnly( 'poll-'.$pid, $roles );
  109. return zula_redirect( $this->_router->makeUrl( 'poll', 'config' ) );
  110. }
  111. $this->addAsset( 'js/options.js' );
  112. return $form->getOutput();
  113. }
  114. /**
  115. * Handles editing an existing poll
  116. *
  117. * @return string
  118. */
  119. public function editSection() {
  120. if ( !$this->_acl->check( 'poll_edit' ) ) {
  121. throw new Module_NoPermission;
  122. }
  123. $this->setTitle( t('Edit poll') );
  124. $this->setOutputType( self::_OT_CONFIG );
  125. try {
  126. $pid = $this->_router->getArgument( 'id' );
  127. $poll = $this->_model()->getPoll( $pid );
  128. // Check if user has permission
  129. $aclResource = 'poll-'.$pid;
  130. if ( !$this->_acl->resourceExists( $aclResource ) || !$this->_acl->check( $aclResource ) ) {
  131. throw new Module_NoPermission;
  132. }
  133. $this->setTitle( sprintf( t('Edit poll "%s"'), $poll['title'] ) );
  134. // Get and check if form is valid
  135. $form = $this->buildPollForm( $pid, $poll['title'], $poll['duration'],
  136. $poll['status'], $this->_model()->getPollOptions($pid)
  137. );
  138. if ( $form->hasInput() && $form->isValid() ) {
  139. $fd = $form->getValues( 'poll' );
  140. $this->_model()->editPoll( $pid, $fd['title'], $fd['duration'], $fd['status'] );
  141. $this->_event->success( t('Edited poll') );
  142. // Update ACL resource
  143. try {
  144. $roles = $this->_input->post( 'acl_resources/poll-'.$pid );
  145. } catch ( Input_KeyNoExist $e ) {
  146. $roles = array();
  147. }
  148. $this->_acl->allowOnly( 'poll-'.$pid, $roles );
  149. } else {
  150. return $form->getOutput();
  151. }
  152. } catch ( Router_ArgNoExist $e ) {
  153. } catch ( Poll_NoExist $e ) {
  154. $this->_event->error( t('Poll does not exist') );
  155. }
  156. return zula_redirect( $this->_router->makeUrl( 'poll', 'config' ) );
  157. }
  158. /**
  159. * Builds the form for adding or editing a poll
  160. *
  161. * @param int $pid
  162. * @param string $title
  163. * @param int $duration
  164. * @param string $status
  165. * @param array $options
  166. * @return object
  167. */
  168. protected function buildPollForm( $pid=null, $title=null, $duration=1, $status='active', $options=array() ) {
  169. $op = $pid === null ? 'add' : 'edit';
  170. $form = new View_form( 'config/form_poll.html', 'poll' );
  171. $form->addElement( 'poll/title', $title, t('Title'), new Validator_Length(1, 255) );
  172. $form->addElement( 'poll/duration', $duration, t('Duration'), new Validator_Between(0, 29030400) );
  173. $form->addElement( 'poll/status', $status, t('Status'), new Validator_InArray( array('active', 'closed') ) );
  174. if ( $op == 'add' ) {
  175. $form->addElement( 'poll/options', $options, t('Options'), new Validator_Between(2, 100) );
  176. try {
  177. foreach( $this->_input->post( 'poll/options' ) as $key=>$tmpOpt ) {
  178. $form->addElement( 'poll/options/'.$key, $key, sprintf( t('Option %1$d'), $key+1 ), new Validator_Length(1, 255) );
  179. }
  180. } catch ( Input_KeyNoExist $e ) {
  181. }
  182. } else {
  183. $form->assign( array('poll' => array('options' => $options)) );
  184. }
  185. $form->assign( array(
  186. 'OP' => $op,
  187. 'ID' => $pid,
  188. ));
  189. $form->assignHtml( array(
  190. 'ACL_FORM' => $this->_acl->buildForm( array(t('View poll') => 'poll-'.$pid) ),
  191. ));
  192. return $form;
  193. }
  194. /**
  195. * Adds a new poll option to a poll
  196. *
  197. * @return string|bool
  198. */
  199. public function addOptSection() {
  200. if ( !$this->_acl->check( 'poll_edit' ) ) {
  201. throw new Module_NoPermission;
  202. }
  203. $this->setTitle( t('Add option') );
  204. $this->setOutputType( self::_OT_CONFIG );
  205. // Get which poll to add the option to
  206. try {
  207. $pid = $this->_router->getArgument( 'id' );
  208. $poll = $this->_model()->getPoll( $pid );
  209. // check if user has permission to the poll
  210. $aclResource = 'poll-'.$poll['id'];
  211. if ( !$this->_acl->resourceExists( $aclResource ) || !$this->_acl->check( $aclResource ) ) {
  212. throw new Module_NoPermission;
  213. }
  214. // Build form and check it is valid
  215. $form = $this->buildOptionForm();
  216. if ( $form->hasInput() && $form->isValid() ) {
  217. $this->_model()->addOption( $poll['id'], $form->getValues( 'poll/title' ) );
  218. $this->_event->success( t('Added poll option') );
  219. return zula_redirect( $this->_router->makeUrl( 'poll', 'config', 'edit', null, array('id' => $poll['id']) ) );
  220. }
  221. return $form->getOutput();
  222. } catch ( Router_ArgNoExist $e ) {
  223. $this->_event->error( t('No poll selected') );
  224. } catch ( Poll_NoExist $e ) {
  225. $this->_event->error( t('Poll does not exist') );
  226. }
  227. return zula_redirect( $this->_router->makeUrl( 'poll', 'config' ) );
  228. }
  229. /**
  230. * Edits an existing poll option for a poll (only if user has permission
  231. * to the parent poll.)
  232. *
  233. * @return string|bool
  234. */
  235. public function editOptSection() {
  236. if ( !$this->_acl->check( 'poll_edit' ) ) {
  237. throw new Module_NoPermission;
  238. }
  239. $this->setTitle( t('Edit poll option') );
  240. $this->setOutputType( self::_OT_CONFIG );
  241. // Get which option we are to edit
  242. try {
  243. $optionId = $this->_router->getArgument( 'id' );
  244. $option = $this->_model()->getOption( $optionId );
  245. // check user permission
  246. $aclResource = 'poll-'.$option['poll_id'];
  247. if ( !$this->_acl->resourceExists( $aclResource ) || !$this->_acl->check( $aclResource ) ) {
  248. throw new Module_NoPermission;
  249. }
  250. $form = $this->buildOptionForm( $option['title'], $option['id'] );
  251. if ( $form->hasInput() && $form->isValid() ) {
  252. $this->_model()->editOption( $option['id'], $form->getValues( 'poll/title' ) );
  253. $this->_event->success( t('Edited poll option') );
  254. return zula_redirect( $this->_router->makeUrl( 'poll', 'config', 'edit', null, array('id' => $option['poll_id']) ) );
  255. }
  256. return $form->getOutput();
  257. } catch ( Router_ArgNoExist $e ) {
  258. $this->_event->error( t('No option selected') );
  259. } catch ( Poll_OptionNoExist $e ) {
  260. $this->_event->error( t('Option does not exist') );
  261. }
  262. return zula_redirect( $this->_router->makeUrl( 'poll', 'config' ) );
  263. }
  264. /**
  265. * Builds the form for adding or editing a poll option
  266. *
  267. * @param string $title
  268. * @param int $optionId
  269. * @return object
  270. */
  271. protected function buildOptionForm( $title=null, $optionId=null ) {
  272. $op = is_null($optionId) ? 'add' : 'edit';
  273. $form = new View_form( 'config/form_option.html', 'poll', ($op == 'add') );
  274. $form->addElement( 'poll/title', $title, t('Title'), new Validator_Length(1, 255) );
  275. $form->assign( array(
  276. 'ID' => $optionId,
  277. 'OP' => $op,
  278. ));
  279. return $form;
  280. }
  281. /**
  282. * Deletes all selected poll options
  283. *
  284. * @return string
  285. */
  286. public function delOptSection() {
  287. $this->setOutputType( self::_OT_CONFIG );
  288. if ( !$this->_acl->check( 'poll_delete' ) ) {
  289. throw new Module_NoPermission;
  290. } else if ( !$this->_input->checkToken() ) {
  291. $this->_event->error( Input::csrfMsg() );
  292. } else {
  293. try {
  294. $poll = $this->_model()->getPoll( $this->_router->getArgument('id') );
  295. // Check user has permission
  296. $resource = 'poll-'.$poll['id'];
  297. if ( $this->_acl->resourceExists( $resource ) && $this->_acl->check( $resource ) ) {
  298. $optionIds = $this->_input->post( 'option_ids' );
  299. foreach( (array) $optionIds as $oid ) {
  300. try {
  301. $this->_model()->deleteOption( $oid );
  302. } catch ( Poll_OptionNoExist $e ) {
  303. }
  304. }
  305. $this->_event->success( t('Deleted selected options') );
  306. } else {
  307. throw new Module_NoPermission;
  308. }
  309. } catch ( Input_KeyNoExist $e ) {
  310. $this->_event->error( t('No options selected') );
  311. }
  312. }
  313. if ( isset( $poll['id'] ) ) {
  314. return zula_redirect( $this->_router->makeUrl( 'poll', 'config', 'edit', null, array('id' => $poll['id']) ) );
  315. } else {
  316. return zula_redirect( $this->_router->makeUrl( 'poll', 'config' ) );
  317. }
  318. }
  319. }
  320. ?>