/** * 関連ページに反映する * * @param string $type * @param array $data * @return boolean */ public function refrect($type, $data) { if (isset($this->data['Page'])) { $data = $this->data['Page']; } // モバイルページへのコピーでスーパークラスのIDを上書きしてしまうので退避させておく $this->__pageInsertID = parent::getInsertID(); $agentId = $this->PageCategory->getAgentId($type); if (!$agentId) { // カテゴリがない場合は trueを返して終了 return true; } $data['url'] = '/' . Configure::read('BcAgent.' . $type . '.prefix') . $this->removeAgentPrefixFromUrl($data['url']); $agentPage = $this->find('first', array('conditions' => array('Page.url' => $data['url']), 'recursive' => -1)); unset($data['id']); unset($data['sort']); unset($data['status']); if ($agentPage) { $agentPage['Page']['name'] = $data['name']; $agentPage['Page']['title'] = $data['title']; $agentPage['Page']['description'] = $data['description']; $agentPage['Page']['draft'] = $data['draft']; $agentPage['Page']['modified'] = $data['modified']; $agentPage['Page']['contents'] = $data['contents']; $agentPage['Page']['reflect_mobile'] = false; $agentPage['Page']['reflect_smartphone'] = false; $this->set($agentPage); } else { if ($data['page_category_id']) { $fields = array('parent_id', 'name', 'title'); $pageCategoryTree = $this->PageCategory->getTreeList($fields, $data['page_category_id']); $path = getViewPath() . 'Pages' . DS . Configure::read('BcAgent.' . $type . '.prefix'); $parentId = $agentId; foreach ($pageCategoryTree as $pageCategory) { $path .= '/' . $pageCategory['PageCategory']['name']; $categoryId = $this->PageCategory->getIdByPath($path); if (!$categoryId) { $pageCategory['PageCategory']['parent_id'] = $parentId; $this->PageCategory->create($pageCategory); $ret = $this->PageCategory->save(); $parentId = $categoryId = $this->PageCategory->getInsertID(); } else { $parentId = $categoryId; } } $data['page_category_id'] = $categoryId; } else { $data['page_category_id'] = $agentId; } $data['author_id'] = $_SESSION['Auth']['User']['id']; $data['sort'] = $this->getMax('sort') + 1; $data['status'] = false; // 新規ページの場合は非公開とする unset($data['publish_begin']); unset($data['publish_end']); unset($data['created']); unset($data['modified']); $data['reflect_mobile'] = false; $data['reflect_smartphone'] = false; $this->create($data); } return $this->save(); }
/** * Creates array of $model->alias => array($model->id => $model->data) for * use by afterDelete or afterSave. * * @param AppModel $model * @param boolean $created Indicates whether the record is being/was created * @param boolean $reset Determines whether to reset the _records property * before adding new ones. */ function setSearchableRecords(&$model, $created = false, $reset = false) { if ($reset) { $this->_records = array(); } // Set the foreign key from either the model id or the last inserted id $foreignKey = $model->id; if (!$foreignKey && $created) { $foreignKey = $model->getInsertID(); } $this->_records[$model->alias][$foreignKey] = $model->data; }
/** * When doing any update all calls, you want to avoid updating the record * you've just modified, as the order will have been set already, so exclude * it with some conditions. * * @param AppModel $model * @return array Array Model.primary_key => $id */ function conditionsNotCurrent(&$model) { if (!($id = $model->id)) { $id = $model->getInsertID(); } return array($model->escapeField($model->primaryKey) . ' <>' => $id); }
/** * 最終登録IDを取得する * * @return int */ public function getInsertID() { if (!$this->__pageInsertID) { $this->__pageInsertID = parent::getInsertID(); } return $this->__pageInsertID; }
/** * * Private function that saves the nodes recursively * * @param AppModel $Model Model instance * @param array $data The nodes to create * @param array $options Settings * @return mixed true if succesfully saved or false on error * @access private */ private function __doTreeSave($Model, $data, $options = array()) { $return = false; //Special case in the first run if ($options['depth'] > 0) { $Model->create(); if (!$Model->save(array_diff_key(array_merge(array($this->settings[$Model->alias]['scopeField'] => $options['scope'], $this->settings[$Model->alias]['parent'] => $options['parent']), $data), array($Model->alias => $Model->alias)))) { return false; } $options['parent'] = $Model->getInsertID(); $return = true; } if (array_key_exists($Model->alias, $data)) { $options['depth']++; foreach ($data[$Model->alias] as $childData) { if (!$this->__doTreeSave($Model, $childData, $options)) { return false; } } $return = true; } return $return; }