| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
class patForms_Storage_Propel extends patForms_Storage |
|---|
| 49 |
{ |
|---|
| 50 |
|
|---|
| 51 |
* Stores the classname of the Propel object |
|---|
| 52 |
* |
|---|
| 53 |
* @var object |
|---|
| 54 |
* @access private |
|---|
| 55 |
*/ |
|---|
| 56 |
private $classname; |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
* Stores the Propel peer instance |
|---|
| 60 |
* |
|---|
| 61 |
* @var object |
|---|
| 62 |
* @access private |
|---|
| 63 |
*/ |
|---|
| 64 |
private $peer; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Inits the storage from a Propel peer |
|---|
| 68 |
* |
|---|
| 69 |
* Sets the name of the Propel object classname, creates the Propel peer and |
|---|
| 70 |
* sets the primary field to 'Id'. |
|---|
| 71 |
* |
|---|
| 72 |
* @access public |
|---|
| 73 |
* @param string $peername The classname of the Propel peer |
|---|
| 74 |
*/ |
|---|
| 75 |
public function setStorageLocation($peername) |
|---|
| 76 |
{ |
|---|
| 77 |
$this->peer = new $peername(); |
|---|
| 78 |
$this->classname = array_pop($tmp = explode('.', $this->peer->getOMClass())); |
|---|
| 79 |
|
|---|
| 80 |
$primary = array(); |
|---|
| 81 |
$object = new $this->classname(); |
|---|
| 82 |
foreach($object->buildPkeyCriteria()->keys() as $key) { |
|---|
| 83 |
$primary[] = BasePeer::translateFieldName( |
|---|
| 84 |
$this->classname, $key, 'colName', 'phpName'); |
|---|
| 85 |
} |
|---|
| 86 |
$this->setPrimaryField($primary); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
* Returns a Creole Criteria object for the object lookup |
|---|
| 91 |
* |
|---|
| 92 |
* @access public |
|---|
| 93 |
* @param array $values The values to create the Criteria |
|---|
| 94 |
* @return The Criteria object |
|---|
| 95 |
*/ |
|---|
| 96 |
private function getCriteria($values) |
|---|
| 97 |
{ |
|---|
| 98 |
$object = new $this->classname(); |
|---|
| 99 |
$primary = $this->getPrimary($values); |
|---|
| 100 |
$object->fromArray($primary); |
|---|
| 101 |
return $object->buildPkeyCriteria(); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
* get an entry |
|---|
| 106 |
* |
|---|
| 107 |
* This tries to find an entry in the storage container |
|---|
| 108 |
* that matches the current data that has been set in the |
|---|
| 109 |
* form and populates the form with the data of this |
|---|
| 110 |
* entry |
|---|
| 111 |
* |
|---|
| 112 |
* @access public |
|---|
| 113 |
* @param object patForms patForms object that should be stored |
|---|
| 114 |
* @return boolean true on success |
|---|
| 115 |
*/ |
|---|
| 116 |
public function loadEntry(&$form) |
|---|
| 117 |
{ |
|---|
| 118 |
if(!$object = $this->_entryExists($form->getValues())) { |
|---|
| 119 |
|
|---|
| 120 |
return array(); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
$form->setValues($object->toArray()); |
|---|
| 124 |
return true; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* Lets the Propel peer validate the form data. |
|---|
| 129 |
* |
|---|
| 130 |
* This method gets called by the patForms instance by being registered |
|---|
| 131 |
* to its onValidate event. It should not be necessary to call it this |
|---|
| 132 |
* method otherwise. |
|---|
| 133 |
* |
|---|
| 134 |
* @access public |
|---|
| 135 |
* @param object $form The form |
|---|
| 136 |
* @return True or false, the validation result |
|---|
| 137 |
*/ |
|---|
| 138 |
public function validateEntry(&$form) |
|---|
| 139 |
{ |
|---|
| 140 |
if (!$object = $this->_entryExists($form->getValues())) { |
|---|
| 141 |
$object = new $this->classname(); |
|---|
| 142 |
} |
|---|
| 143 |
$object->fromArray($form->getValues()); |
|---|
| 144 |
$result = $object->validate(); |
|---|
| 145 |
|
|---|
| 146 |
if ($result !== true) { |
|---|
| 147 |
$peer = $object->getPeer(); |
|---|
| 148 |
foreach($result as $colname => $error) { |
|---|
| 149 |
$name = $peer->translateFieldname($colname, 'colName', 'phpName'); |
|---|
| 150 |
$element = $form->getElement($name); |
|---|
| 151 |
$element->addValidatorErrorCodes(array( |
|---|
| 152 |
'C' => array( |
|---|
| 153 |
1 => $error->getMessage(), |
|---|
| 154 |
), |
|---|
| 155 |
), 1000); |
|---|
| 156 |
$element->addValidationError(1001); |
|---|
| 157 |
} |
|---|
| 158 |
return false; |
|---|
| 159 |
} |
|---|
| 160 |
return true; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
* adds an entry to the storage |
|---|
| 165 |
* |
|---|
| 166 |
* @param object patForms patForms object that should be stored |
|---|
| 167 |
* @return boolean true on success |
|---|
| 168 |
*/ |
|---|
| 169 |
public function _addEntry(&$form) |
|---|
| 170 |
{ |
|---|
| 171 |
$object = new $this->classname(); |
|---|
| 172 |
$object->fromArray($form->getValues()); |
|---|
| 173 |
$object->save(); |
|---|
| 174 |
|
|---|
| 175 |
$form->setValues($object->toArray(), true); |
|---|
| 176 |
return true; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
* updates an entry in the storage |
|---|
| 181 |
* |
|---|
| 182 |
* @param object patForms patForms object that should be stored |
|---|
| 183 |
* @return boolean true on success |
|---|
| 184 |
*/ |
|---|
| 185 |
public function _updateEntry(&$form, $primary) |
|---|
| 186 |
{ |
|---|
| 187 |
$object = $this->_entryExists($form->getValues()); |
|---|
| 188 |
$object->fromArray($form->getValues()); |
|---|
| 189 |
$object->save(); |
|---|
| 190 |
|
|---|
| 191 |
$form->setValues($object->toArray(), true); |
|---|
| 192 |
return true; |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
* check, whether an entry exists |
|---|
| 197 |
* |
|---|
| 198 |
* This method gets called multiple times, e.g. when an existing |
|---|
| 199 |
* object gets updated. We'll therefor cache results locally using |
|---|
| 200 |
* a criteria string representation as hash. |
|---|
| 201 |
* |
|---|
| 202 |
* @access private |
|---|
| 203 |
* @param array |
|---|
| 204 |
*/ |
|---|
| 205 |
public function _entryExists($values) |
|---|
| 206 |
{ |
|---|
| 207 |
static $objects; |
|---|
| 208 |
$criteria = $this->getCriteria($values); |
|---|
| 209 |
$hash = $criteria->toString(); |
|---|
| 210 |
|
|---|
| 211 |
if (isset($objects[$hash])) { |
|---|
| 212 |
return $objects[$hash]; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
$objects[$hash] = $this->peer->doSelectOne($criteria); |
|---|
| 216 |
|
|---|
| 217 |
if(empty($objects[$hash])) { |
|---|
| 218 |
return false; |
|---|
| 219 |
} |
|---|
| 220 |
return $objects[$hash]; |
|---|
| 221 |
} |
|---|
| 222 |
} |
|---|
| 223 |
?> |
|---|