AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / include / classes / DAO / ForumsDAO.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14  * DAO for "forums" table
15  * @access      public
16  * @author      Cindy Qi Li
17  * @package     DAO
18  */
19
20 if (!defined('TR_INCLUDE_PATH')) exit;
21
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
23
24 class ForumsDAO extends DAO {
25
26         /**
27          * Create new forums
28          * @access  public
29          * @param   
30          * @return  user id, if successful
31          *          false and add error into global var $msg, if unsuccessful
32          * @author  Cindy Qi Li
33          */
34         public function Create($title, $description)
35         {
36                 global $addslashes;
37
38                 $title = $addslashes(trim($title));
39                 $decsription = $addslashes(trim($description));
40                 
41                 if ($this->isFieldsValid($title))
42                 {
43                         /* insert into the db */
44                         $sql = "INSERT INTO ".TABLE_PREFIX."forums
45                                       (title, description, created_date)
46                                VALUES ('".$title."',
47                                        '".$decsription."',
48                                        now())";
49
50                         if (!$this->execute($sql))
51                         {
52                                 $msg->addError('DB_NOT_UPDATED');
53                                 return false;
54                         }
55                         else
56                         {
57                                 return mysql_insert_id();
58                         }
59                 }
60                 else
61                 {
62                         return false;
63                 }
64         }
65
66         /**
67          * Delete a forum
68          * @access  public
69          * @param   forum ID
70          * @return  true, if successful
71          *          false and add error into global var $msg, if unsuccessful
72          * @author  Cindy Qi Li
73          */
74         public function Delete($forumID)
75         {
76                 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
77                 require_once(TR_INCLUDE_PATH.'classes/DAO/ContentForumsAssocDAO.class.php');
78                 $contentForumsAssocDAO = new ContentForumsAssocDAO();
79                 
80                 // delete the forum and related data
81                 $contentForumsAssocDAO->DeleteByForumID($forumID);
82                 
83                 $sql = "DELETE FROM ".TABLE_PREFIX."forums
84                          WHERE forum_id = ".$forumID;
85                 $this->execute($sql);
86         }
87
88         /**
89          * Return by given forum id
90          * @access  public
91          * @param   forum id
92          * @return  one row
93          * @author  Cindy Qi Li
94          */
95         public function get($forumID)
96         {
97                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'forums WHERE forum_id='.$forumID;
98                 if ($rows = $this->execute($sql))
99                 {
100                         return $rows[0];
101                 }
102                 else return false;
103         }
104
105         /**
106          * Validate fields preparing for insert and update
107          * @access  private
108          * @param   $title
109          * @return  true    if update successfully
110          *          false   if update unsuccessful
111          * @author  Cindy Qi Li
112          */
113         private function isFieldsValid($title)
114         {
115                 global $msg;
116                 
117                 $missing_fields = array();
118                 
119                 if ($title == '')
120                 {
121                         $missing_fields[] = _AT('title');
122                 }
123                 
124                 if ($missing_fields)
125                 {
126                         $missing_fields = implode(', ', $missing_fields);
127                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
128                 }
129                 
130                 if (!$msg->containsErrors())
131                         return true;
132                 else
133                         return false;
134         }
135 }
136 ?>