noalyss Version-9
forecast.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21
22/*!\file
23 * \brief manage the table forecast
24 */
25/*!
26 * \brief manage the table forecast
27 */
29{
30 private static $variable=array ("id"=>"f_id",
31 "name"=>"f_name","start_date"=>"f_start_date","end_date"=>"f_end_date"
32 );
33 private $cn;
34 /**
35 * @brief constructor
36 * @param $p_init Database object
37 */
38 function __construct ($p_init,$p_id=0)
39 {
40 $this->cn=$p_init;
41 $this->f_id=$p_id;
42 }
43 public function get_parameter($p_string)
44 {
45 if ( array_key_exists($p_string,self::$variable) )
46 {
47 $idx=self::$variable[$p_string];
48 return $this->$idx;
49 }
50 else
51 throw new Exception("Attribut inexistant $p_string");
52 }
53 public function set_parameter($p_string,$p_value)
54 {
55 if ( array_key_exists($p_string,self::$variable) )
56 {
57 $idx=self::$variable[$p_string];
58 $this->$idx=$p_value;
59 }
60 else
61 throw new Exception("Attribut inexistant $p_string");
62 }
63 public function get_info()
64 {
65 return var_export(self::$variable,true);
66 }
67
68 public function verify()
69 {
70 // Verify that the elt we want to add is correct
71 // the f_name must be unique (case insensitive)
72 if ( noalyss_strlentrim($this->f_name)==0) throw new Exception(_('Le nom ne peut pas ĂȘtre vide'));
73
74 return 0;
75 }
76 public function save()
77 {
78 /* please adapt */
79 if ( $this->get_parameter("id") == 0 )
80 $this->insert();
81 else
82 $this->update();
83 }
84
85 public function insert()
86 {
87 if ( $this->verify() != 0 ) return;
88 $sql="insert into forecast (f_name,f_start_date,f_end_date) ".
89 " values ($1,$2,$3) returning f_id";
90 $res=$this->cn->exec_sql(
91 $sql,
92 array($this->f_name,$this->f_start_date,$this->f_end_date)
93 );
94 $this->f_id=Database::fetch_result($res,0,0);
95 }
96
97 /**
98 *@brief update the forecast table
99 */
100 public function update()
101 {
102 if ( $this->verify() != 0 ) return;
103
104 $sql="update forecast set f_name=$1,f_start_date=$2,f_end_date=$3 ".
105 " where f_id = $4";
106 $res=$this->cn->exec_sql(
107 $sql,
108 array($this->f_name,$this->f_start_date,$this->f_end_date, $this->f_id)
109 );
110
111 }
112 /**
113 *@brief load all the existing forecast
114 *@param $p_cn is an Database object
115 *@return array of f_id and f_name
116 */
117 public static function load_all($p_cn)
118 {
119 $sql="select f_id, f_name,f_start_date,f_end_date from forecast order by 2 desc";
120 $ret=$p_cn->get_array($sql);
121 return $ret;
122 }
123 /**
124 * @brief load from db
125 * @return boolean true if found else false
126 */
127 public function load():bool
128 {
129 $sql="select f_id, f_name,f_start_date ,f_end_date from forecast where f_id=$1";
130 $res=$this->cn->exec_sql(
131 $sql,
132 array($this->f_id)
133 );
134 if ( Database::num_row($res) == 0 ) return false;
136 $a_index=array_values(self::$variable);
137 foreach ( $a_index as $idx)
138 {
139 $this->$idx=$row[$idx];
140 }
141 return true;
142 }
143 public function delete()
144 {
145 $sql="delete from forecast where f_id=$1";
146 $res=$this->cn->exec_sql($sql,array($this->f_id));
147 }
148 public function object_clone()
149 {
150 $this->load();
151 /* save into the table forecast */
152 $sql="insert into forecast(f_name,f_start_date,f_end_date) select 'clone '||f_name,f_start_date,f_end_date from forecast where f_id=$1 returning f_id";
153 $new=$this->cn->get_value($sql,array($this->f_id));
154
155 /* save into forecast_cat */
156 $sql="insert into forecast_cat(fc_desc,f_id,fc_order) select fc_desc,$1,fc_order from forecast_cat where f_id=$2 returning fc_id" ;
157 $array=$this->cn->get_array($sql,array($new,$this->f_id));
158
159 $old=$this->cn->get_array("select fc_id from forecast_cat where f_id=$1",array($this->f_id));
160 /* save into forecast_item */
161 for ($i=0;$i<count($array);$i++)
162 {
163 $this->cn->exec_sql("insert into forecast_item (fi_text,fi_account,fi_order,fc_id,fi_amount,fi_pid) ".
164 " select fi_text,fi_account,fi_order,$1,fi_amount,fi_pid ".
165 " from forecast_item where fc_id=$2",array($array[$i]['fc_id'],$old[$i]['fc_id']));
166 }
167 }
168
169
170}
171?>
noalyss_strlentrim($p_string)
Definition: ac_common.php:1549
$idx
$input_from cn
Definition: balance.inc.php:66
static fetch_result($ret, $p_row=0, $p_col=0)
wrapper for the function pg_fetch_all
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
static num_row($ret)
wrapper for the function pg_num_rows
manage the table forecast
__construct($p_init, $p_id=0)
constructor
update()
update the forecast table
get_parameter($p_string)
static $variable
static load_all($p_cn)
load all the existing forecast
set_parameter($p_string, $p_value)
load()
load from db