content.php

Go to the documentation of this file.
00001 <?php
00031 include_once("include/classes/renderer.php");
00032 
00038 class Content {
00043     const FTYPE_BOOL  = 1;      
00044     const FTYPE_INT   = 2;      
00045     const FTYPE_UINT  = 3;      
00046     const FTYPE_FLOAT = 4;      
00047     const FTYPE_DTIME = 5;      
00048     const FTYPE_STEXT = 6;      
00049     const FTYPE_LTEXT = 7;      
00050     const FTYPE_FTEXT = 8;      
00051     const FTYPE_IMAGE = 9;      
00053     private $id_area;           
00055     public $cnt = array();      
00071     function __construct($id, $istype = FALSE, $pid = 0, $sort = NULL, $page = NULL) {
00072         global $cfg, $auth, $session;
00073 
00074         $this->id_area = $cfg->getAreaId();
00075 
00076         /* build selection query ($where, $order) */
00077         if ($istype) {
00078             $idtype = $id;
00079             $where = " AND (id_parent = " . $pid . ")";
00080         } else {
00081             /* retrieve content type id */
00082             $res = mysql_query("SELECT id_type FROM content WHERE id = " . $id);
00083             if (mysql_num_rows($res) > 0) {
00084                 $line = mysql_fetch_assoc($res);
00085                 mysql_free_result($res);
00086                 $idtype = $line['id_type'];
00087                 $where = " AND (id = " . $id . ")";
00088             } else {
00089                 return;         // no content found
00090             }
00091         }
00092         /* add more filters */
00093         $where = " WHERE (id_area = " . $this->id_area . ")" . $where;
00094 
00095         /* sort mode */
00096         if (!is_null($sort)) {
00097 //          $sort = strtoupper($sort);
00098             switch (substr($sort, 0, 1)) {
00099                 case "A":   // alpha
00100                     /* search for the first alpha field name in _cntview_N (use $idtype) */
00101                     $order = "";
00102                     break;
00103 
00104                 case "P":   // position
00105                     $order = "position";
00106                     break;
00107 
00108                 case "T":   // time
00109                     $order = "date_created";
00110                     break;
00111 
00112                 default:
00113             }
00114             if ($order) {
00115                 $order = " ORDER BY " . $order . " " . (substr($sort, 1, 1) == "A" ? "ASC" : "DESC");
00116             }
00117         }
00118         // limit (pages): see renderer (TODO)
00119 
00120         /* extract content */
00121         $res = mysql_query("SELECT * FROM _cntview_" . $idtype . $where . $order);
00122         while ($line = mysql_fetch_assoc($res)) {
00123             if ($auth->isAdmin() || ($session->getValue("uid") == $line['id_owner']) ||     // admin or owner or...
00124                 ($line['visible'] AND !$line['expired'] /* AND READ_PERMS_ARE_OK */)) {     // ...user can read content
00125                 $this->cnt[count($this->cnt)] = $line;      // append content to array
00126                 }
00127         }
00128         mysql_free_result($res);
00129     }
00130 
00137 //  public function getValue($fieldname) {
00138 //      return $this->cnt[$fieldname];
00139 //  }
00140 
00153     private function getNextPos($id_area, $id_type, $id_parent) {
00154         $sql =  "SELECT MAX(position) AS maxpos FROM content " .
00155                 "WHERE (id_area = " . $id_area . ") " .
00156                 "AND (id_type = " . $id_type . ") " .
00157                 "AND (id_parent = " . $id_parent . ")";
00158         $res = mysql_query($sql);
00159         $line = mysql_fetch_assoc($res);
00160         mysql_free_result($res);
00161         if (is_null($line['maxpos'])) {
00162             return 1;   // first content ever defined in this record group
00163         } else {
00164             return $line['maxpos'] + 1;
00165         }
00166     }
00167 
00185     public static function add() {      // TODO test
00186         global $cfg, $session;
00187 
00188         $pid = isset($_POST['id_parent']) ? $_POST['id_parent'] : 0;
00189         $id_area = $cfg->getAreaId();
00190 
00191         $sql =  "INSERT INTO content (id_area, id_parent, id_type, id_owner, visible, position, readperm, writeperm, " .
00192                 "group_readperm, group_writeperm, counter, date_created, date_lastmodified, date_expiration) VALUES (" .
00193                 $id_area . ", " .                                               // id_area
00194                 $pid . ", " .                                                   // id_parent
00195                 $_POST['id_type'] . ", " .                                      // id_type
00196                 $session->getValue("uid") . ", " .                              // id_owner (user must be logged in)    FIXME sessvar ancora da definire!
00197                 ($_POST['visible'] ? "1" : "0") . ", " .                        // visible
00198                 $this->getNextPos($id_area, $_POST['id_type'], $pid) . ", " .   // position
00199                 $_POST['readperm'] . ", " .                                     // readperm
00200                 $_POST['writeperm'] . ", " .                                    // writeperm
00201                 "'" . $_POST['group_readperm'] . "', " .                        // group_readperm
00202                 "'" . $_POST['group_writeperm'] . "', " .                       // group_writeperm
00203                 "0, " .                                                         // counter
00204                 time() . ", 0, " .                                              // date_created, date_lastmodified
00205                 $_POST['date_expiration'] .                                     // date_expiration
00206                 ")";
00207         if (mysql_query($sql)) {
00208             return mysql_insert_id();
00209         } else {
00210             return FALSE;
00211         }
00212     }
00213 
00230     public static function update() {       // TODO test
00231         if (!isset($_POST['id'])) { return FALSE; }
00232         
00233         if (isset($_POST['visible'])) {
00234             $sql = "visible = '" . ($_POST['visible'] ? "1" : "0") . "'";
00235         }
00236 
00237         if (isset($_POST['readperm'])) {
00238             if ($sql) { $sql .= ", "; }
00239             $sql .= "readperm = " . $_POST['readperm'];
00240         }
00241 
00242         if (isset($_POST['writeperm'])) {
00243             if ($sql) { $sql .= ", "; }
00244             $sql .= "writeperm = " . $_POST['writeperm'];
00245         }
00246 
00247         if (isset($_POST['group_readperm'])) {
00248             if ($sql) { $sql .= ", "; }
00249             $sql .= "group_readperm = '" . $_POST['group_readperm'] . "'";
00250         }
00251 
00252         if (isset($_POST['group_writeperm'])) {
00253             if ($sql) { $sql .= ", "; }
00254             $sql .= "group_writeperm = '" . $_POST['group_writeperm'] . "'";
00255         }
00256 
00257         if (isset($_POST['date_expiration'])) {
00258             if ($sql) { $sql .= ", "; }
00259             $sql .= "date_expiration = " . $_POST['date_expiration'];
00260         }
00261         
00262         if ($sql) {
00263             $sql = "UPDATE content SET " . $sql . " WHERE id = " . $_POST['id'];
00264             return mysql_query($sql);
00265         } else {
00266             return FALSE;
00267         }
00268     }
00269 
00278     public static function delete($id) {
00279         global $cfg;
00280         $id_area = $cfg->getAreaId();
00281 
00282         /* recursively delete attached contents */
00283         $result = TRUE;
00284         $res = mysql_query( "SELECT id FROM content " .
00285                             "WHERE (id_area = " . $id_area . ") " .
00286                             "AND (id_parent = " . $id . ")");
00287 
00288         if ($res === FALSE) { return FALSE; }
00289         if (mysql_num_rows($res) == 0) { return FALSE; }
00290 
00291         while ($result && $line = mysql_fetch_assoc($res)) {
00292             $result = Content::delete($line['id']);
00293         }
00294         mysql_free_result($res);
00295 
00296         if ($result) {
00297             /* delete attached file: remove file first! - if we remove content first, we won't be able to find */
00298             /* the complete path to the file(s) because it depends on id_owner, in table `content` */
00299             /* FIXME BUG in caso di delete che non va a buon fine (se si usano le transazioni) si perderanno
00300                 irrimediabilmente i file. Pertanto occorre gestire al di fuori la cancellazione dei file (tabella
00301                 temporanea con nome sessid?) */
00302             include_once("include/classes/files.php");
00303             Files::delete($id);
00304     
00305             /* we need to rebuild position order after removing the record */
00306             /* get id_type, id_parent and position of the content to delete */
00307             $res = mysql_query( "SELECT id_type, id_parent, position FROM content " .
00308                                 "WHERE (id = " . $id . ") " .
00309                                 "AND (id_area = " . $id_area . ")");
00310 
00311             if ($res === FALSE) { return FALSE; }
00312             if (mysql_num_rows($res) == 0) { return FALSE; }
00313 
00314             $line = mysql_fetch_assoc($res);
00315             mysql_free_result($res);
00316     
00317             /* delete record from table `content` and all related tables */
00318             $result =   mysql_query("DELETE FROM content WHERE (id = " . $id . ") AND (id_area = " . $id_area . ")") &&
00319 
00320                         /* rebuild position order here */
00321                         mysql_query("UPDATE content SET position = position - 1 " .
00322                                     "WHERE (id_area = " . $id_area . ") " .
00323                                     "AND (id_type = " . $line['id_type'] . ") " .
00324                                     "AND (id_parent = " . $line['id_parent'] . ") " .
00325                                     "AND (position > " . $line['position'] . ")");
00326         }
00327 
00328         return $result;
00329     }
00330 
00341     public function getRenderedContent($id_renderer, $id_container = NULL) {
00342         if (count($this->cnt) == 0) { return; }
00343 
00344         $r = new Renderer($id_renderer, $id_container);
00345         for ($i = 0; $i < count($this->cnt); $i++) {
00346             $r->appendRendered($this->cnt[$i]);
00347         }
00348         return $r->getRendered();
00349     }
00350 }
00351 ?>

Generated on Fri Feb 22 11:19:48 2008 for CMSkey by  doxygen 1.5.3