/app/models/Answer.php

https://github.com/brtriver/sukonv · PHP · 131 lines · 94 code · 3 blank · 34 comment · 7 complexity · faa5dfbb4f31e54373da90c18fba64bc MD5 · raw file

  1. <?php
  2. namespace app\models;
  3. use \lithium\util\Validator;
  4. use \lithium\util\String;
  5. use app\models\Question;
  6. use app\models\User;
  7. use lithium\storage\Session;
  8. class Answer extends \lithium\data\Model {
  9. /**
  10. * to define validates
  11. */
  12. public $validates = array(
  13. 'parent_id' => array(
  14. array('isParentId', 'message' => 'Related Question does not exist.'),
  15. ),
  16. 'url' => array(
  17. array('isValidUrl', 'message' => 'URL is invalid.', 'skipEmpty' => true),
  18. ),
  19. 'description' => array(
  20. array('notEmpty', 'message' => 'Description is empty.'),
  21. ),
  22. 'framework' => array(
  23. array('notEmptyItems', 'message' => 'Framework is empty.'),
  24. /**
  25. * MEMO: default is required rule, so you want not to validate if empty, you let the required option set to false.
  26. *
  27. * @author maeda
  28. */
  29. array('inFrameworkItemList', 'message' => 'Framework is not in the list', 'required' => false),
  30. ),
  31. );
  32. /**
  33. * to define framework types
  34. *
  35. * @vars array value of framework
  36. */
  37. protected static $_frameworks = array(
  38. 'symfony1.0',
  39. 'symfony1.1',
  40. 'symfony1.2',
  41. 'symfony1.3',
  42. 'symfony1.4',
  43. 'Symfony2.0',
  44. 'CakePHP1.1',
  45. 'CakePHP1.2',
  46. 'CakePHP1.3',
  47. 'CakePHP2.0',
  48. 'Lithium0.x',
  49. );
  50. public static function __init(array $options = array()) {
  51. parent::__init($options);
  52. // for save
  53. Answer::applyFilter('save', function($self, $params, $chain) {
  54. // set created, modified
  55. // $chain->next($self, $params, $chain);
  56. // set answer document to question.
  57. // MEMO: entity ?? data ?? it's changed??
  58. $question = Question::find($params['entity']->parent_id);
  59. // find User by Email
  60. $user = User::findByEmail(Session::read('user.email'))->to('array');
  61. $params['entity']->user_id = $user['_id'];
  62. // set others
  63. $params['entity']->created = date('Y-m-d H:i:s');
  64. $params['entity']->parent_id = $question->_id;
  65. $params['entity']->_id = new \MongoID();
  66. $params['entity']->like = 0;
  67. $data = ($question->answers)? $question->answers->to('array'): array();
  68. // if the answers exist, added to array of answers with merge.
  69. $entity = $params['entity']->to('array');
  70. if (count($data)) {
  71. $answers = array_merge($data, array($entity['_id'] => $entity));
  72. } else {
  73. $answers = array($entity['_id'] => $entity);
  74. }
  75. $question->answers = $answers;
  76. $question->save();
  77. return true;
  78. });
  79. // Validators
  80. Validator::add('notEmptyItems',function ($value, $format, $options) {
  81. if (count($value) == 0) {
  82. $options['message'] = "hoge";
  83. return false;
  84. }
  85. return true;
  86. });
  87. Validator::add('inFrameworkItemList', function ($value, $format, $options){
  88. $flg = true;
  89. foreach ((array)$value as $v) {
  90. /**
  91. * MEMO:: If call default validator rules, you can call rules like "is"+ "rule_name", "isInList" below.
  92. *
  93. * @author maeda
  94. */
  95. if (!Validator::isInList($v, null, array('list' => Answer::getFrameworks()))) {
  96. $flg = false;
  97. break;
  98. }
  99. }
  100. return $flg;
  101. });
  102. Validator::add('isValidUrl', function ($value, $format, $options){
  103. //return true;
  104. return Validator::isUrl($value);
  105. });
  106. Validator::add('isParentId', function ($value, $format, $options) {
  107. $question = Question::find($value);
  108. // If editing the post, skip the current psot
  109. return ($question)? true: false;
  110. });
  111. }
  112. /**
  113. * get defined values of framework
  114. *
  115. * @return array: defined values of framework
  116. */
  117. static public function getFrameworks($pattern=null) {
  118. $fws = static::$_frameworks;
  119. if ($pattern) {
  120. foreach ($fws as $k => $v) {
  121. if (!preg_match("#". $pattern ."#i", $v)) {
  122. unset($fws[$k]);
  123. }
  124. }
  125. }
  126. return $fws;
  127. }
  128. }