| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * patForms storage base class - extend this to create your own storage containers. |
|---|
| 4 | * |
|---|
| 5 | * $Id$ |
|---|
| 6 | * |
|---|
| 7 | * @package patForms |
|---|
| 8 | * @subpackage Storage |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * could not open storage container |
|---|
| 13 | */ |
|---|
| 14 | define('PATFORMS_STORAGE_ERROR_STORAGE_INVALID', 'patForms::Storage::0001'); |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * patForms storage base class - extend this to create your own storage containers. |
|---|
| 18 | * |
|---|
| 19 | * @access protected |
|---|
| 20 | * @package patForms |
|---|
| 21 | * @subpackage Storage |
|---|
| 22 | * @author Stephan Schmidt <schst@php-tools.net> |
|---|
| 23 | * @license LGPL, see license.txt for details |
|---|
| 24 | * @link http://www.php-tools.net |
|---|
| 25 | */ |
|---|
| 26 | class patForms_Storage |
|---|
| 27 | { |
|---|
| 28 | /** |
|---|
| 29 | * fields that contains the primary key(s) for the data |
|---|
| 30 | * |
|---|
| 31 | * Defaults to 'id' |
|---|
| 32 | * |
|---|
| 33 | * @access private |
|---|
| 34 | * @var array |
|---|
| 35 | */ |
|---|
| 36 | var $_primaryFields = array('id'); |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * static values, that will be added to all entries |
|---|
| 40 | * |
|---|
| 41 | * @access private |
|---|
| 42 | * @var array |
|---|
| 43 | */ |
|---|
| 44 | var $_staticValues = array(); |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * set the primary key(s) for the storage container |
|---|
| 48 | * |
|---|
| 49 | * You may either pass a string or an array containing strings |
|---|
| 50 | * if your storage container has a composite primary key |
|---|
| 51 | * |
|---|
| 52 | * @access public |
|---|
| 53 | * @param mixed primary key field |
|---|
| 54 | */ |
|---|
| 55 | function setPrimaryField( $field ) |
|---|
| 56 | { |
|---|
| 57 | if (!is_array($field)) { |
|---|
| 58 | $field = array($field); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | $this->_primaryFields = $field; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * get the primary key for an entry |
|---|
| 66 | * |
|---|
| 67 | * This will return an associative array with all fields |
|---|
| 68 | * that form the primary key of the entry. |
|---|
| 69 | * |
|---|
| 70 | * If the form does not contain all values for the |
|---|
| 71 | * primary key, an empty array will be returned. |
|---|
| 72 | * |
|---|
| 73 | * @access protected |
|---|
| 74 | * @param array form values |
|---|
| 75 | * @return array primray values |
|---|
| 76 | */ |
|---|
| 77 | function getPrimary( $values ) |
|---|
| 78 | { |
|---|
| 79 | $primary = array(); |
|---|
| 80 | foreach ($this->_primaryFields as $p) { |
|---|
| 81 | if (isset($values[$p])) { |
|---|
| 82 | $primary[$p] = $values[$p]; |
|---|
| 83 | } else { |
|---|
| 84 | return array(); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | return $primary; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * set static values, that will be added to all entries |
|---|
| 92 | * |
|---|
| 93 | * @access public |
|---|
| 94 | * @param array |
|---|
| 95 | */ |
|---|
| 96 | function setStaticValues($staticValues) |
|---|
| 97 | { |
|---|
| 98 | $this->_staticValues = $staticValues; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * store an entry |
|---|
| 103 | * |
|---|
| 104 | * This method decides whether a new entry has to be |
|---|
| 105 | * created or an old entry has to be updated based |
|---|
| 106 | * on the values of the form data |
|---|
| 107 | * |
|---|
| 108 | * If the elements that store the primary key are empty, |
|---|
| 109 | * a new entry will be added, otherwise the entry will be updated |
|---|
| 110 | * |
|---|
| 111 | * @access public |
|---|
| 112 | * @param object patForms patForms object that should be stored |
|---|
| 113 | * @return boolean true on success |
|---|
| 114 | */ |
|---|
| 115 | function storeEntry( &$form ) |
|---|
| 116 | { |
|---|
| 117 | $values = $form->getValues(); |
|---|
| 118 | $primary = $this->getPrimary($values); |
|---|
| 119 | $new = false; |
|---|
| 120 | if (empty($primary)) { |
|---|
| 121 | $new = true; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | if(!$new) { |
|---|
| 125 | $exists = $this->_entryExists($primary); |
|---|
| 126 | if (patErrorManager::isError($exists)) { |
|---|
| 127 | return $exists; |
|---|
| 128 | } |
|---|
| 129 | if(!$exists) { |
|---|
| 130 | $new = true; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | if ($new) { |
|---|
| 135 | $result = $this->_addEntry($form); |
|---|
| 136 | } else { |
|---|
| 137 | $result = $this->_updateEntry($form, $primary); |
|---|
| 138 | } |
|---|
| 139 | return $result; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * validate the form |
|---|
| 144 | * |
|---|
| 145 | * @access public |
|---|
| 146 | * @param object $form The form |
|---|
| 147 | * @return True or false, the validation result |
|---|
| 148 | */ |
|---|
| 149 | function validateEntry(&$form) |
|---|
| 150 | { |
|---|
| 151 | return true; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * get an entry |
|---|
| 156 | * |
|---|
| 157 | * This tries to find an entry in the storage container |
|---|
| 158 | * that matches the current data that has been set in the |
|---|
| 159 | * form and populates the form with the data of this |
|---|
| 160 | * entry |
|---|
| 161 | * |
|---|
| 162 | * @access public |
|---|
| 163 | * @param object patForms patForms object that should be stored |
|---|
| 164 | * @return boolean true on success |
|---|
| 165 | */ |
|---|
| 166 | function loadEntry( &$form ) |
|---|
| 167 | { |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * adds an entry to the storage |
|---|
| 172 | * |
|---|
| 173 | * Implement this in the concrete storage container. |
|---|
| 174 | * |
|---|
| 175 | * @abstract |
|---|
| 176 | * @param object patForms patForms object that should be stored |
|---|
| 177 | * @return boolean true on success |
|---|
| 178 | */ |
|---|
| 179 | function _addEntry( &$form ) |
|---|
| 180 | { |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | * updates an entry in the storage |
|---|
| 185 | * |
|---|
| 186 | * Implement this in the concrete storage container. |
|---|
| 187 | * |
|---|
| 188 | * @abstract |
|---|
| 189 | * @param object patForms patForms object that should be stored |
|---|
| 190 | * @return boolean true on success |
|---|
| 191 | */ |
|---|
| 192 | function _updateEntry( &$form ) |
|---|
| 193 | { |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | * checks, whether an entry exists |
|---|
| 198 | * |
|---|
| 199 | * Implement this in the concrete storage container. |
|---|
| 200 | * |
|---|
| 201 | * @abstract |
|---|
| 202 | * @param array primary values |
|---|
| 203 | * @return boolean|array values of the entry, if it exists, false otherwise |
|---|
| 204 | */ |
|---|
| 205 | function _entryExists( $primary ) |
|---|
| 206 | { |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | ?> |
|---|