Skip to content

Commit

Permalink
Merge branch 'dev' of git://github.com/ceeram/infinitas into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 committed Jan 6, 2010
2 parents 45e0c8f + 706b550 commit 4d4cd06
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 106 deletions.
45 changes: 1 addition & 44 deletions app_model.php
Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion infinitas/blog/views/posts/admin_index.ctp
Expand Up @@ -76,7 +76,7 @@
?>
</td>
</tr>
<?
<?php
$i++;
}
?>
Expand Down
133 changes: 72 additions & 61 deletions infinitas/core/models/behaviors/lockable.php
@@ -1,68 +1,79 @@
<?php
/**
* Comment Template.
*
* @todo -c Implement .this needs to be sorted out.
*
* Copyright (c) 2009 Carl Sutton ( dogmatic69 )
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2009 Carl Sutton ( dogmatic69 )
* @link http://www.dogmatic.co.za
* @package sort
* @subpackage sort.comments
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.5a
*/
/**
* Comment Template.
*
* @todo -c Implement .this needs to be sorted out.
*
* Copyright (c) 2009 Carl Sutton ( dogmatic69 )
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2009 Carl Sutton ( dogmatic69 )
* @link http://www.dogmatic.co.za
* @package sort
* @subpackagesort.comments
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.5a
*/

class LockableBehavior extends ModelBehavior
{
class LockableBehavior extends ModelBehavior {

/**
* Contain settings indexed by model name.
*
* @var array
* @access private
*/
var $__settings = array();
/**
* Contain default settings.
*
* @var array
* @access protected
*/
var $_defaults = array(
'fields' => 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($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('validate' => false, 'callbacks' => 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;
}
}
?>

0 comments on commit 4d4cd06

Please sign in to comment.