- Added Validators
[activemongo.git] / samples / blog / Post.php
blob93eb605e1b5e69f5d30a6f2165609f7bf9a29561
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 ActiveMongo |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
18 | |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
39 class PostModel extends ActiveMongo
41 static $validates_presence_of = array(
42 'title',
43 'uri',
44 'author',
47 static $validates_length_of = array(
48 array('title', 'min' => 5, 'max' => 30),
51 public $title;
52 public $uri;
53 public $author;
54 public $comment;
55 public $ts;
57 const LIMIT_PER_PAGE=20;
59 /**
60 * Return this collection name.
62 * @return string
64 function getCollectionName()
66 return 'post';
69 /**
70 * Display the blog posts entries in the correct
71 * order, getting only the needed columns, and
72 * adding pagination.
74 * @return PostModel This document
76 function listing_page()
78 /* Get collection */
79 $collection = $this->_getCollection();
81 /* Deal with MongoDB directly */
82 $columns = array(
83 'title' => 1,
84 'uri' => 1,
85 'author_name' => 1,
86 'author_username' => 1,
87 'ts' => 1,
89 $cursor = $collection->find(array(), $columns);
90 $cursor->sort(array("ts" => -1))->limit(self::LIMIT_PER_PAGE);
92 /* Pagination */
93 if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0) {
94 $skip = (int)$_GET['page'] * self::LIMIT_PER_PAGE;
95 $cursor->skip($skip);
98 /* Pass our results to ActiveMongo */
99 $this->setCursor($cursor);
101 return $this;
104 function before_validate(&$obj)
106 $obj['ts'] = new MongoTimestamp();
111 * Author Filter.
113 * Check if we save the Author _id,
114 * or it throws an exception.
116 * @param mixed &$value Current value
117 * @param mixed $old_value Past value, used on Update
119 * @return bool
121 function author_filter(&$value, $old_value)
123 if (!$value instanceof MongoID) {
124 throw new Exception("Invalid MongoID");
126 return true;
130 * Update Author information in the Posts. This function
131 * is trigger when a new Post is created or the Author
132 * has updated his/her profile.
134 * @param MongoID $id Author ID
136 * @return void
138 function updateAuthorInfo(MongoID $id)
140 $author = new AuthorModel;
141 $author->find($id);
143 $document = array(
144 '$set' => array(
145 'author_name' => $author->name,
146 'author_username' => $author->username,
150 $filter = array(
151 'author' => $id,
154 $this->_getCollection()->update($filter, $document, array('multiple' => true));
156 return true;
160 * A new post must have its author information
161 * on it. (to avoid multiple requests at render time).
163 * @return void
165 function after_create()
167 $this->updateAuthorInfo($this->author);
171 * Simple abstraction to add a new comment,
172 * to avoid typo errors at coding time.
174 function add_comment($user, $email, $comment)
176 $this->comment[] = array(
177 "user" => $user,
178 "email" => $email,
179 "comment" => $comment,
181 return true;
184 function setup()
186 $this->addIndex(array('uri' => 1), array('unique'=> 1));
187 $this->addIndex(array('author' => 1));
188 $this->addIndex(array('ts' => -1));