3 * class representing the posts table
5 class tdoPosts
extends tdoAbstract
{
6 protected $fields = array (
16 public function __construct(&$db) {
17 $this->name
= 'posts';
19 parent
::__construct ($db);
20 // $this->setIndex($this->fields['post_id']);
21 // $this->buildTable($tableDefinition);
24 // public function __set ($key, $value) {
25 // if (stristr($key, 'body'))
26 // $this->clean($value);
28 // return parent::__set($key, $value);
31 public function getBody (){
32 $this->set_post_body(stripslashes($this->post_body
->getValue()));
33 return $this->post_body
;
36 public function clean (&$var) {
37 // here we might need to strip certain tags to avoid javascript injection
38 // then again... since we strip with smarty the posts the <script> something </script> will become
39 // <script type="text/javascript"> //<![CDATA[ something; //]]> </script> ... so it's commented out :D = Brilliant!! <- sarcasm
40 $var = stripslashes ($var);
41 if (extension_loaded('tidy')){
43 'output-xhtml' => true,
44 'enclose-text' => false,
45 'show-body-only'=> true,
47 'fix-backslash' => false,
49 'merge-spans' => true,
50 'break-before-br'=> true,
54 $var = tidy_repair_string ($var, $config, "utf8");
58 function isPrivate () {
59 if ($this->post_flags
& P_PRIVATE
== P_PRIVATE
)
65 public function getPostsCategories (){
66 $this->buildInherentWheres();
67 $cat = new tdoCategories($this->db
);
68 $cp = new tdoCategoriesPosts($this->db
);
70 $cat->set_FieldModifier('GROUP_CONCAT(DISTINCT(%s))', $cat->category_name
); // mySql
72 $cp->joinWith('INNER', $cp->category_id
, $cat, $cat->category_id
);
74 $this->joinWith ('INNER', $this->post_id
, $cp, $cp->post_id
);
76 $this->addGroup($this->post_id
);
78 return $this->getArray();
82 * cleaning the fields' contents before updating
83 * TODO: this can be equaly achieved, with less hassle, by modifying the $this->escape() method :D
87 public function update ($id = null) {
88 $this->clean ($this->post_title
,false);
89 $this->clean ($this->post_body
);
91 return parent
::update ($id);
95 * cleaning the fields' contents before inserting into the db
97 public function insert () {
98 $this->clean ($this->post_title
,false);
99 $this->clean ($this->post_body
);
101 return parent
::insert ();
104 private function buildPosts ($userId = null) {
105 $this->post_active
= 'Y';
107 $this->addGroup ($this->post_id
);
108 $this->addOrder ($this->post_time
,false);
110 $cat = new tdoCategories ($this->db
);
111 $cp = new tdoCategoriesPosts ($this->db
);
113 $user = new tdoUsers ($this->db
);
114 // I don't know if group concat is ANSI SQL compliant
115 $cat->set_FieldModifier('GROUP_CONCAT(DISTINCT(%s) SEPARATOR \', \')', $cat->category_name
);
117 $w1 = new tdoAbstractClause ($this->post_private
, '=', 'N');
118 $w2 = new tdoAbstractClause ($cat->category_private
, '=', 'N');
120 $w3 = new tdoAbstractClause ($w1, 'AND', $w2);
122 if (!empty ($userId)) {
123 $w4 = new tdoAbstractClause ($this->post_author
, '=', $userId);
124 $this->addWhere ($w3, 'OR', $w4);
126 $this->addWhere ($w2,'AND',$w1);
129 $cp->joinWith ('INNER', $cp->category_id
, $cat, $cat->category_id
);
131 $this->joinWith ('INNER', $this->post_id
, $cp, $cp->post_id
);
132 $this->joinWith ('INNER', $this->post_author
, $user, $user->user_id
);
134 $this->addGroup ($this->post_id
);
137 public function getAllPosts ($userId = null) {
138 $this->buildPosts ($userId);
140 return $this->getArray ();
143 public function getPost ($postId, $userId = null) {
144 $this->buildPosts ($userId);
146 // these is the only modification from getAllPosts
147 if (!empty($postId)) {
148 $this->post_id
= $postId;
151 return /*/end/**/ ($this->getArray(1));
154 public function test () {
157 $this->db
->query ( $this->buildSql (1));
159 return $this->db
->getObjects ();
163 * Hard coded FULLTEXT search on post_title and post_body
164 * Probably it won't be very hard to abstract it:
165 * 1/ We already have FULLTEXT as flag on tdoAbstractField, I'll need only
166 * the MATCH () AGAINST () stuff, plus IN BOOLEAN MODE or WITH QUERY EXPANSION
167 * 2/ I have to look in what to do for different type of tables
169 * @param string $searchText
171 public function searchPosts ($searchText, $userId = null) {
173 $this->buildPosts ($userId);
174 $s = new tdoAbstractClause (' MATCH (post_title, post_body) AGAINST (' . $this->escape($searchText) . ')');
176 $this->addWhere ($s);
177 // $sql = 'SELECT ' . $this->outputFieldList() . ' FROM ' . $this->name . ' AS ' . $this->alias . ' WHERE '.
178 // 'MATCH (post_title, post_body) AGAINST (' . $this->escape($searchText) . ') AND post_active = "Y"';
180 // $this->db->query ($sql);
182 return $this->getArray ();