/** * Setup * * @param AppModel $model * @param array $settings * @access public */ public function setup(&$model, $settings = array()) { if (!isset($this->settings[$model->alias])) { $this->settings[$model->alias] = $this->defaults; } $this->settings[$model->alias] = am($this->settings[$model->alias], ife(is_array($settings), $settings, array())); $cfg = $this->settings[$model->alias]; $model->bindModel(array('hasMany' => array('Comment' => array('className' => $cfg['commentModel'], 'foreignKey' => 'foreign_key', 'unique' => true, 'conditions' => '', 'fields' => '', 'dependent' => true, 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''))), false); $model->Comment->bindModel(array('belongsTo' => array($model->name => array('className' => $model->name, 'foreignKey' => 'foreign_key', 'unique' => true, 'conditions' => '', 'fields' => '', 'dependent' => false))), false); $model->Comment->bindModel(array('belongsTo' => array($cfg['userModelAlias'] => array('className' => $cfg['userModelClass'], 'foreignKey' => 'user_id', 'conditions' => '', 'fields' => '', 'counterCache' => true, 'order' => ''))), false); }
/** * Recover a corrupted tree * * The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data * will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode * 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction * parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present. * * @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB. * @param AppModel $Model Model instance * @param string $mode parent or tree * @param mixed $missingParentAction 'return' to do nothing and return, 'delete' to * delete, or the id of the parent to set as the parent_id * @return boolean true on success, false on failure * @access public * @link http://book.cakephp.org/view/1628/Recover */ function recover(&$Model, $mode = 'parent', $missingParentAction = null) { if (is_array($mode)) { extract(array_merge(array('mode' => 'parent'), $mode)); } extract($this->settings[$Model->alias]); $Model->recursive = $recursive; if ($mode == 'parent') { $Model->bindModel(array('belongsTo' => array('VerifyParent' => array('className' => $Model->alias, 'foreignKey' => $parent, 'fields' => array($Model->primaryKey, $left, $right, $parent))))); $missingParents = $Model->find('list', array('recursive' => 0, 'conditions' => array($scope, array('NOT' => array($Model->escapeField($parent) => null), $Model->VerifyParent->escapeField() => null)))); $Model->unbindModel(array('belongsTo' => array('VerifyParent'))); if ($missingParents) { if ($missingParentAction == 'return') { foreach ($missingParents as $id => $display) { $this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')'; } return false; } elseif ($missingParentAction == 'delete') { $Model->deleteAll(array($Model->primaryKey => array_flip($missingParents))); } else { $Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents))); } } $count = 1; foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) { $Model->id = $array[$Model->alias][$Model->primaryKey]; $lft = $count++; $rght = $count++; $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false)); } foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) { $Model->create(); $Model->id = $array[$Model->alias][$Model->primaryKey]; $this->_setParent($Model, $array[$Model->alias][$parent]); } } else { $db =& ConnectionManager::getDataSource($Model->useDbConfig); foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) { $path = $this->getpath($Model, $array[$Model->alias][$Model->primaryKey]); if ($path == null || count($path) < 2) { $parentId = null; } else { $parentId = $path[count($path) - 2][$Model->alias][$Model->primaryKey]; } $Model->updateAll(array($parent => $db->value($parentId, $parent)), array($Model->escapeField() => $array[$Model->alias][$Model->primaryKey])); } } return true; }
/** * Upgrade to 4.0.0. */ public function to_40() { $answer = strtoupper($this->in('This upgrade will delete the following tables after migration: settings, access, access_levels. Are you sure you want to continue?', array('Y', 'N'))); if ($answer === 'N') { exit; } $Access = new AppModel(null, 'access', FORUM_DATABASE); $Access->alias = 'Access'; $Access->tablePrefix = FORUM_PREFIX; $Access->bindModel(array('belongsTo' => array('User' => array('className' => FORUM_USER)))); $AccessLevel = new AppModel(null, 'access_levels', FORUM_DATABASE); $AccessLevel->alias = 'AccessLevel'; $AccessLevel->tablePrefix = FORUM_PREFIX; $Forum = ClassRegistry::init('Forum.Forum'); $Acl = ClassRegistry::init('Forum.Access'); // Create ACL request objects $this->out('Installing ACL...'); $aclMap = $Acl->installAcl(); $levelMap = array(); foreach ($AccessLevel->find('all') as $level) { $id = $level['AccessLevel']['id']; if ($id == 3) { $alias = Configure::read('Forum.aroMap.superMod'); } else { if ($id == 4) { $alias = Configure::read('Forum.aroMap.admin'); } else { continue; } } $record = $Acl->getBySlug($alias); $levelMap[$id] = $record['Access']['id']; } // Create users $this->out('Migrating users...'); foreach ($Access->find('all') as $user) { $Acl->add(array('foreign_key' => $user['User']['id'], 'parent_id' => $levelMap[$user['Access']['access_level_id']])); } // Migrate access levels $this->out('Migrating access relations...'); $forums = $Forum->find('all', array('conditions' => array('Forum.access_level_id !=' => 0))); if ($forums) { foreach ($forums as $forum) { $Forum->id = $forum['Forum']['id']; $Forum->save(array('access_level_id' => $levelMap[$forum['Forum']['access_level_id']]), false); } } // Delete tables $this->out('Deleting old tables...'); return $this->_querySql('4.0'); }