Skip to content

Instantly share code, notes, and snippets.

@tareko
Created August 23, 2015 22:30
Show Gist options
  • Save tareko/a41859fe87117c8d9eb1 to your computer and use it in GitHub Desktop.
Save tareko/a41859fe87117c8d9eb1 to your computer and use it in GitHub Desktop.
Trade model saves even when TradesDetail model is absent.
<?php
App::uses('AppModel', 'Model');
/**
* Trade Model
*
* @property Users $Users
* @property Shifts $Shifts
*
* Status legend:
* 0 - Unprocessed
* 1 - In progress
* 2 - Complete
* 3 - Cancelled
*
* User_status legend:
* 0 - Unprocessed
* 1 - Email sent
* 2 - Accepted
* 3 - Rejected
*
*/
class Trade extends AppModel {
/**
* Search plugin initialization
*/
public $actsAs = array('Search.Searchable', 'Containable');
public $components = array('sendTradeRequest');
public $findMethods = array('tradeIndex' => true);
public $order = array('Trade.updated' => 'DESC');
public $filterArgs = array(
/* array('name' => 'month', 'type' => 'value', 'field' => 'MONTH(Shift.date)'),
array('name' => 'year', 'type' => 'value', 'field' => 'YEAR(Shift.date)'),
array('name' => 'location', 'type' => 'value', 'field' => 'ShiftsType.location_id'),
*/ );
/**
* Validation rules
*
* @var array
*/
//TODO: Add a validation rule so that a shift must actually belong to a user for him/her to
// trade it away - form security stuff
public $validate = array(
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Please select a proper user (numeric)',
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'notBlank' => array(
'rule' => array('notBlank'),
'message' => 'Please select a proper user (notBlank)',
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'shift_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Please select a proper shift (numeric)',
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'notBlank' => array(
'rule' => array('notBlank'),
'message' => 'Please select a proper shift (notBlank)',
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'checkDuplicate' => array(
'rule' => 'checkDuplicate',
'message' => 'This shift is already in the process of being traded! Please cancel the pre-existing trade before trying to trade this shift again'
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Shift' => array(
'className' => 'Shift',
'foreignKey' => 'shift_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
public $hasMany = array(
'TradesDetail' => array(
'className' => 'TradesDetail',
'foreignKey' => 'trade_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
public function getUnprocessedTrades($conditions = array()) {
return $this->find('all', array(
'recursive' => 3,
'contain' => array(
'Shift' => array(
'fields' => array(
'id',
'date'),
'ShiftsType' => array(
'fields' => array(
'times'),
'Location' => array(
'location')
)
),
'TradesDetail' => array(
'User' => array(
'fields' => array(
'id',
'name',
'email')
)
),
'User' => array(
'fields' => array(
'id',
'name',
'email'),
),
),
'conditions' => array_merge(
array('status' => 0),
$conditions),
));
}
/*
* Check duplicate trade and return true if duplicate found
*
*/
public function checkDuplicate($check) {
$trade = $this->find('first', array(
'fields' => array(
'Trade.user_id',
'Trade.shift_id',
'Trade.user_status',
'Trade.status'),
'conditions' => array(
'Trade.user_status <' => 2,
'Trade.status !=' => 2,
'Trade.user_id' => $this->data['Trade']['user_id'],
'Trade.shift_id' => $this->data['Trade' ]['shift_id']),
));
return ($trade ? false : true);
}
}
public function index() {
debug($this->request->data);
if ($this->request->isPost() && !empty($this->request->data)) {
if ($this->Trade->saveAll($this->request->data, array('deep' => true))) {
$this->Flash->success(__('The trade has been saved'));
} else {
$this->Flash->alert(__('The trade could not be saved. Please fix the errors below and try again.'));
}
}
$this->set('usersId', (isset($this->request->query['id']) ? $this->request->query['id'] : $this->_usersId()));
$this->set('groupList', $this->User->getGroupsForUser($this->_usersId(), array(), true, true));
$this->render();
}
<?php
App::uses('AppModel', 'Model');
/**
* TradesDetail Model
*
* @property User $User
* @property Trade $Trade
* @property User $User
* Status: 0 - Unprocessed
* 1 - Request sent; awaiting reply
* 2 - Accepted
* 3 - Rejected
*/
class TradesDetail extends AppModel {
public $actsAs = array('Search.Searchable', 'Containable');
public $order = array('updated' => 'DESC');
/**
* Display field
*
* @var string
*/
public $displayField = 'trade_id';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'trade_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'The linked trade must be known',
'allowEmpty' => false,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'A user being traded to is required',
'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'status' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'The format in which the status was submitted is incorrect',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Trade' => array(
'className' => 'Trade',
'foreignKey' => 'trade_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment