moved code up one level to eliminate the docs subdirectory
[acontent.git] / include / classes / DAO / PrimaryResourcesDAO.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 "primary_resources" 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 PrimaryResourcesDAO extends DAO {
25
26         /**
27         * Insert a new row
28         * @access  public
29         * @param   content_id, file_name, language_code
30         * @return  table rows
31         * @author  Cindy Qi Li
32         */
33         public function Create($content_id, $file_name, $lang)
34         {
35                 global $addslashes;
36                 
37                 $content_id = intval($content_id);
38                 $file_name = $addslashes(convertAmp($file_name));
39                 $lang = $addslashes($lang);
40
41                 $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources 
42                            SET content_id=$content_id, 
43                                resource='$file_name', 
44                                language_code='$lang'";
45             
46                 return $this->execute($sql);
47         }
48         
49         /**
50         * Delete rows by content_id
51         * @access  public
52         * @param   content_id
53         * @return  true or false
54         * @author  Cindy Qi Li
55         */
56         public function Delete($cid)
57         {
58                 $pri_resource_ids = array();
59                 
60                 // Get all primary resources ID out that're associated with this content
61                 $rows = $this->getByContent($cid);
62                 
63                 if (is_array($rows)){
64                         foreach ($rows as $row) $pri_resource_ids[] = $row['primary_resource_id'];
65                 }
66                 
67                 if (!empty($pri_resource_ids)){
68                         $glued_pri_ids = implode(",", $pri_resource_ids);
69
70                         // Delete all secondary a4a
71                         $sql = 'DELETE c, d FROM '.TABLE_PREFIX.'secondary_resources c 
72                              LEFT JOIN '.TABLE_PREFIX.'secondary_resources_types d 
73                                     ON c.secondary_resource_id=d.secondary_resource_id 
74                                  WHERE primary_resource_id IN ('.$glued_pri_ids.')';
75
76                         // If successful, remove all primary resources
77                         if ($this->execute($sql)){
78                                 $sql = 'DELETE a, b FROM '.TABLE_PREFIX.'primary_resources a 
79                                      LEFT JOIN '.TABLE_PREFIX.'primary_resources_types b 
80                                             ON a.primary_resource_id=b.primary_resource_id 
81                                          WHERE content_id='.$cid;
82                                 return $this->execute($sql);
83                         }
84                 }
85                 return true;
86         }
87         
88         /**
89         * Delete rows that primary resource name is the given $resourceName
90         * @access  public
91         * @param   $resourceName: primary resource name
92         * @return  true or false
93         * @author  Cindy Qi Li
94         */
95         function DeleteByResourceName($resourceName)
96         {
97                 $sql = "DELETE FROM ".TABLE_PREFIX."primary_resources
98                          WHERE resource = '".$resourceName."'";
99                 return $this->execute($sql);
100         }
101         
102     /**
103      * Delete rows by using resource id
104      * @access  public
105      * @param   $resourceID: primary resource ID
106      * @return  true or false
107      * @author  Harris Wong
108      * @date    Oct 6, 2010
109      */
110     function DeleteByResourceID($resourceID){
111         // Delete all secondary a4a
112         $sql = 'DELETE c, d FROM '.TABLE_PREFIX.'secondary_resources c LEFT JOIN '.TABLE_PREFIX."secondary_resources_types d ON c.secondary_resource_id=d.secondary_resource_id WHERE primary_resource_id=$resourceID";
113         $result = $this->execute($sql);
114         
115         // If successful, remove all primary resources
116         if ($result){
117             $sql = 'DELETE a, b FROM '.TABLE_PREFIX.'primary_resources a LEFT JOIN '.TABLE_PREFIX."primary_resources_types b ON a.primary_resource_id=b.primary_resource_id WHERE a.primary_resource_id=$resourceID";
118             return $this->execute($sql);
119         }
120     }
121
122         /**
123         * Return rows by content_id
124         * @access  public
125         * @param   cid: content_id
126         * @return  table rows
127         * @author  Cindy Qi Li
128         */
129         public function getByContent($cid)
130         {
131             $sql = 'SELECT * FROM '.TABLE_PREFIX.'primary_resources WHERE content_id='.$cid.' ORDER BY primary_resource_id';;
132             return $this->execute($sql);
133         }
134
135     /**
136      * Return rows by primary resource name
137      * @access  public
138      * @param   $cid: the content id
139      * @param   $lang: the language code. 
140      * @param   $resourceName: primary resource name
141      * @return  table rows
142      * @author  Harris Wong
143      * @date    Oct 6, 2010
144      */
145     public function getByResourceName($cid, $lang, $resource_name){
146                 $sql = "SELECT * FROM ".TABLE_PREFIX."primary_resources 
147                         WHERE content_id=".$cid."
148                           AND language_code = '".$lang."'
149                           AND resource='".$resource_name."'";
150                 return $this->execute($sql);
151     }
152 }
153 ?>