/** * beforeValidate is called before a model is validated, you can use this callback to * add behavior validation rules into a models validate array. Returning false * will allow you to make the validation fail. * * @param Model $model Model using this behavior * @param array $options Options passed from Model::save(). * @return mixed False or null will abort the operation. Any other result will continue. * @see Model::save() */ public function beforeValidate(Model $model, $options = array()) { //ページデータをセット $referencePageId = $model->getReferencePageId($model->data); $fields = array('room_id', 'permalink'); $targetPage = $model->findById($referencePageId, $fields); if (empty($targetPage)) { return false; } $model->data['Page']['room_id'] = Current::read('Room.id'); $slug = $model->data['Page']['slug']; if (!isset($slug)) { $slug = ''; } $model->data['Page']['slug'] = $slug; $permalink = ''; if (strlen($targetPage['Page']['permalink']) !== 0) { $permalink = $targetPage['Page']['permalink'] . '/'; } $permalink .= $slug; $model->data['Page']['permalink'] = $permalink; $model->data['Page']['is_published'] = true; $model->data['Page']['is_container_fluid'] = false; return parent::beforeValidate($model, $options); }
/** * 根据id 查找数据,默认查找缓存 * @param int $id * @param boolean $isCache * @return array */ public function findById($id, $isCache = true) { if ($isCache == true) { $dataList = $this->_getGlobalData($this->_tableName); return $dataList[$id]; } else { return parent::findById($id); } }
/** * 根据id查找指定的id * @param int $id * @param boolean $isCache * @return array */ public function findById($id, $isCache = true) { if ($isCache == true) { $serverList = $this->_getGlobalData('operator_list'); return $serverList[$id]; } else { return parent::findById($id); } }
public static function getUser($id = null) { $db = new Model(); if ($id == null) { $id = $_SESSION['uid']; } $user = $db->findById($id, 'users'); return self::instantiate($user); }
/** * IDで検索(blog_pluginsのattribute属性付き */ public function findById($id, $options = array()) { $plugin = parent::findById($id, $options); if (!empty($plugin)) { $plugin['title_align'] = $plugin['contents_align'] = 'left'; $plugin['title_color'] = $plugin['contents_color'] = ''; } return $plugin; }
/** * hasDiffAddress * @param Model $model * @return boolean */ protected function hasDiffAddress(Model $model) { $this->_instance = $model->findById($model->id); if ($this->_instance !== null && isset($model->data['User']['live_country'])) { $prevAddress = array('live_country' => $this->_instance['User']['live_country'], 'live_place' => $this->_instance['User']['live_place'], 'live_address' => $this->_instance['User']['live_address']); $currentAddress = array('live_country' => $model->data['User']['live_country'], 'live_place' => $model->data['User']['live_place'], 'live_address' => $model->data['User']['live_address']); $result = Hash::diff($prevAddress, $currentAddress); return !empty($result) ? true : false; } return false; }
public function beforeSave(Model $model, $options = array()) { if (isset($this->options[$model->alias]['actions']['edit'])) { $fields = array(); foreach ($this->options[$model->alias]['actions']['edit']['fields'] as $field => $options) { $fields[] = $field; } $this->oldData = $model->findById($model->id, $fields); } return true; }
/** * Creates Old object and List of changed fields * @param Model $Model * @param array $options * @return bool */ public function beforeSave($Model, $options) { $Model->recursive = -1; $Model->old = $Model->findById($Model->id); if ($Model->old) { foreach ($Model->data[$Model->alias] as $key => $value) { if ($Model->old[$Model->alias][$key] != $value) { $Model->changed_fields[] = $key; } } } return true; }
public function download(Model $model, $id) { $model->recursive = -1; $data = $model->findById($id); $fields = $this->settings[$model->alias]; $fileName = $data[$model->alias][$fields['name']]; $fileInfo = explode('.', $fileName); $fileType = $fileInfo[count($fileInfo) - 1]; header('Content-type: ' . $fileType); header('Content-Disposition: attachment; filename="' . $fileName . '"'); echo $data[$model->alias][$fields['content']]; exit; }
/** * beforeSave function. * * @access public * @param array $options (default: array()) * @return void */ public function beforeSave(Model $Model) { // Lock out those who arnt allowed to write if (!$Model->Project->hasWrite($Model->_auth_user_id)) { throw new ForbiddenException(__('You do not have permissions to write to this project')); } $before = $Model->findById($Model->id); foreach ($Model->data[$Model->name] as $field => $value) { if ($field != 'modified' && $before[$Model->name][$field] != $value) { $this->_cache[$field] = $before[$Model->name][$field]; } } return true; }
/** * onLogin * @param Model $model * @return array $data */ protected function onLogin(Model $model) { $result = false; $this->_instance = $model->findById($model->id); if ($this->_instance !== null && (empty($this->_instance['User']['lat']) || empty($this->_instance['User']['lng']))) { if (isset($model->data['User']['lat']) && !empty($model->data['User']['lat']) && isset($model->data['User']['lng']) && !empty($model->data['User']['lng'])) { $result = $this->onExistLatLng($model->data['User']['lat'], $model->data['User']['lng']); } else { $result = $this->onIpProcess(); } } else { return array('lat' => $this->_instance['User']['lat'], 'lng' => $this->_instance['User']['lng']); } return $this->onProcessAddressResult($result); }
/** * Obtiene los grupos hijos del grupo al que el usuario loggeado pertenece, para obtener los prefijos a los que este usuario tiene permiso de edición. * @return Array * @TODO hacer que obtenga los hijos de la tabla de grupo como arbol */ function __getGroupPrefix() { //$groups=array(); $parent = array(Configure::read("loggedUser.User.group_id")); $group = $this->Group->findById(Configure::read("loggedUser.User.group_id"), array('name')); $groups = Set::extract("/Group/name", $group); //pr($groups); while (!empty($parent)) { $recordset = $this->Group->find("all", array('conditions' => array('Group.parent_id' => $parent), 'fields' => array('Group.id', "Group.name"))); $childrenName = Set::extract("/Group/name", $recordset); if (!empty($childrenName)) { $groups = am($groups, $childrenName); } $parent = Set::extract("/Group/id", $recordset); } #while(!empty($parent)); #pr($groups); return $groups; }
public function beforeValidate(Model $model, $options = array()) { if (!array_key_exists('id', $model->data[$model->alias]) || !$model->data[$model->alias]['id']) { if (!isset($model->id) || !$model->id) { return true; } $model->data[$model->alias]['id'] = $model->id; } if (!array_key_exists('opt_' . $this->config['field'], $model->data[$model->alias]) || !$model->data[$model->alias]['opt_' . $this->config['field']]) { throw new RuntimeException(__d('optimistic_lock', 'Field %s doesn\'t appear in the post request.', $this->config['field'])); } if ($currentRecord = $model->findById($model->data[$model->alias]['id'])) { if ($model->data[$model->alias]['opt_' . $this->config['field']] != $currentRecord[$model->alias][$this->config['field']]) { $model->validationErrors[$this->config['field']] = $this->config['message']; return false; } } unset($model->data[$model->alias]['opt_' . $this->config['field']]); return true; }
public function beforeSave(Model $model, $options = array()) { if (isset($model->data[$model->name][$this->settings[$model->name]['label']])) { extract($this->settings[$model->name]); $attachment = $model->data[$model->name][$label]; if (!empty($attachment['name'])) { //====defining names for our database columns and also giving unique name for each attachment====// $model->data[$model->name][$fields['filename']] = $this->_fileExists($this->_attachmentRoot . $attachment['name']) ? time() . '_' . $attachment['name'] : $attachment['name']; //====checking all the conditions before uploading and saving====// if (!$this->_isUploaded($attachment) || !$this->_isValidSize($attachment['size']) || !$this->_isValidType($attachment) || !$this->_dirExists($this->_attachmentRoot) || !$this->_moveUploaded($attachment['tmp_name'], $model->data[$model->name][$fields['filename']])) { $model->validationErrors[$label] = $this->_errorMsg; return false; } //====if the file type is image/jpeg====// if (in_array($attachment['type'], $this->_validThumbTypes)) { list($width, $height) = getimagesize($this->_attachmentRoot . $model->data[$model->name][$fields['filename']]); if (isset($this->settings[$model->name]['thumbnails'])) { //====resizing the images and saving them to different location====// foreach ($this->settings[$model->name]['thumbnails'] as $dir => $size) { list($width, $height) = explode('x', strtolower($size)); if ($this->_dirExists($this->_attachmentRoot . $dir)) { $this->_resizeToThumb($model->data[$model->name][$fields['filename']], $dir, $width, $height); } } } } } else { if ($model->id > 0) { $record = $model->findById($model->id); if (!empty($record[$model->name][$fields['filename']])) { $model->data[$model->name][$fields['filename']] = $record[$model->name][$fields['filename']]; } else { $model->data[$model->name][$fields['filename']] = ''; } } else { $model->data[$model->name][$fields['filename']] = ''; } } } return true; }
/** * 根据ID查找记录 * @param int $id */ public function findById($id, $isCache = TRUE) { if ($isCache) { $serverList = $this->_getGlobalData('gameser_list'); return $serverList[$id]; } else { return parent::findById($id); } }
/** * After delete * 画像ファイルの削除を行う * 削除に失敗してもデータの削除は行う * * @param Model $model * @return void * @access public */ function beforeDelete(&$model) { $model->data = $model->findById($model->id); $this->delFiles($model); }
/** * After delete * 画像ファイルの削除を行う * 削除に失敗してもデータの削除は行う * * @param Model $Model * @return void * @access public */ public function beforeDelete(Model $Model, $cascade = true) { $Model->data = $Model->findById($Model->id); $this->delFiles($Model); }
/** * Validation file when is required. * * @param Model $model Reference to model * @param array $valudateValue Array of settings validation * @return boolean Success of validation */ public function fileRequired(Model $model, $validateValue) { $validateKeys = array_keys($validateValue); $validateVariable = array_shift($validateValue); if ($model->id) { $file = $model->findById($model->id); if (!empty($file[$model->alias][$validateKeys[0]]) && file_exists($this->settings[$model->alias][$validateKeys[0]]['path'] . DS . $file[$model->alias][$validateKeys[0]])) { return true; } } if (!empty($validateVariable['tmp_name'])) { return true; } return false; }
public function edit(Model $model, $id = 0) { $model->render = 'auto'; if ($model->exists($id)) { $this->recursive = 0; $data = $model->findById($id); if ($model->request->is(array('post', 'put'))) { if ($model->save($model->request->data)) { $model->Message->alert('general.edit.success'); return $model->redirect(array('action' => get_class($model), 'view', $id)); } else { $model->Message->alert('general.edit.failed'); } } else { $model->request->data = $data; } } else { $model->Message->alert('general.view.notExists'); return $model->redirect(array('action' => get_class($model))); } }
/** * Save each file to folder and update db * * @param Model $Model * @param boolean $created */ public function afterSave(Model $Model, $created) { if ($this->_saving) { return true; } $this->_saving = true; if (!empty($this->_uploadLater)) { foreach ($this->_uploadLater as $key => $fields) { foreach ($fields as $name => $file) { if (!is_uploaded_file($file['tmp_name'])) { continue; } $ext = strtolower(strrchr($file['name'], '.')); $filename = md5($file['name'] . $file['size']) . $ext; $dest = $this->settings['uploadLocation'] . Inflector::tableize($Model->alias) . DS; if (!file_exists($dest)) { new Folder($dest, true); } move_uploaded_file($file['tmp_name'], $dest . $filename); $Model->saveField($name, $filename); } } $this->_uploadLater = array(); } $this->_saving = false; $Model->data = $Model->findById($Model->id); return true; }
/** * afterDeleteで使いたいので削除前に削除対象のデータを保持しておく * * @param Model $Model 認証キーを使ってるモデル * @param bool $cascade cascade * @return bool * @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ public function beforeDelete(Model $Model, $cascade = true) { if ($cascade) { $this->_deleteTargetData = $Model->findById($Model->id); } return true; }
/** * Inserts an item on a certain position * * @param Model $model * @param $position * @return boolean */ private function __insertAtPosition(Model $model, $position) { extract($this->settings[$model->alias]); $data = $model->data; $model->data[$model->alias][$positionColumn] = 0; $model->save(null, array('validate' => $validate, 'callbacks' => $callbacks)); $model->create($data); $model->recursive = 0; $model->findById($model->id); $this->removeFromList($model); $result = $this->__incrementPositionsOnLowerItems($model, $position); if ($position <= $this->__bottomPositionInList($model) + 1) { $model->data[$model->alias][$positionColumn] = $position; $result = $model->save(null, array('validate' => $validate, 'callbacks' => $callbacks)); } return $result; }
public function beforeDelete(Model $Model, $cascade = true) { $upload = $Model->findById($Model->id); $upload = $upload[$Model->alias]; $uploadType = $Model->alias == 'BrwImage' ? 'images' : 'files'; $relModel = ClassRegistry::init($upload['model']); $uploadsPath = $relModel->brwConfig[$uploadType][$upload['category_code']]['path']; $this->_deleteFiles($uploadsPath, $upload['model'], $upload['record_id'], $upload['name']); return true; }
public static function getPic($id) { $db = new Model(); $pic = $db->findById($id, 'pics'); return $pic; }
public function Gettest($id) { $model = new Model(); $item = $model->findById($id); return $item; }