PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Comments/Model/Behavior/CommentableBehavior.php

https://github.com/kareypowell/croogo
PHP | 113 lines | 68 code | 9 blank | 36 comment | 8 complexity | c6e1dd2e2da1001f0611dab0c83da266 MD5 | raw file
  1. <?php
  2. App::uses('ModelBehavior', 'Model/Behavior');
  3. /**
  4. * CommentableBehavior
  5. *
  6. * @category Comments.Model.Behavior
  7. * @package Croogo.Comments.Model.Behavior
  8. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  9. * @link http://www.croogo.org
  10. */
  11. class CommentableBehavior extends ModelBehavior {
  12. /**
  13. * Setup behavior
  14. *
  15. * @return void
  16. */
  17. public function setup(Model $model, $config = array()) {
  18. $this->settings[$model->alias] = $config;
  19. $this->_setupRelationships($model);
  20. }
  21. /**
  22. * Setup relationships
  23. *
  24. * @return void
  25. */
  26. protected function _setupRelationships(Model $model) {
  27. $model->bindModel(array(
  28. 'hasMany' => array(
  29. 'Comment' => array(
  30. 'className' => 'Comments.Comment',
  31. 'foreignKey' => 'foreign_key',
  32. 'dependent' => true,
  33. 'limit' => 5,
  34. 'conditions' => array(
  35. 'model' => $model->alias,
  36. 'status' => (bool)1,
  37. ),
  38. ),
  39. ),
  40. ), false);
  41. }
  42. /**
  43. * beforeDelete callback
  44. *
  45. * @return boolean
  46. */
  47. public function beforeDelete(Model $model, $cascade = true) {
  48. if ($cascade) {
  49. if (isset($model->hasMany['Comment'])) {
  50. $model->hasMany['Comment']['conditions'] = '';
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * Get Comment settings from Type
  57. *
  58. * @param Model Model instance
  59. * @param array $data Model data to check
  60. * @return bool
  61. */
  62. public function getTypeSetting(Model $model, $data) {
  63. $defaultSetting = array(
  64. 'commentable' => false,
  65. 'autoApprove' => false,
  66. 'spamProtection' => false,
  67. 'captchaProtection' => false,
  68. );
  69. if (!CakePlugin::loaded('Taxonomy')) {
  70. return $defaultSetting;
  71. }
  72. if (empty($data[$model->alias]['type'])) {
  73. return $defaultSetting;
  74. }
  75. $Type = ClassRegistry::init('Taxonomy.Type');
  76. $type = $Type->find('first', array(
  77. 'recursive' => -1,
  78. 'conditions' => array(
  79. $Type->escapeField('alias') => $data[$model->alias]['type'],
  80. ),
  81. ));
  82. if ($type) {
  83. return array(
  84. 'commentable' => $type['Type']['comment_status'] == 2,
  85. 'autoApprove' => $type['Type']['comment_approve'] == 1,
  86. 'spamProtection' => $type['Type']['comment_spam_protection'],
  87. 'captchaProtection' => $type['Type']['comment_captcha'],
  88. );
  89. }
  90. return $defaultSetting;
  91. }
  92. /**
  93. * Convenience method for Comment::add()
  94. *
  95. * @return bool
  96. * @see Comment::add()
  97. */
  98. public function addComment(Model $Model, $data, $options = array()) {
  99. if (!isset($Model->id)) {
  100. throw new UnexpectedValueException('Id is not set');
  101. }
  102. return $Model->Comment->add($data, $Model->alias, $Model->id, $options);
  103. }
  104. }