Beispiel #1
0
 /**
  * Creates a new ARO/ACO node
  *
  * @param int $linkId
  * @param mixed $parentId
  * @param string $alias
  * @return boolean True on success, false on fail
  * @access public
  */
 function create($linkId = 0, $parentId = null, $alias = '')
 {
     parent::create();
     if (strtolower(get_class($this)) == "aclnode") {
         trigger_error("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", E_USER_ERROR);
         return null;
     }
     extract($this->__dataVars());
     if ($parentId == null || $parentId === 0) {
         $parent = $this->find(null, 'MAX(rght) as rght', null, -1);
         $parent['lft'] = $parent[0]['rght'];
         if ($parent[0]['rght'] == null || !$parent[0]['rght']) {
             $parent['lft'] = 0;
         }
     } else {
         $parent = $this->find($this->_resolveID($parentId), null, null, 0);
         if ($parent == null || count($parent) == 0) {
             trigger_error("Null parent in {$class}::create()", E_USER_WARNING);
             return null;
         }
         $parent = $parent[$class];
         $this->_syncTable(1, $parent['lft'], $parent['lft']);
     }
     $return = $this->save(array($class => array($secondary_id => $linkId, 'alias' => $alias, 'lft' => $parent['lft'] + 1, 'rght' => $parent['lft'] + 2)));
     $this->id = $this->getLastInsertID();
     return $return;
 }
Beispiel #2
0
 function create($data = array(), $filterKey = false)
 {
     parent::create($data, $filterKey);
     if (empty($this->data[$this->name]['activity_id'])) {
         $default_activity = $this->Activity->default_value('ACTI');
         if (!empty($default_activity)) {
             $this->set('activity_id', $default_activity['Activity']['id']);
         }
     }
     return $this->data;
 }
function sess_write($key, $value)
{
    // If the client doesn't have a session, and one isn't being created ($value), do nothing.
    if (empty($_COOKIE[session_name()]) && empty($value)) {
        return TRUE;
    }
    $Controller = getInstance()->Controller;
    $Session = new AppModel('Session');
    $result = $Session->findById($key);
    $user_id = 0;
    if (isset($Controller->User->user['id'])) {
        $user_id = $Controller->User->user['id'];
    }
    if (isset($result['Session']['id'])) {
        $Session->save(array('hostname' => $_SERVER['REMOTE_ADDR'], 'session' => $value), array('id' => $key));
    } else {
        $Session->create(array('id' => $key, 'hostname' => $_SERVER['REMOTE_ADDR'], 'session' => $value));
    }
    return TRUE;
}
Beispiel #4
0
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * 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.
  *
  * @since 1.2
  * @TODO Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
  * @param AppModel $model
  * @param string $mode parent or tree
  * @return boolean True on success, false on failure
  * @access public
  */
 function recover(&$model, $mode = 'parent')
 {
     extract($this->settings[$model->name]);
     $model->recursive = -1;
     if ($mode == 'parent') {
         $count = 1;
         foreach ($model->findAll($scope, array($model->primaryKey)) as $Array) {
             $model->{$model->primaryKey} = $Array[$model->name][$model->primaryKey];
             $lft = $count++;
             $rght = $count++;
             $model->save(array($left => $lft, $right => $rght));
         }
         foreach ($model->findAll($scope, array($model->primaryKey, $parent)) as $Array) {
             $model->create();
             $model->id = $Array[$model->name][$model->primaryKey];
             $this->_set_parent($model, $Array[$model->name][$parent], true);
         }
     } else {
         foreach ($model->findAll($scope, array($model->primaryKey, $parent)) as $Array) {
             $path = $this->get_path($model, $Array[$model->name][$model->primaryKey]);
             if ($path == null || count($path) < 2) {
                 $parent_id = null;
             } else {
                 $parent_id = $path[count($path) - 2][$model->name][$model->primaryKey];
             }
             // Sidestep beforeSave
             $model->updateAll(array($parent => $parent_id), array($model->primaryKey => $Array[$model->name][$model->primaryKey]));
         }
     }
 }
Beispiel #6
0
 /**
  * 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.
  *
  * @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
  * @param AppModel $model
  * @param string $mode parent or tree
  * @return boolean true on success, false on failure
  * @access public
  */
 function recover(&$model, $mode = 'parent')
 {
     extract($this->settings[$model->alias]);
     $model->recursive = -1;
     if ($mode == 'parent') {
         $count = 1;
         foreach ($model->find('all', array('conditions' => $scope, 'fields' => array($model->primaryKey), 'order' => $left)) as $array) {
             $model->{$model->primaryKey} = $array[$model->alias][$model->primaryKey];
             $lft = $count++;
             $rght = $count++;
             $model->save(array($left => $lft, $right => $rght));
         }
         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], true);
         }
     } else {
         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 => $parentId), array($model->escapeField() => $array[$model->alias][$model->primaryKey]));
         }
     }
 }
Beispiel #7
0
 /**
  * resetSlugs method
  *
  * Regenerate all slugs. On large dbs this can take more than 30 seconds - a time limit is set to allow a minimum
  * 100 updates per second as a preventative measure.
  *
  * @param AppModel $model
  * @param array $conditions
  * @param int $recursive
  * @return bool true on success false otherwise
  * @access public
  */
 function resetSlugs(&$model, $conditions = array(), $recursive = -1)
 {
     extract($this->settings[$model->alias]);
     if (!$model->hasField($slugField)) {
         return false;
     }
     $fields = array_merge((array) $model->primaryKey, $label);
     $sort = $model->displayField . ' ASC';
     $count = $model->find('count');
     set_time_limit(max(30, $count / 100));
     foreach ($model->find('all', compact('conditions', 'fields', 'sort', 'recursive')) as $row) {
         $model->create();
         $model->save($row);
     }
     return true;
 }
Beispiel #8
0
 public function admin_add()
 {
     if ($this->data != null) {
         // create
         $this->Model = AppModel::create(Url::$data['modelName'], $this->data);
         // save & redirect?
         if ($this->Model->save()) {
             $this->redirect(DS . ADMIN_ROUTE . DS . Url::$data['controller'], false);
         }
     } else {
         // pre-data?
         $pre_data = $this->filter->field ? array($this->filter->field => $this->filter->value) : null;
         // create empty
         $this->Model = AppModel::create(Url::$data['modelName'], $pre_data);
     }
     $this->assign('item', $this->Model);
 }
Beispiel #9
0
 public function createLog($userid, $entity, $action, $incident, $entidade_id)
 {
     parent::create(array('usuario_id' => $userid, 'tx_entidade' => $entity, 'tx_evento' => $action, 'tx_ocorrencia' => $incident, 'entidade_id' => $entidade_id));
     parent::save();
 }
 /**
  * Inserts an item on a certain position
  *
  * @param AppModel $model
  * @param $position
  * @return boolean
  */
 private function __insertAtPosition($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;
 }
Beispiel #11
0
 /**
  * resetSlugs method
  *
  * Regenerate all slugs. On large dbs this can take more than 30 seconds - a time limit is set to allow a minimum
  * 100 updates per second as a preventative measure.
  *
  * @param AppModel $Model
  * @param array $conditions
  * @param int $recursive
  * @return bool true on success false otherwise
  * @access public
  */
 public function resetSlugs(&$Model, $params = array())
 {
     $recursive = -1;
     extract($this->settings[$Model->alias]);
     if ($notices && !$Model->hasField($slugField)) {
         return false;
     }
     $defaults = array('page' => 1, 'limit' => 100, 'fields' => array_merge(array($Model->primaryKey, $slugField), $label), 'order' => $Model->displayField . ' ASC', 'conditions' => array(), 'recursive' => $recursive);
     $params = array_merge($defaults, $params);
     $count = $Model->find('count', compact('conditions'));
     $max = ini_get('max_execution_time');
     if ($max) {
         set_time_limit(max($max, $count / 100));
     }
     while ($rows = $Model->find('all', $params)) {
         foreach ($rows as $row) {
             $Model->create();
             $Model->save($row, true, array($slugField));
         }
         $params['page']++;
     }
     return true;
 }
 /**
  *
  * 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;
 }
 /**
  * Updates the counter caches of each node in the parentIds property. Called
  * after save or after deletion of a node.
  *
  * @param AppModel $model The model object that the behavior is attached to
  * @access protected
  */
 function _updateCounterCache(&$model)
 {
     // Foreach item in the parentIds property array...
     foreach ($this->_parentIds[$model->alias] as $k => $parentId) {
         // Reset the $model
         $model->create();
         // Initialise the data array for saving
         $data = array($model->alias => array($model->primaryKey => $parentId));
         // Set the child count of the current node
         if ($this->settings[$model->alias]['child_count']) {
             $data[$model->alias][$this->settings[$model->alias]['child_count']] = $model->childCount($parentId);
         }
         // Set the direct child count of the current node
         if ($this->settings[$model->alias]['direct_child_count']) {
             $data[$model->alias][$this->settings[$model->alias]['direct_child_count']] = $model->childCount($parentId, true);
         }
         // Save the data with new counter caches
         $model->save($data, array('validate' => false, 'callbacks' => false));
         unset($this->_parentIds[$model->alias][$k]);
     }
 }