PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/source/components/com_pfforum/site/controllers/topicform.php

https://github.com/projectfork/Projectfork
PHP | 270 lines | 108 code | 52 blank | 110 comment | 21 complexity | 58892a19e36afc399381fa606c8f98ac MD5 | raw file
  1. <?php
  2. /**
  3. * @package pkg_projectfork
  4. * @subpackage com_pfforum
  5. *
  6. * @author Tobias Kuhn (eaxs)
  7. * @copyright Copyright (C) 2006-2013 Tobias Kuhn. All rights reserved.
  8. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
  9. */
  10. defined('_JEXEC') or die();
  11. jimport('joomla.application.component.controllerform');
  12. /**
  13. * Projectfork Topic Form Controller
  14. *
  15. */
  16. class PFforumControllerTopicform extends JControllerForm
  17. {
  18. /**
  19. * The default item view
  20. *
  21. * @var string
  22. */
  23. protected $view_item = 'topicform';
  24. /**
  25. * The default list view
  26. *
  27. * @var string
  28. */
  29. protected $view_list = 'topics';
  30. /**
  31. * The prefix to use with controller messages.
  32. *
  33. * @var string
  34. */
  35. protected $text_prefix = 'COM_PROJECTFORK_TOPIC';
  36. /**
  37. * Method to get a model object, loading it if required.
  38. *
  39. * @param string $name The model name. Optional.
  40. * @param string $prefix The class prefix. Optional.
  41. * @param array $config Configuration array for model. Optional.
  42. *
  43. * @return object The model.
  44. */
  45. public function &getModel($name = 'TopicForm', $prefix = 'PFforumModel', $config = array('ignore_request' => true))
  46. {
  47. $model = parent::getModel($name, $prefix, $config);
  48. return $model;
  49. }
  50. /**
  51. * Method to add a new record.
  52. *
  53. * @return boolean True if the item can be added, false if not.
  54. */
  55. public function add()
  56. {
  57. if (!parent::add()) {
  58. // Redirect to the return page.
  59. $this->setRedirect($this->getReturnPage());
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * Method to cancel an edit.
  66. *
  67. * @param string $key The name of the primary key of the URL variable.
  68. *
  69. * @return boolean True if access level checks pass, false otherwise.
  70. */
  71. public function cancel($key = 'id')
  72. {
  73. $result = parent::cancel($key);
  74. // Redirect to the return page.
  75. $this->setRedirect($this->getReturnPage());
  76. return $result;
  77. }
  78. /**
  79. * Method to save a record.
  80. *
  81. * @param string $key The name of the primary key of the URL variable.
  82. * @param string $url_var The name of the URL variable if different from the primary key.
  83. *
  84. * @return boolean True if successful, false otherwise.
  85. */
  86. public function save($key = null, $url_var = 'id')
  87. {
  88. $result = parent::save($key, $url_var);
  89. // If ok, redirect to the return page.
  90. if ($result) $this->setRedirect($this->getReturnPage());
  91. return $result;
  92. }
  93. /**
  94. * Method to check if you can add a new record.
  95. *
  96. * @param array $data An array of input data.
  97. *
  98. * @return boolean
  99. */
  100. protected function allowAdd($data = array())
  101. {
  102. // Get form input
  103. $project = isset($data['project_id']) ? (int) $data['project_id'] : PFApplicationHelper::getActiveProjectId();
  104. $user = JFactory::getUser();
  105. $asset = 'com_pfforum';
  106. $access = true;
  107. if ($project) {
  108. // Check if the user has viewing access when not a super admin
  109. if (!$user->authorise('core.admin')) {
  110. $access = in_array($project, PFUserHelper::getAuthorisedProjects());
  111. }
  112. // Change the asset name
  113. $asset .= '.project.' . $project;
  114. }
  115. return ($user->authorise('core.create', $asset) && $access);
  116. }
  117. /**
  118. * Method override to check if you can edit an existing record.
  119. *
  120. * @param array $data An array of input data.
  121. * @param string $key The name of the key for the primary key.
  122. *
  123. * @return boolean
  124. */
  125. protected function allowEdit($data = array(), $key = 'id')
  126. {
  127. // Get form input
  128. $id = (int) isset($data[$key]) ? $data[$key] : 0;
  129. $user = JFactory::getUser();
  130. $uid = $user->get('id');
  131. $asset = 'com_pfforum.topic.' . $id;
  132. // Check if the user has viewing access when not a super admin
  133. if (!$user->authorise('core.admin')) {
  134. $db = JFactory::getDbo();
  135. $query = $db->getQuery(true);
  136. $query->select('access')
  137. ->from('#__pf_topics')
  138. ->where('id = ' . $id);
  139. $db->setQuery($query);
  140. $lvl = $db->loadResult();
  141. if (!in_array($lvl, $user->getAuthorisedViewLevels())) {
  142. return false;
  143. }
  144. }
  145. // Check edit permission first
  146. if ($user->authorise('core.edit', $asset)) {
  147. return true;
  148. }
  149. // Fallback on edit.own.
  150. // First test if the permission is available.
  151. if (!$user->authorise('core.edit.own', $asset)) {
  152. return false;
  153. }
  154. // Load the item
  155. $record = $this->getModel()->getItem($id);
  156. // Abort if not found
  157. if (empty($record)) return false;
  158. // Now test the owner is the user.
  159. $owner = (int) isset($data['created_by']) ? (int) $data['created_by'] : $record->created_by;
  160. // If the owner matches 'me' then do the test.
  161. return ($owner == $uid && $uid > 0);
  162. }
  163. /**
  164. * Gets the URL arguments to append to an item redirect.
  165. *
  166. * @param int $id The primary key id for the item.
  167. * @param string $url_var The name of the URL variable for the id.
  168. *
  169. * @return string The arguments to append to the redirect URL.
  170. */
  171. protected function getRedirectToItemAppend($id = null, $url_var = 'id')
  172. {
  173. // Need to override the parent method completely.
  174. $tmpl = JRequest::getCmd('tmpl');
  175. $layout = JRequest::getCmd('layout', 'edit');
  176. $item_id = JRequest::getUInt('Itemid');
  177. $return = $this->getReturnPage();
  178. $append = '';
  179. // Setup redirect info.
  180. if ($tmpl) $append .= '&tmpl=' . $tmpl;
  181. $append .= '&layout=edit';
  182. if ($id) $append .= '&' . $url_var . '=' . $id;
  183. if ($item_id) $append .= '&Itemid=' . $item_id;
  184. if ($return) $append .= '&return='.base64_encode($return);
  185. return $append;
  186. }
  187. /**
  188. * Get the return URL.
  189. * If a "return" variable has been passed in the request
  190. *
  191. * @return string The return URL.
  192. */
  193. protected function getReturnPage()
  194. {
  195. $return = JRequest::getVar('return', null, 'default', 'base64');
  196. if (empty($return) || !JUri::isInternal(base64_decode($return))) {
  197. return JRoute::_('index.php?option=com_pfforum&view=' . $this->view_list, false);
  198. }
  199. else {
  200. return base64_decode($return);
  201. }
  202. }
  203. /**
  204. * Function that allows child controller access to model data after the data has been saved.
  205. *
  206. * @param jmodel $model The data model object.
  207. * @param array $data The validated data.
  208. *
  209. * @return void
  210. */
  211. protected function postSaveHook(&$model, $data)
  212. {
  213. $task = $this->getTask();
  214. if ($task == 'save') {
  215. $this->setRedirect(JRoute::_('index.php?option=com_pfforum&view=' . $this->view_list, false));
  216. }
  217. }
  218. }