/modules/thirdparty/patForms/patForms/Storage/DB.php

https://github.com/erictj/protean · PHP · 307 lines · 145 code · 38 blank · 124 comment · 17 complexity · 52b1fb08d074bdb767d41b8c782662d7 MD5 · raw file

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