| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
define('PATFORMS_STORAGE_ERROR_STORAGE_INVALID', 'patForms::Storage::0001'); |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 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 |
* check, whether an entry exists |
|---|
| 172 |
* |
|---|
| 173 |
* @author Frank Kleine <frank.kleine@schlund.de> |
|---|
| 174 |
* @access public |
|---|
| 175 |
* @param object patForms patForms object that should be stored |
|---|
| 176 |
* @return boolean true if object exists |
|---|
| 177 |
*/ |
|---|
| 178 |
function entryExists(&$form) |
|---|
| 179 |
{ |
|---|
| 180 |
$values = $form->getValues(); |
|---|
| 181 |
$primary = $this->getPrimary($values); |
|---|
| 182 |
$data = $this->_entryExists($primary); |
|---|
| 183 |
if ($data == false) { |
|---|
| 184 |
return false; |
|---|
| 185 |
} |
|---|
| 186 |
return true; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
* adds an entry to the storage |
|---|
| 191 |
* |
|---|
| 192 |
* Implement this in the concrete storage container. |
|---|
| 193 |
* |
|---|
| 194 |
* @abstract |
|---|
| 195 |
* @param object patForms patForms object that should be stored |
|---|
| 196 |
* @return boolean true on success |
|---|
| 197 |
*/ |
|---|
| 198 |
function _addEntry( &$form ) |
|---|
| 199 |
{ |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
* updates an entry in the storage |
|---|
| 204 |
* |
|---|
| 205 |
* Implement this in the concrete storage container. |
|---|
| 206 |
* |
|---|
| 207 |
* @abstract |
|---|
| 208 |
* @param object patForms patForms object that should be stored |
|---|
| 209 |
* @return boolean true on success |
|---|
| 210 |
*/ |
|---|
| 211 |
function _updateEntry( &$form ) |
|---|
| 212 |
{ |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
* checks, whether an entry exists |
|---|
| 217 |
* |
|---|
| 218 |
* Implement this in the concrete storage container. |
|---|
| 219 |
* |
|---|
| 220 |
* @abstract |
|---|
| 221 |
* @param array primary values |
|---|
| 222 |
* @return boolean|array values of the entry, if it exists, false otherwise |
|---|
| 223 |
*/ |
|---|
| 224 |
function _entryExists( $primary ) |
|---|
| 225 |
{ |
|---|
| 226 |
return false; |
|---|
| 227 |
} |
|---|
| 228 |
} |
|---|
| 229 |
?> |
|---|