/ulaform/lib/Action/Sql.php

https://github.com/ewandor/horde · PHP · 191 lines · 115 code · 18 blank · 58 comment · 9 complexity · d001d8d4c554baacaee358d1d40809c3 MD5 · raw file

  1. <?php
  2. /**
  3. * Ulaform_Action_Sql Class provides a Ulaform action driver to submit the
  4. * results of a form to database.
  5. *
  6. * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
  7. *
  8. * See the enclosed file COPYING for license information (GPL). If you
  9. * did not receive this file, see http://www.horde.org/licenses/gpl.
  10. *
  11. * @author Vilius Šumskas <vilius@lnk.lt>
  12. * @package Ulaform
  13. */
  14. class Ulaform_Action_Sql extends Ulaform_Action {
  15. /**
  16. * The database connection object.
  17. *
  18. * @var Horde_Db_Adapter
  19. */
  20. protected $_db;
  21. /**
  22. * Charset
  23. *
  24. * @var string
  25. */
  26. protected $_charset;
  27. /**
  28. * Construct a new SQL storage object.
  29. *
  30. * @param array $params The connection parameters
  31. *
  32. * @throws InvalidArguementException
  33. */
  34. public function __construct($params = array())
  35. {
  36. if (empty($params['db'])) {
  37. throw new InvalidArgumentException('Missing required connection parameter(s).');
  38. }
  39. $this->_db = $params['db'];
  40. $this->_charset = $params['charset'];
  41. }
  42. /**
  43. * Actually carry out the action.
  44. *
  45. * @return boolean True on success.
  46. * @throws Ulaform_Exception
  47. */
  48. public function doAction($form_params, $form_data, $fields)
  49. {
  50. /* Check if table exists. */
  51. if (!in_array($form_params['table'], $this->_db->tables())) {
  52. try {
  53. $this->_createDataTable($form_params, $fields);
  54. } catch (Horde_Db_Exception $e) {
  55. throw new Ulaform_Exception($e->getMessage());
  56. }
  57. }
  58. /* Submit data to database. */
  59. $columns = array();
  60. $values = array();
  61. foreach ($fields as $field) {
  62. switch ($field['field_type']) {
  63. case 'file':
  64. case 'image':
  65. if (count($form_data[$field['field_name']])) {
  66. $data = file_get_contents($form_data[$field['field_name']]['file']);
  67. if (Horde_String::lower($this->_db->adapterName()) == 'mssql' ||
  68. Horde_String::lower($this->_db->adapterName()) == 'pgsql') {
  69. $data = bin2hex($data);
  70. }
  71. $columns[] = $field['field_name'];
  72. $values[] = $data;
  73. }
  74. break;
  75. case 'set':
  76. $columns[] = $field['field_name'];
  77. $values[] = implode(', ', $form_data[$field['field_name']]);
  78. break;
  79. default:
  80. $data = $form_data[$field['field_name']];
  81. $columns[] = $field['field_name'];
  82. $values[] = Horde_String::convertCharset($data, 'UTF-8', $this->_charset);
  83. break;
  84. }
  85. }
  86. $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
  87. $form_params['table'],
  88. implode(', ', $columns),
  89. str_repeat('?, ', count($values) - 1) . '?');
  90. try {
  91. $this->_db->insert($sql, $values);
  92. } catch (Horde_Db_Exception $e) {
  93. throw new Ulaform_Exception($e->getMessage());
  94. }
  95. return true;
  96. }
  97. /**
  98. * Identifies this action driver and returns a brief description, used by
  99. * admin when configuring an action for a form and set up using Horde_Form.
  100. *
  101. * @return array Array of required parameters.
  102. */
  103. static public function getInfo()
  104. {
  105. $info['name'] = _("SQL");
  106. $info['desc'] = _("This driver allows to insertion of form results into a database.");
  107. return $info;
  108. }
  109. /**
  110. * Returns the required parameters for this action driver, used by admin
  111. * when configuring an action for a form and set up using Horde_Form.
  112. *
  113. * @return array Array of required parameters.
  114. */
  115. static public function getParams()
  116. {
  117. $params = array();
  118. $params['table'] = array('label' => _("Table"), 'type' => 'text');
  119. return $params;
  120. }
  121. /**
  122. * Create table for submiting data.
  123. *
  124. * @return boolean True on success.
  125. * @throws Ulaform_Exception
  126. */
  127. protected function _createDataTable($form_params, $fields)
  128. {
  129. /* Generate SQL query. */
  130. $columns = array();
  131. foreach ($fields as $field) {
  132. switch ($field['field_type']) {
  133. case 'file':
  134. case 'image':
  135. // TODO: Use Horde_SQL
  136. switch (Horde_String::lower($this->_db->adapterName())) {
  137. case 'pgsql':
  138. $columns[] = $field['field_name'] . ' TEXT';
  139. break;
  140. case 'mysql':
  141. case 'mysqli':
  142. $columns[] = $field['field_name'] . ' MEDIUMBLOB';
  143. break;
  144. default:
  145. $columns[] = $field['field_name'] . ' BLOB';
  146. break;
  147. }
  148. break;
  149. case 'address':
  150. case 'countedtext':
  151. case 'description':
  152. case 'html':
  153. case 'longtext':
  154. case 'set':
  155. $columns[] = $field['field_name'] . ' TEXT';
  156. break;
  157. default:
  158. $columns[] = $field['field_name'] . ' VARCHAR(255)';
  159. break;
  160. }
  161. }
  162. $sql = sprintf('CREATE TABLE %s (%s)',
  163. $form_params['table'],
  164. implode(', ', $columns));
  165. /* Create table. */
  166. try {
  167. $this->_db->execute($sql);
  168. } catch (Horde_Db_Exception $e) {
  169. throw new Ulaform_Exception($e);
  170. }
  171. return true;
  172. }
  173. }