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

/source/components/com_pfcomments/site/models/form.php

https://github.com/projectfork/Projectfork
PHP | 137 lines | 58 code | 25 blank | 54 comment | 8 complexity | 0a4414cb12c66ab769f142d330d7cce2 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Projectfork
  4. * @subpackage Comments
  5. *
  6. * @author Tobias Kuhn (eaxs)
  7. * @copyright Copyright (C) 2006-2012 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. // Base this model on the backend version.
  12. JLoader::register('PFcommentsModelComment', JPATH_ADMINISTRATOR . '/components/com_pfcomments/models/comment.php');
  13. /**
  14. * Projectfork Component Comment Form Model
  15. *
  16. */
  17. class PFcommentsModelForm extends PFcommentsModelComment
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * @param array $config An optional associative array of configuration settings.
  23. *
  24. * @see jcontroller
  25. */
  26. public function __construct($config = array())
  27. {
  28. // Call parent constructor
  29. parent::__construct($config);
  30. }
  31. /**
  32. * Method to get item data.
  33. *
  34. * @param integer $id The id of the item.
  35. * @return mixed $value Item data object on success, false on failure.
  36. */
  37. public function getItem($id = null)
  38. {
  39. // Initialise variables.
  40. $id = (int) (!empty($id)) ? $id : $this->getState($this->getName() . '.id');
  41. // Get a row instance.
  42. $table = $this->getTable();
  43. // Attempt to load the row.
  44. $return = $table->load($id);
  45. // Check for a table object error.
  46. if ($return === false && $table->getError()) {
  47. $this->setError($table->getError());
  48. return false;
  49. }
  50. $properties = $table->getProperties(1);
  51. $value = JArrayHelper::toObject($properties, 'JObject');
  52. // Convert attrib field to Registry.
  53. $value->params = new JRegistry;
  54. $value->params->loadString($value->attribs);
  55. // Count parent replies
  56. $value->parent_replies = $this->countReplies($value->parent_id);
  57. // Count replies of this item
  58. $value->replies = $this->countReplies($value->id);
  59. // Compute selected asset permissions.
  60. $uid = JFactory::getUser()->get('id');
  61. $access = PFcommentsHelper::getActions($value->id);
  62. // Check general edit permission first.
  63. if ($access->get('core.edit')) {
  64. $value->params->set('access-edit', true);
  65. }
  66. // Now check if edit.own is available.
  67. elseif (!empty($uid) && $access->get('core.edit.own')) {
  68. // Check for a valid user and that they are the owner.
  69. if ($uid == $value->created_by) {
  70. $value->params->set('access-edit', true);
  71. }
  72. }
  73. // Check edit state permission.
  74. if ($id) {
  75. // Existing item
  76. $value->params->set('access-change', $access->get('core.edit.state'));
  77. }
  78. else {
  79. // New item
  80. $access = PFcommentsHelper::getActions();
  81. $value->params->set('access-change', $access->get('core.edit.state'));
  82. }
  83. return $value;
  84. }
  85. /**
  86. * Get the return URL.
  87. *
  88. * @return string The return URL.
  89. */
  90. public function getReturnPage()
  91. {
  92. return base64_encode($this->getState('return_page'));
  93. }
  94. /**
  95. * Method to auto-populate the model state.
  96. * Note. Calling getState in this method will result in recursion.
  97. *
  98. * @return void
  99. */
  100. protected function populateState()
  101. {
  102. // Load state from the request.
  103. $pk = JRequest::getInt('id');
  104. $this->setState($this->getName() . '.id', $pk);
  105. $return = JRequest::getVar('return', null, 'default', 'base64');
  106. $this->setState('return_page', base64_decode($return));
  107. // Load the parameters.
  108. $params = JFactory::getApplication()->getParams();
  109. $this->setState('params', $params);
  110. $this->setState('layout', JRequest::getCmd('layout'));
  111. }
  112. }