noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_todo_list.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023  * \brief the todo list is managed by this class
00024  */
00025 
00026 require_once('function_javascript.php');
00027 
00028 /*!\brief
00029  * This class manages the table todo_list
00030  *
00031  *
00032  * Data Member :
00033  * - $cn database connx
00034  * - $variable
00035  *    - id (todo_list.tl_id)
00036  *    - date (todo_list.tl_Date)
00037  *    - title (todo_list.title)
00038  *    - desc (todo_list.tl_desc)
00039  *    - owner (todo_list.use_id)
00040  *
00041  */
00042 class Todo_List
00043 {
00044 
00045     private static $variable=array(
00046                                  "id"=>"tl_id",
00047                                  "date"=>"tl_date",
00048                                  "title"=>"tl_title",
00049                                  "desc"=>"tl_desc",
00050                                  "owner"=>"use_login");
00051     private $cn;
00052     private  $tl_id,$tl_date,$tl_title,$use_login;
00053 
00054     function __construct ($p_init)
00055     {
00056         $this->cn=$p_init;
00057         $this->tl_id=0;
00058         $this->tl_desc="";
00059         $this->use_login=$_SESSION['g_user'];
00060 
00061     }
00062     public function get_parameter($p_string)
00063     {
00064         if ( array_key_exists($p_string,self::$variable) )
00065         {
00066             $idx=self::$variable[$p_string];
00067             return $this->$idx;
00068         }
00069         else
00070             exit (__FILE__.":".__LINE__.'Erreur attribut inexistant');
00071     }
00072     public function check($p_idx,&$p_value)
00073     {
00074         if ( strcmp ($p_idx, 'tl_id') == 0 )
00075         {
00076             if ( strlen($p_value) > 6 || isNumber ($p_value) == false) return false;
00077         }
00078         if ( strcmp ($p_idx, 'tl_date') == 0 )
00079         {
00080             if ( strlen(trim($p_value)) ==0 ||strlen($p_value) > 12 || isDate ($p_value) == false) return false;
00081         }
00082         if ( strcmp ($p_idx, 'tl_title') == 0 )
00083         {
00084             $p_value=mb_substr($p_value,0,120) ;
00085             return true;
00086         }
00087         if ( strcmp ($p_idx, 'tl_desc') == 0 )
00088         {
00089             $p_value=mb_substr($p_value,0,400) ;
00090             return true;
00091         }
00092         return true;
00093     }
00094     public function set_parameter($p_string,$p_value)
00095     {
00096         if ( array_key_exists($p_string,self::$variable) )
00097         {
00098             $idx=self::$variable[$p_string];
00099             if ($this->check($idx,$p_value) == true )      $this->$idx=$p_value;
00100         }
00101         else
00102             exit (__FILE__.":".__LINE__.'Erreur attribut inexistant');
00103 
00104 
00105     }
00106     public function get_info()
00107     {
00108         return var_export(self::$variable,true);
00109     }
00110     public function verify()
00111     {
00112         if ( isDate($this->tl_date) == false )
00113         {
00114                         $this->tl_date=date('d.m.Y');
00115         }
00116         return 0;
00117     }
00118     public function save()
00119     {
00120         if (  $this->get_parameter("id") == 0 )
00121             $this->insert();
00122         else
00123             $this->update();
00124     }
00125 
00126     public function insert()
00127     {
00128         if ( $this->verify() != 0 ) return;
00129         if (trim($this->tl_title)=='')
00130             $this->tl_title=mb_substr(trim($this->tl_desc),0,30);
00131 
00132         if (trim($this->tl_title)=='')
00133         {
00134             alert('La note est vide');
00135             return;
00136         }
00137 
00138         /*  limit the title to 35 char */
00139         $this->tl_title=mb_substr(trim($this->tl_title),0,30);
00140 
00141         $sql="insert into todo_list (tl_date,tl_title,tl_desc,use_login) ".
00142              " values (to_date($1,'DD.MM.YYYY'),$2,$3,$4)  returning tl_id";
00143         $res=$this->cn->exec_sql(
00144                  $sql,
00145                  array($this->tl_date,
00146                        $this->tl_title,
00147                        $this->tl_desc,
00148                        $this->use_login)
00149              );
00150         $this->tl_id=Database::fetch_result($res,0,0);
00151 
00152     }
00153 
00154     public function update()
00155     {
00156         if ( $this->verify() != 0 ) return;
00157 
00158         if (trim($this->tl_title)=='')
00159             $this->tl_title=mb_substr(trim($this->tl_desc),0,40);
00160 
00161         if (trim($this->tl_desc)=='')
00162         {
00163             alert('La note est vide');
00164             return;
00165         }
00166 
00167         /*  limit the title to 35 char */
00168         $this->tl_title=mb_substr(trim($this->tl_title),0,40);
00169 
00170         $sql="update todo_list set tl_title=$1,tl_date=to_date($2,'DD.MM.YYYY'),tl_desc=$3 ".
00171              " where tl_id = $4";
00172         $res=$this->cn->exec_sql(
00173                  $sql,
00174                  array($this->tl_title,
00175                        $this->tl_date,
00176                        $this->tl_desc,
00177                        $this->tl_id)
00178              );
00179 
00180     }
00181     /*!\brief load all the task
00182      *\return an array of the existing tasks of the current user
00183      */
00184     public function load_all()
00185     {
00186         $sql="select tl_id, tl_title,tl_desc,to_char( tl_date,'DD.MM.YYYY') as str_tl_date,tl_date
00187              from todo_list where use_login=$1".
00188              " order by tl_date::date asc";
00189         $res=$this->cn->exec_sql(
00190                  $sql,
00191                  array($this->use_login));
00192         $array=Database::fetch_all($res);
00193         return $array;
00194     }
00195     public function load()
00196     {
00197 
00198         $sql="select tl_id,tl_title,tl_desc,to_char( tl_date,'DD.MM.YYYY') as tl_date
00199              from todo_list where tl_id=$1 and use_login=$2";
00200 
00201         $res=$this->cn->exec_sql(
00202                  $sql,
00203                  array($this->tl_id,$_SESSION['g_user'])
00204              );
00205 
00206         if ( Database::num_row($res) == 0 ) return;
00207         $row=Database::fetch_array($res,0);
00208         foreach ($row as $idx=>$value)
00209         {
00210             $this->$idx=$value;
00211         }
00212 
00213     }
00214     public function delete()
00215     {
00216         $sql="delete from todo_list where tl_id=$1 and use_login=$2";
00217         $res=$this->cn->exec_sql($sql,array($this->tl_id,$_SESSION['g_user']));
00218 
00219     }
00220     /**
00221      *@brief transform into xml
00222      */
00223     public function toXML()
00224     {
00225         $id='<tl_id>'.$this->tl_id.'</tl_id>';
00226         $title='<tl_title>'.escape_xml($this->tl_title).'</tl_title>';
00227         $desc='<tl_desc>'.escape_xml($this->tl_desc).'</tl_desc>';
00228         $date='<tl_date>'.$this->tl_date.'</tl_date>';
00229         $ret='<data>'.$id.$title.$desc.$date.'</data>';
00230         return $ret;
00231     }
00232     /*!\brief static testing function
00233      */
00234     static function test_me()
00235     {
00236         $cn=new Database(dossier::id());
00237         $r=new Todo_List($cn);
00238         $r->set_parameter('title','test');
00239         $r->use_login='phpcompta';
00240         $r->set_parameter('date','02.03.2008');
00241         $r->save();
00242         $r->set_parameter('id',3);
00243         $r->load();
00244         print_r($r);
00245         $r->set_parameter('title','Test UPDATE');
00246         $r->save();
00247         print_r($r);
00248         $r->set_parameter('id',1);
00249         $r->delete();
00250     }
00251 
00252 }
00253 
00254 
 All Data Structures Namespaces Files Functions Variables Enumerations