function countdim($array) { if (is_array(reset($array))) { $return = countdim(reset($array)) + 1; } else { $return = 1; } return $return; }
/** * Saves model data to the database. * By default, validation occurs before save. * * @param array $data Data to save. * @param boolean $validate If set, validation will be done before the save * @param array $fieldList List of fields to allow to be written * @return boolean success * @access public */ function save($data = null, $validate = true, $fieldList = array()) { $db =& ConnectionManager::getDataSource($this->useDbConfig); if ($data) { if (countdim($data) == 1) { $this->set(array($this->name => $data)); } else { $this->set($data); } } $whitelist = !(empty($fieldList) || count($fieldList) == 0); if ($validate && !$this->validates()) { return false; } if (!$this->beforeSave()) { return false; } $fields = $values = array(); if (count($this->data) > 1) { $weHaveMulti = true; $joined = false; } else { $weHaveMulti = false; } $habtm = count($this->hasAndBelongsToMany); foreach ($this->data as $n => $v) { if (isset($weHaveMulti) && isset($v[$n]) && $habtm > 0) { $joined[] = $v; } else { if ($n === $this->name) { foreach (array('created', 'updated', 'modified') as $field) { if (array_key_exists($field, $v) && (empty($v[$field]) || $v[$field] === null)) { unset($v[$field]); } } foreach ($v as $x => $y) { if ($this->hasField($x) && ($whitelist && in_array($x, $fieldList) || !$whitelist)) { $fields[] = $x; $values[] = $y; } } } } } $exists = $this->exists(); if (!$exists && $this->hasField('created') && !in_array('created', $fields) && ($whitelist && in_array('created', $fieldList) || !$whitelist)) { $fields[] = 'created'; $values[] = date('Y-m-d H:i:s'); } if ($this->hasField('modified') && !in_array('modified', $fields) && ($whitelist && in_array('modified', $fieldList) || !$whitelist)) { $fields[] = 'modified'; $values[] = date('Y-m-d H:i:s'); } if ($this->hasField('updated') && !in_array('updated', $fields) && ($whitelist && in_array('updated', $fieldList) || !$whitelist)) { $fields[] = 'updated'; $values[] = date('Y-m-d H:i:s'); } if (!$exists) { $this->id = false; } if (count($fields)) { if (!empty($this->id)) { if ($db->update($this, $fields, $values)) { if (!empty($joined)) { $this->__saveMulti($joined, $this->id); } $this->afterSave(); $this->data = false; $this->_clearCache(); return true; } else { return false; } } else { if ($db->create($this, $fields, $values)) { if (!empty($joined)) { $this->__saveMulti($joined, $this->id); } $this->afterSave(); $this->data = false; $this->_clearCache(); $this->validationErrors = array(); return true; } else { return false; } } } else { return false; } }
function afterFind($results) { //pr($results); if (countdim($results[0]['Site']) == 1) { //a find() was executed if (isset($results[0]['Site']['id'])) { $site = $results[0]['Site']; $results[0]['Site']['rurality_points'] = $this->calculateRuralityPoints($site); } } elseif (countdim($results[0]['Site']) == 2) { //a findall for ($i = 0; $i < count($results[0]['Site']); $i++) { $results[0]['Site'][$i]['rurality_points'] = $this->calculateRuralityPoints($results[0]['Site'][$i]); } } //pr($results[0]['Site']); return $results; }
function __output($data) { $out = ''; foreach ($data as $key => $val) { $keyAppend = ''; if (in_array($key, array('Calendar', 'Event', 'Timezone', 'Todo', 'Alarm', 'Journals'))) { $key = 'v' . $key; } if (is_array($val) && strtolower($key) != $key) { if (countdim($val) > 1) { foreach ($val as $val2) { $out .= strtoupper("begin:{$key}\n"); $out .= $this->__output($val2); $out .= strtoupper("end:{$key}\n"); } } else { $out .= strtoupper("begin:{$key}\n"); $out .= $this->__output($val); $out .= strtoupper("end:{$key}\n"); } } else { if (is_array($val)) { $tmp = array(); foreach ($val as $key2 => $val2) { if ($key2 !== 0) { $tmp[] = strtoupper($key2) . '=' . $val2; } } if (!empty($tmp)) { $keyAppend = ';' . join(';', $tmp); } $_val = $val[0]; } else { $_val = $val; } switch ($key) { case 'end_date': case 'start_date': case 'date_stamp': case 'last_modified': case 'trigger': if (strpos($_val, ' weeks') === false && strpos($_val, ' days') === false && strpos($_val, ' hours') === false && strpos($_val, ' minutes') === false && strpos($_val, ' seconds') === false) { $utc = false; if (strpos($_val, 'UTC')) { $utc = true; } if (strpos($_val, ' ') === false && strpos($_val, ':') === false) { $val = date('Ymd', strtotime($_val)); } else { $_val = trim(r('UTC', '', $_val)); $tmp = date('Ymd', strtotime($_val)) . 'T' . date('His', strtotime($_val)); if ($utc) { $tmp .= 'Z'; } $val = $tmp; } } else { $val = $this->__putDuration($val); } break; case 'duration': $val = $this->__putDuration($val); break; case 'contact': case 'comment': case 'description': case 'location': case 'prodid': case 'resources': case 'status': case 'summary': $s = array_keys($this->__textMap); $r = array_values($this->__textMap); $val = str_replace($s, $r, $val); $val = str_replace('\\\\', '\\', $val); break; default: if ($val === true) { $val = 'TRUE'; } elseif ($val === false) { $val = 'FALSE'; } break; } if (in_array($key, array_keys($this->__keyMap))) { $key = $this->__keyMap[$key]; } if (is_array($val) && isset($val[0])) { $val = $val[0]; } $out .= strtoupper(str_replace('_', '-', $key)) . $keyAppend . ':' . $val . "\n"; } } return $out; }
/** * @deprecated * @see Set::countDim */ function countdim($array) { trigger_error(__('Deprecated: Use Set::countDim instead'), E_USER_WARNING); if (is_array(reset($array))) { $return = countdim(reset($array)) + 1; } else { $return = 1; } return $return; }