1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Comments;
13
14 use ICanBoogie\ActiveRecord;
15 use ICanBoogie\ActiveRecord\Query;
16 use ICanBoogie\DateTime;
17
18 19 20
21 class Model extends ActiveRecord\Model
22 {
23 24 25 26 27 28 29
30 public function save(array $properties, $key=null, array $options=array())
31 {
32 if (!$key && empty($properties[Comment::CREATED_AT]))
33 {
34 $properties[Comment::CREATED_AT] = DateTime::now();
35 }
36
37 $properties += array
38 (
39 Comment::STATUS => 'pending',
40 Comment::NOTIFY => 'no',
41 Comment::UPDATED_AT => DateTime::now()
42 );
43
44 if (!in_array($properties[Comment::NOTIFY], array('no', 'yes', 'author', 'done')))
45 {
46 throw new \InvalidArgumentException(\ICanBoogie\format
47 (
48 'Invalid value for property %property: %value', array
49 (
50 '%property' => Comment::NOTIFY,
51 '%value' => $properties[Comment::NOTIFY]
52 )
53 ));
54 }
55
56 return parent::save($properties, $key, $options);
57 }
58
59 60 61 62 63 64 65
66 protected function scope_approved(Query $query)
67 {
68 return $query->filter_by_status(Comment::STATUS_APPROVED);
69 }
70
71 72 73 74 75 76 77
78 protected function scope_pending(Query $query)
79 {
80 return $query->filter_by_status(Comment::STATUS_PENDING);
81 }
82
83 84 85 86 87 88 89
90 protected function scope_spam(Query $query, $approved=true)
91 {
92 return $query->filter_by_status(Comment::STATUS_SPAM);
93 }
94
95 96 97 98 99 100 101 102 103
104 public function including_node(array $records)
105 {
106 $keys = array();
107
108 foreach ($records as $record)
109 {
110 $keys[$record->nid] = $record;
111 }
112
113 $nodes = ActiveRecord\get_model('nodes')->find_using_constructor(array_keys($keys));
114
115 foreach ($nodes as $key => $node)
116 {
117 $keys[$key]->node = $node;
118 }
119
120 return $records;
121 }
122 }