source: trunk/patForms/Storage/DB.php @ 247

Revision 247, 5.5 KB checked in by schst, 8 years ago (diff)

Always use quoteSmart()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/**
3 * patForms storage DB
4 *
5 * $Id$
6 *
7 * @package     patForms
8 * @subpackage  Storage
9 * @author      Stephan Schmidt <schst@php-tools.net>
10 */
11
12/**
13 * needs PEAR::DB
14 */
15require_once 'DB.php';
16
17/**
18 * could not open storage container
19 */
20define( 'PATFORMS_STORAGE_ERROR_STORAGE_INVALID', 20000 );
21 
22/**
23 * patForms storage DB
24 *
25 * Stores form data in a database.
26 *
27 * @access      protected
28 * @package     patForms
29 * @subpackage  Storage
30 * @author      Stephan Schmidt <schst@php-tools.net>
31 * @license     LGPL, see license.txt for details
32 * @link        http://www.php-tools.net
33 * @todo        add error management
34 */
35class patForms_Storage_DB extends patForms_Storage
36{
37   /**
38    * datasource name
39    *
40    * @access   private
41    * @var      string
42    */
43    var $_dsn;
44
45   /**
46    * table name
47    *
48    * @access   private
49    * @var      string
50    */
51    var $_table;
52
53   /**
54    * instance of PEAR::DB
55    *
56    * @access   private
57    * @var      object
58    */
59    var $_db;
60
61   /**
62    * field map
63    *
64    * @access   private
65    * @var      string
66    */
67    var $_fieldMap  =   array();
68
69   /**
70    * set the dsn and table
71    *
72    * @access   public
73    * @param    string      datasource name
74    * @param    string      table
75    */
76    function setStorageLocation( $dsn, $table )
77    {
78        $this->_dsn     =   $dsn;
79        $this->_table   =   $table;
80    }
81
82   /**
83    * set the field map
84    *
85    * The field map is an associative array, that defines how
86    * the form elements (key) map to fields in the
87    * table (value)
88    *
89    * @access   public
90    * @param    array       field map
91    */
92    function setFieldMap($fieldMap)
93    {
94        $this->_fieldMap = $fieldMap;
95    }
96
97   /**
98    * get an entry
99    *
100    * This tries to find an entry in the storage container
101    * that matches the current data that has been set in the
102    * form and populates the form with the data of this
103    * entry
104    *
105    * @access   public
106    * @param    object patForms     patForms object that should be stored
107    * @return   boolean             true on success
108    */
109    function loadEntry(&$form)
110    {
111        $values  = $form->getValues();
112        $primary = $this->getPrimary($values);
113
114        // no primary key, storage will only add entries
115        if (empty($primary)) {
116            return array();
117        }
118       
119        /**
120         * entry does not exists
121         */
122        if (!$data = $this->_entryExists($primary)) {
123            return array();
124        }
125           
126        $values = $this->_unmapFields($data);
127
128        $form->setValues($values);
129        return true;
130    }
131
132   /**
133    * adds an entry to the storage
134    *
135    * The entry will be appended at the end of the file.
136    *
137    * @abstract
138    * @param    object patForms     patForms object that should be stored
139    * @return   boolean             true on success
140    */
141    function _addEntry( &$form )
142    {
143        $values = $form->getValues();
144       
145        $this->_prepareConnection();
146        $values = $this->_mapFields($values);
147       
148        $values = array_merge($values, $this->_staticValues);
149       
150        $fields = array_keys($values);
151        $values = array_map(array($this->_db, 'quoteSmart'), array_values($values));
152
153        $query  = sprintf('INSERT INTO %s (%s) VALUES (%s);', 
154                          $this->_table,
155                          implode(',', $fields),
156                          implode(',', $values)
157                      );
158       
159        $result = $this->_db->query($query);
160       
161        if (PEAR::isError($result)) {
162            return false;
163        }
164        return true;       
165    }
166
167   /**
168    * updates an entry in the storage
169    *
170    * Implement this in the concrete storage container.
171    *
172    * @abstract
173    * @param    object patForms     patForms object that should be stored
174    * @return   boolean             true on success
175    */
176    function _updateEntry( &$form, $primary )
177    {
178        $values = $form->getValues();
179       
180        $this->_prepareConnection();
181        $values  = $this->_mapFields($values);
182        $primary = $this->_mapFields($primary);
183       
184        $tmp = array();
185        foreach ($values as $key => $value) {
186            array_push( $tmp, $key.'='.$this->_db->quoteSmart( $value ) );
187        }
188
189        $ptmp = array();
190        foreach ($primary as $key => $value) {
191            array_push( $ptmp, $key.'='.$this->_db->quoteSmart( $value ) );
192        }
193
194        $query = 'UPDATE '.$this->_table.' SET '.implode( ', ', $tmp ).' WHERE '.implode( ' AND ', $ptmp );
195        $this->_db->query( $query );
196        return true;       
197    }
198
199   /**
200    * check, whether an entry exists
201    *
202    * @access   private
203    * @param    array
204    */
205    function _entryExists($primary)
206    {
207        $this->_prepareConnection();
208        $primary = $this->_mapFields($primary);
209
210        $tmp = array();
211        foreach ($primary as $key => $value) {
212            array_push($tmp, $key.'='.$this->_db->quoteSmart($value));
213        }
214
215        $query  = 'SELECT * FROM '.$this->_table.' WHERE '.implode( ' AND ', $tmp );
216        $result = $this->_db->getRow( $query, array(), DB_FETCHMODE_ASSOC );
217
218        if (empty($result)) {
219            return false;
220        }
221
222        return $result;
223    }
224
225   /**
226    * map the values to the correct fields
227    *
228    * @access   private
229    * @param    array       values
230    * @return   array       values mapped to the correct fields
231    */
232    function _mapFields( $values )
233    {
234        if (empty($this->_fieldMap)) {
235            return $values;
236        }
237
238        $fields =   array();
239        foreach ($this->_fieldMap as $el => $field) {
240            if (!isset($values[$el])) {
241                continue;
242            }
243
244            $fields[$field] = $values[$el];
245        }
246        return $fields; 
247    }
248
249   /**
250    * map the fields to the correct elements
251    *
252    * @access   private
253    * @param    array       values
254    * @return   array       values mapped to the correct fields
255    */
256    function _unmapFields( $values )
257    {
258        if (empty($this->_fieldMap)) {
259            return $values;
260        }
261
262        $fields = array();
263        foreach ($this->_fieldMap as $el => $field) {
264            if( !isset($values[$field])) {
265                continue;
266            }
267
268            $fields[$el] = $values[$field];
269        }
270        return $fields; 
271    }
272
273   /**
274    * prepare the DB connection
275    *
276    * @access   private
277    */
278    function _prepareConnection()
279    {
280        if ($this->_db != null) {
281            return true;
282        }
283       
284        if (is_object($this->_dsn)) {
285            $this->_db = &$this->_dsn;
286            return true;
287        }
288
289        $this->_db = &DB::connect($this->_dsn);
290        return true;
291    }
292}
293?>
Note: See TracBrowser for help on using the repository browser.