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

/Comments/Model/Comment.php

https://github.com/kareypowell/croogo
PHP | 241 lines | 128 code | 27 blank | 86 comment | 13 complexity | 432ee56e1a95892f9dd624286bb57447 MD5 | raw file
  1. <?php
  2. App::uses('AppModel', 'Model');
  3. App::uses('CroogoStatus', 'Croogo.Lib');
  4. /**
  5. * Comment
  6. *
  7. * @category Model
  8. * @package Croogo.Comments.Model
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class Comment extends AppModel {
  15. /**
  16. * Model name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Comment';
  22. /**
  23. * @deprecated
  24. */
  25. const STATUS_APPROVED = 1;
  26. /**
  27. * @deprecated
  28. */
  29. const STATUS_PENDING = 0;
  30. /**
  31. * Behaviors used by the Model
  32. *
  33. * @var array
  34. * @access public
  35. */
  36. public $actsAs = array(
  37. 'Tree',
  38. 'Croogo.Cached' => array(
  39. 'groups' => array(
  40. 'comments',
  41. 'nodes',
  42. ),
  43. ),
  44. 'Croogo.Publishable',
  45. 'Croogo.Trackable',
  46. 'Search.Searchable',
  47. );
  48. /**
  49. * Validation
  50. *
  51. * @var array
  52. * @access public
  53. */
  54. public $validate = array(
  55. 'body' => array(
  56. 'rule' => 'notEmpty',
  57. 'message' => 'This field cannot be left blank.',
  58. ),
  59. 'name' => array(
  60. 'rule' => 'notEmpty',
  61. 'message' => 'This field cannot be left blank.',
  62. ),
  63. 'email' => array(
  64. 'rule' => 'email',
  65. 'required' => true,
  66. 'message' => 'Please enter a valid email address.',
  67. ),
  68. );
  69. /**
  70. * Model associations: belongsTo
  71. *
  72. * @var array
  73. * @access public
  74. */
  75. public $belongsTo = array(
  76. 'User' => array(
  77. 'className' => 'Users.User',
  78. ),
  79. );
  80. /**
  81. * Filter fields
  82. *
  83. * @var array
  84. */
  85. public $filterArgs = array(
  86. 'status' => array('type' => 'value'),
  87. );
  88. /**
  89. * Add a new Comment
  90. *
  91. * Options:
  92. * - parentId id of parent comment (if it is a reply)
  93. * - userData author data (User data (if logged in) / Author fields from Comment form)
  94. *
  95. * @param array $data Comment data (Usually POSTed data from Comment form)
  96. * @param string $model Model alias
  97. * @param int $foreignKey Foreign Key (Node Id from where comment was posted).
  98. * @param array $options Options
  99. * @return bool true if comment was added, false otherwise.
  100. * @throws NotFoundException
  101. */
  102. public function add($data, $model, $foreignKey, $options = array()) {
  103. $options = Hash::merge(array(
  104. 'parentId' => null,
  105. 'userData' => array(),
  106. ), $options);
  107. $record = array();
  108. $node = array();
  109. $foreignKey = (int)$foreignKey;
  110. $parentId = is_null($options['parentId']) ? null : (int)$options['parentId'];
  111. $userData = $options['userData'];
  112. if (empty($this->{$model})) {
  113. throw new UnexpectedValueException(sprintf('%s not configured for Comments', $model));
  114. }
  115. $node = $this->{$model}->findById($foreignKey);
  116. if (empty($node)) {
  117. throw new NotFoundException(__d('croogo', 'Invalid Node id'));
  118. }
  119. if (!is_null($parentId)) {
  120. if (
  121. $this->isValidLevel($parentId) &&
  122. $this->isApproved($parentId, $model, $foreignKey)
  123. ) {
  124. $record['parent_id'] = $parentId;
  125. } else {
  126. return false;
  127. }
  128. }
  129. if (!empty($userData) && is_array($userData)) {
  130. $record['user_id'] = $userData['User']['id'];
  131. $record['name'] = $userData['User']['name'];
  132. $record['email'] = $userData['User']['email'];
  133. $record['website'] = $userData['User']['website'];
  134. } else {
  135. $record['name'] = $data[$this->alias]['name'];
  136. $record['email'] = $data[$this->alias]['email'];
  137. $record['website'] = $data[$this->alias]['website'];
  138. }
  139. $record['ip'] = $data[$this->alias]['ip'];
  140. $record['model'] = $model;
  141. $record['foreign_key'] = $node[$this->{$model}->alias]['id'];
  142. $record['body'] = h($data[$this->alias]['body']);
  143. if (isset($node[$this->{$model}->alias]['type'])) {
  144. $record['type'] = $node[$this->{$model}->alias]['type'];
  145. } else {
  146. $record['type'] = '';
  147. }
  148. if (isset($data[$this->alias]['status'])) {
  149. $record['status'] = $data[$this->alias]['status'];
  150. } else {
  151. $record['status'] = CroogoStatus::PENDING;
  152. }
  153. return (bool)$this->save($record);
  154. }
  155. /**
  156. * Checks wether comment has been approved
  157. *
  158. * @param integer $commentId comment id
  159. * @param integer $nodeId node id
  160. * @return boolean true if comment is approved
  161. */
  162. public function isApproved($commentId, $model, $foreignKey) {
  163. return $this->hasAny(array(
  164. $this->escapeField() => $commentId,
  165. $this->escapeField('model') => $model,
  166. $this->escapeField('foreign_key') => $foreignKey,
  167. $this->escapeField('status') => 1,
  168. ));
  169. }
  170. /**
  171. * Checks wether comment is within valid level range
  172. *
  173. * @return boolean
  174. * @throws NotFoundException
  175. */
  176. public function isValidLevel($commentId) {
  177. if (!$this->exists($commentId)) {
  178. throw new NotFoundException(__d('croogo', 'Invalid Comment id'));
  179. }
  180. $path = $this->getPath($commentId, array($this->escapeField()));
  181. $level = count($path);
  182. return Configure::read('Comment.level') > $level;
  183. }
  184. /**
  185. * Change status of given Comment Ids
  186. *
  187. * @param array $ids array of Comment Ids
  188. * @param boolean
  189. * @return mixed
  190. * @see Model::saveMany()
  191. */
  192. public function changeStatus($ids, $status) {
  193. $dataArray = array();
  194. foreach ($ids as $id) {
  195. $dataArray[] = array(
  196. $this->primaryKey => $id,
  197. 'status' => $status
  198. );
  199. }
  200. return $this->saveMany($dataArray, array('validate' => false));
  201. }
  202. /**
  203. * Provide our own bulkPublish since BulkProcessBehavior::bulkPublish is incompatible with boolean status
  204. */
  205. public function bulkPublish($ids) {
  206. return $this->changeStatus($ids, true);
  207. }
  208. /**
  209. * Provide our own bulkUnpublish since BulkProcessBehavior::bulkUnpublish is incompatible with boolean status
  210. */
  211. public function bulkUnpublish($ids) {
  212. return $this->changeStatus($ids, false);
  213. }
  214. }