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