From 07bf347a8c891915d26f56f0eac00a5fabf30cca Mon Sep 17 00:00:00 2001 From: ceeram Date: Wed, 6 Jan 2010 00:55:04 +0100 Subject: [PATCH 1/3] moved lock() to Core.Lockable --- app_model.php | 45 +------ infinitas/core/models/behaviors/lockable.php | 132 ++++++++++--------- 2 files changed, 72 insertions(+), 105 deletions(-) diff --git a/app_model.php b/app_model.php index 6cae27d31..0c7be117c 100644 --- a/app_model.php +++ b/app_model.php @@ -23,53 +23,10 @@ class AppModel extends Model var $useDbConfig = 'default'; var $actsAs = array( - 'Containable', + 'Containable', 'Core.Lockable' // 'Core.Logable' some wierd issues ); - function lock( $fields = null, $id = null ) - { - $old_recursive = $this->recursive; - - $this->recursive = -1; - $data = parent::read( array( 'locked', 'locked_by', 'id' ), $id ); - - $this->Session = new CakeSession(); - - if ( $data[$this->name]['locked'] && $data[$this->name]['locked_by'] != $this->Session->read( 'Auth.User.id' ) ) - { - return false; - } - - $data[$this->name]['locked'] = 1; - $data[$this->name]['locked_by'] = $this->Session->read( 'Auth.User.id' ); - $data[$this->name]['locked_since'] = date( 'Y-m-d H:i:s' ); - - parent::save( $data, array( 'validation' => false ) ); - - $this->recursive = $old_recursive; - $data = $this->read( $fields, $id ); - return $data; - } - - /** - * For unlocking records. - * - * sets the lock to false and sets the date and person to null - * - * @param array $data - * @param array $options - * @return {@see Model::save} - */ - function save( $data = null, $validate = true, $fieldList = array() ) - { - $data[$this->name]['locked'] = 0; - $data[$this->name]['locked_by'] = null; - $data[$this->name]['locked_since'] = null; - - return parent::save( $data, $validate, $fieldList ); - } - function find( $conditions = null, $fields = array(), $order = null, $recursive = null ) { switch( $conditions ) diff --git a/infinitas/core/models/behaviors/lockable.php b/infinitas/core/models/behaviors/lockable.php index 95a157ab9..11e4fdfa6 100644 --- a/infinitas/core/models/behaviors/lockable.php +++ b/infinitas/core/models/behaviors/lockable.php @@ -1,68 +1,78 @@ array( + 'locked_by' => 'locked_by', + 'locked_since' => 'locked_since', + 'locked' => 'locked' + ) + ); - /** - * Initiate behavior for the model using specified settings. Available settings: - * - * - locked_by: int field name of the peson that locked the field - * - locked_since: datetime field name of the date the record was locked. - * - locked: bool field name of the locked status field. - * - * @param object $Model Model using the behaviour - * @param array $settings Settings to override for model. - * @access public - */ - function setup(&$Model, $settings = array()) - { - $default = array( - 'locked_by' => 'locked_by', - 'locked_since' => 'locked_since', - 'locked' => 'locked' - ); +/** + * @param object $Model Model using the behavior + * @param array $settings Settings to override for model. + * @access public + * @return void + */ + function setup(&$Model, $config = null) { + if (is_array($config)) { + $this->settings[$Model->alias] = array_merge($this->_defaults, $config); + } else { + $this->settings[$Model->alias] = $this->_defaults; + } + } - if ( !isset( $this->__settings[$Model->alias] ) ) - { - $this->__settings[$Model->alias] = $default; - } + function lock(&$Model, $fields = null, $id = null){ + $old_recursive = $Model->recursive; + $Model->recursive = -1; + $data = $Model->read(array('locked', 'locked_by', 'id'), $id); + $this->Session = new CakeSession(); + $user_id = $this->Session->read('Auth.User.id'); + if($data[$Model->name]['locked'] && $data[$Model->name]['locked_by'] != $user_id){ + return false; + } + $data[$Model->name] = array( + 'locked' => 1, + 'locked_by' => $user_id, + 'locked_since' => date('Y-m-d H:i:s') + ); + $Model->save($data, array('validation' => false )); + $Model->recursive = $old_recursive; + $data = $Model->read($fields, $id); + return $data; + } - $this->__settings[$Model->alias] = am( - $this->__settings[$Model->alias], - ife( is_array( $settings ), $settings, array() ) - ); - } - - function read( $fields, $id ) - { - pr( 'lockable read' ); - exit; - } - } + function beforeSave($Model){ + $Model->data[$Model->name]['locked'] = 0; + $Model->data[$Model->name]['locked_by'] = null; + $Model->data[$Model->name]['locked_since'] = null; + return true; + } +} ?> \ No newline at end of file From a84833c406658656185da454cffcc94554748ad1 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 6 Jan 2010 02:36:09 +0100 Subject: [PATCH 2/3] some more work on lockable behavior --- infinitas/core/models/behaviors/lockable.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/infinitas/core/models/behaviors/lockable.php b/infinitas/core/models/behaviors/lockable.php index 11e4fdfa6..b33c99dd5 100644 --- a/infinitas/core/models/behaviors/lockable.php +++ b/infinitas/core/models/behaviors/lockable.php @@ -51,18 +51,19 @@ function setup(&$Model, $config = null) { function lock(&$Model, $fields = null, $id = null){ $old_recursive = $Model->recursive; $Model->recursive = -1; - $data = $Model->read(array('locked', 'locked_by', 'id'), $id); + $data = $Model->read($this->_defaults['fields'], $id); $this->Session = new CakeSession(); $user_id = $this->Session->read('Auth.User.id'); if($data[$Model->name]['locked'] && $data[$Model->name]['locked_by'] != $user_id){ return false; } $data[$Model->name] = array( + 'id' => $id, 'locked' => 1, 'locked_by' => $user_id, 'locked_since' => date('Y-m-d H:i:s') ); - $Model->save($data, array('validation' => false )); + $Model->save($data, array('validate' => false, 'callbacks' => false)); $Model->recursive = $old_recursive; $data = $Model->read($fields, $id); return $data; From 706b55096a998223a12f86f9f65a7b78616a7698 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 6 Jan 2010 02:37:16 +0100 Subject: [PATCH 3/3] short open tag replaced by php open tag --- infinitas/blog/views/posts/admin_index.ctp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infinitas/blog/views/posts/admin_index.ctp b/infinitas/blog/views/posts/admin_index.ctp index 0acd9eba8..468dd5dbd 100644 --- a/infinitas/blog/views/posts/admin_index.ctp +++ b/infinitas/blog/views/posts/admin_index.ctp @@ -76,7 +76,7 @@ ?> -