|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright 2011-2016 City of Bloomington, Indiana |
| 4 | + * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE |
| 5 | + */ |
| 6 | +namespace Application; |
| 7 | +use Zend\Db\Sql\Sql; |
| 8 | + |
| 9 | +abstract class ActiveRecord |
| 10 | +{ |
| 11 | + protected $tablename; |
| 12 | + protected $data = array(); |
| 13 | + |
| 14 | + const MYSQL_DATE_FORMAT = 'Y-m-d'; |
| 15 | + const MYSQL_TIME_FORMAT = 'H:i:s'; |
| 16 | + const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s'; |
| 17 | + |
| 18 | + abstract public function validate(); |
| 19 | + |
| 20 | + /** |
| 21 | + * Callback from TableGateway |
| 22 | + */ |
| 23 | + public function exchangeArray($data) |
| 24 | + { |
| 25 | + $this->data = $data; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Writes the database back to the database |
| 30 | + */ |
| 31 | + protected function save() |
| 32 | + { |
| 33 | + $this->validate(); |
| 34 | + $zend_db = Database::getConnection(); |
| 35 | + $sql = new Sql($zend_db, $this->tablename); |
| 36 | + if ($this->getId()) { |
| 37 | + $update = $sql->update() |
| 38 | + ->set($this->data) |
| 39 | + ->where(array('id'=>$this->getId())); |
| 40 | + $sql->prepareStatementForSqlObject($update)->execute(); |
| 41 | + } |
| 42 | + else { |
| 43 | + $insert = $sql->insert()->values($this->data); |
| 44 | + $sql->prepareStatementForSqlObject($insert)->execute(); |
| 45 | + $this->data['id'] = $zend_db->getDriver()->getLastGeneratedValue(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Removes this record from the database |
| 51 | + */ |
| 52 | + protected function delete() |
| 53 | + { |
| 54 | + if ($this->getId()) { |
| 55 | + $sql = new Sql(Database::getConnection(), $this->tablename); |
| 56 | + $delete = $sql->delete()->where(['id'=>$this->getId()]); |
| 57 | + $sql->prepareStatementForSqlObject($delete)->execute(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns any field stored in $data |
| 63 | + * |
| 64 | + * @param string $fieldname |
| 65 | + */ |
| 66 | + protected function get($fieldname) |
| 67 | + { |
| 68 | + if (isset($this->data[$fieldname])) { |
| 69 | + return $this->data[$fieldname]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param string $fieldname |
| 75 | + * @param string $value |
| 76 | + */ |
| 77 | + protected function set($fieldname, $value) |
| 78 | + { |
| 79 | + $value = trim($value); |
| 80 | + $this->data[$fieldname] = $value ? $value : null; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the date/time in the desired format |
| 85 | + * |
| 86 | + * Format is specified using PHP's date() syntax |
| 87 | + * http://www.php.net/manual/en/function.date.php |
| 88 | + * If no format is given, the database's raw data is returned |
| 89 | + * |
| 90 | + * @param string $field |
| 91 | + * @param string $format |
| 92 | + * @param DateTimeZone $timezone |
| 93 | + * @return string |
| 94 | + */ |
| 95 | + protected function getDateData($dateField, $format=null, \DateTimeZone $timezone=null) |
| 96 | + { |
| 97 | + if (isset($this->data[$dateField])) { |
| 98 | + if ($format) { |
| 99 | + $date = new \DateTime($this->data[$dateField]); |
| 100 | + if ($timezone) { $date->setTimezone($timezone); } |
| 101 | + return $date->format($format); |
| 102 | + } |
| 103 | + else { |
| 104 | + return $this->data[$dateField]; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets a date |
| 111 | + * |
| 112 | + * Dates should be in DATETIME_FORMAT, set in configuration.inc |
| 113 | + * If we cannot parse the string using DATETIME_FORMAT, we will |
| 114 | + * fall back to trying something strtotime() understands |
| 115 | + * http://www.php.net/manual/en/function.strtotime.php |
| 116 | + * |
| 117 | + * @param string $dateField |
| 118 | + * @param string $date |
| 119 | + * @param string $format |
| 120 | + * @param string $databaseFormat |
| 121 | + */ |
| 122 | + protected function setDateData($dateField, $date, $format=DATETIME_FORMAT, $databaseFormat=self::MYSQL_DATETIME_FORMAT) |
| 123 | + { |
| 124 | + $date = trim($date); |
| 125 | + if ($date) { |
| 126 | + try { |
| 127 | + $d = self::parseDate($date, $format); |
| 128 | + $this->data[$dateField] = $d->format($databaseFormat); |
| 129 | + } |
| 130 | + catch (\Exception $e) { |
| 131 | + $class = strtolower((new \ReflectionClass($this))->getShortName()); |
| 132 | + throw new \Exception("$class/$dateField/invalidDate"); |
| 133 | + } |
| 134 | + } |
| 135 | + else { |
| 136 | + $this->data[$dateField] = null; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return a DateTime object for a date string |
| 142 | + * |
| 143 | + * Dates should be in $format. |
| 144 | + * If we cannot parse the string using $format, we will |
| 145 | + * fall back to trying something strtotime() understands |
| 146 | + * http://www.php.net/manual/en/function.strtotime.php |
| 147 | + * |
| 148 | + * @param string $date |
| 149 | + * @param string $format |
| 150 | + * @throws Exception |
| 151 | + * @return DateTime |
| 152 | + */ |
| 153 | + public static function parseDate($date, $format=DATETIME_FORMAT) |
| 154 | + { |
| 155 | + $d = \DateTime::createFromFormat($format, $date); |
| 156 | + if (!$d) { |
| 157 | + $d = new \DateTime($date); |
| 158 | + } |
| 159 | + return $d; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Loads and returns an object for a foreign key _id field |
| 164 | + * |
| 165 | + * Will cache the object in a protected variable to avoid multiple database |
| 166 | + * lookups. Make sure to declare a protected variable matching the class |
| 167 | + * |
| 168 | + * @param string $class Fully namespaced classname |
| 169 | + * @param string $field |
| 170 | + */ |
| 171 | + protected function getForeignKeyObject($class, $field) |
| 172 | + { |
| 173 | + $var = preg_replace('/_id$/', '', $field); |
| 174 | + if (!$this->$var && isset($this->data[$field])) { |
| 175 | + $this->$var = new $class($this->data[$field]); |
| 176 | + } |
| 177 | + return $this->$var; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Verifies and saves the ID for a foreign key field |
| 182 | + * |
| 183 | + * Loads the object record for the foreign key and caches |
| 184 | + * the object in a private variable |
| 185 | + * |
| 186 | + * @param string $class Fully namespaced classname |
| 187 | + * @param string $field Name of field to set |
| 188 | + * @param string $id The value to set |
| 189 | + */ |
| 190 | + protected function setForeignKeyField($class, $field, $id) |
| 191 | + { |
| 192 | + $id = trim($id); |
| 193 | + $var = preg_replace('/_id$/', '', $field); |
| 194 | + if ($id) { |
| 195 | + $this->$var = new $class($id); |
| 196 | + $this->data[$field] = $this->$var->getId(); |
| 197 | + } |
| 198 | + else { |
| 199 | + $this->$field = null; |
| 200 | + $this->data[$field] = null; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Verifies and saves the ID for a foreign key object |
| 206 | + * |
| 207 | + * Caches the object in a private variable and sets |
| 208 | + * the ID value in the data |
| 209 | + * |
| 210 | + * @param string $class Fully namespaced classname |
| 211 | + * @param string $field Name of field to set |
| 212 | + * @param Object $object Value to set |
| 213 | + */ |
| 214 | + protected function setForeignKeyObject($class, $field, $object) |
| 215 | + { |
| 216 | + if ($object instanceof $class) { |
| 217 | + $var = preg_replace('/_id$/', '', $field); |
| 218 | + $this->data[$field] = $object->getId(); |
| 219 | + $this->$var = $object; |
| 220 | + } |
| 221 | + else { |
| 222 | + throw new \Exception('Object does not match the given class'); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Returns whether the value can be an ID for a record |
| 228 | + * |
| 229 | + * return @bool |
| 230 | + */ |
| 231 | + public static function isId($id) |
| 232 | + { |
| 233 | + return ((is_int($id) && $id>0) || (is_string($id) && ctype_digit($id))); |
| 234 | + } |
| 235 | +} |
0 commit comments