Created
September 4, 2015 13:37
-
-
Save vgrish/877608675365944c1d80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class psAccount extends xPDOSimpleObject | |
{ | |
public $_validated = array(); | |
protected $isSetFields = false; | |
protected $closedFields = array('user', 'method', 'identifier', 'balance', 'incoming', 'outcoming', 'active'); | |
const BALANCE_SET = 'set'; | |
const BALANCE_PUT = 'in'; | |
const BALANCE_TAKE = 'out'; | |
const BALANCE_INITIATE = 'initiate'; | |
const IDENTIFIER_SET = 'set'; | |
const ACTIVE_SET = 'set'; | |
const TARGET_BALANCE = 'balance'; | |
const TARGET_IDENTIFIER = 'identifier'; | |
const TARGET_ACTIVE = 'active'; | |
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
public static function load(xPDO & $xpdo, $className, $criteria, $cacheFlag = true) | |
{ | |
/* @var $instance psAccount */ | |
$instance = parent::load($xpdo, 'psAccount', $criteria, $cacheFlag); | |
if (!is_object($instance) || !($instance instanceof $className)) { | |
if ((is_array($criteria) && !empty($criteria['user']) && (is_numeric($criteria['method']))) | |
) { | |
$user = (int)$criteria['user']; | |
$method = (int)$criteria['method']; | |
if ($xpdo->getCount('psClient', array('id' => $user))) { | |
$instance = $xpdo->newObject('psAccount'); | |
$instance->fromArray(array( | |
'user' => $user, | |
'method' => $method, | |
)); | |
$instance->save(); | |
} | |
} | |
} | |
return $instance; | |
} | |
/** | |
* @param string $k | |
* @param null $v | |
* @param string $vType | |
* @return bool | |
*/ | |
public function set($k, $v = null, $vType = '') | |
{ | |
$isNew = $this->isNew(); | |
$isSetFields = $this->isSetFields; | |
$closedFields = array_flip($this->closedFields); | |
if ($isNew) { | |
unset( | |
$closedFields['user'], | |
$closedFields['method'], | |
$closedFields['identifier'] | |
); | |
} | |
if (array_key_exists($k, $closedFields) && !$isSetFields) { | |
return false; | |
} | |
return parent::set($k, $v, $vType); | |
} | |
/** | |
* @param bool $cacheFlag | |
* @return bool | |
*/ | |
public function save($cacheFlag = false) | |
{ | |
$isNew = $this->isNew(); | |
if ($this->xpdo instanceof modX) { | |
$this->xpdo->invokeEvent('psOnAccountBeforeSave', array( | |
'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD, | |
'account' => &$this, | |
'cacheFlag' => $cacheFlag, | |
)); | |
} | |
$saved = parent:: save($cacheFlag); | |
if ($saved && $this->xpdo instanceof modX) { | |
$this->xpdo->invokeEvent('psOnAccountSave', array( | |
'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD, | |
'account' => &$this, | |
'cacheFlag' => $cacheFlag, | |
)); | |
} | |
return $saved; | |
} | |
public function validate(array $options = array()) | |
{ | |
$validate = false; | |
if (empty($this->_validated)) { | |
$validate = parent:: validate($options); | |
} | |
return $validate; | |
} | |
/** | |
* @param array $ancestors | |
* @return bool | |
*/ | |
public function remove(array $ancestors = array()) | |
{ | |
if ($this->xpdo instanceof modX) { | |
$this->xpdo->invokeEvent('psOnAccountBeforeRemove', array( | |
'account' => &$this, | |
'ancestors' => $ancestors, | |
)); | |
} | |
$removed = parent:: remove($ancestors); | |
if ($this->xpdo instanceof modX) { | |
$this->xpdo->invokeEvent('psOnAccountRemove', array( | |
'account' => &$this, | |
'ancestors' => $ancestors, | |
)); | |
} | |
return $removed; | |
} | |
/** @inheritdoc} */ | |
public function initiateAccount($sum = 0) | |
{ | |
$this->isSetFields = true; | |
$this->set('balance', $sum); | |
$this->set('incoming', 0); | |
$this->set('outcoming', 0); | |
$this->logAccount(self::TARGET_BALANCE, self::BALANCE_INITIATE, $sum); | |
return $this->save(); | |
} | |
/** @inheritdoc} */ | |
public function setIdentifier($identifier = '') | |
{ | |
$this->isSetFields = true; | |
$this->set('identifier', $identifier); | |
$this->logAccount(self::TARGET_IDENTIFIER, self::IDENTIFIER_SET, $identifier); | |
return $this->save(); | |
} | |
/** @inheritdoc} */ | |
public function setActive($active = false) | |
{ | |
$this->isSetFields = true; | |
$active = (boolean)$active; | |
$this->set('active', $active); | |
$this->logAccount(self::TARGET_ACTIVE, self::ACTIVE_SET, $active); | |
return $this->save(); | |
} | |
/** @inheritdoc} */ | |
public function setAccount($sum = 0) | |
{ | |
$this->isSetFields = true; | |
$this->set('balance', $sum); | |
$this->logAccount(self::TARGET_BALANCE, self::BALANCE_SET, $sum); | |
return $this->save(); | |
} | |
/** @inheritdoc} */ | |
public function putAccount($sum = 0) | |
{ | |
$this->isSetFields = true; | |
$sum = abs($sum); | |
$balance = $this->get('balance'); | |
$incoming = abs($this->get('incoming')); | |
$balance += $sum; | |
$incoming += $sum; | |
$this->set('balance', $balance); | |
$this->set('incoming', $incoming); | |
$this->logAccount(self::TARGET_BALANCE, self::BALANCE_PUT, $sum); | |
return $this->save(); | |
} | |
/** @inheritdoc} */ | |
public function takeAccount($sum = 0) | |
{ | |
$this->isSetFields = true; | |
$sum = abs($sum); | |
$balance = $this->get('balance'); | |
$outcoming = abs($this->get('outcoming')); | |
$balance -= $sum; | |
$outcoming += $sum; | |
$this->set('balance', $balance); | |
$this->set('outcoming', $outcoming); | |
$this->logAccount(self::TARGET_BALANCE, self::BALANCE_TAKE, $sum); | |
return $this->save(); | |
} | |
/** @inheritdoc} */ | |
public function getIdentifier($generate = false) | |
{ | |
$identifier = $this->get('identifier'); | |
if (empty($identifier) && $generate) { | |
$identifier = $this->generateIdentifier(); | |
} | |
return $identifier; | |
} | |
/** @inheritdoc} */ | |
public function generateIdentifier() | |
{ | |
$chars = str_split(self::CHARS); | |
$length = count($chars) - 1; | |
$identifier = array(); | |
for ($i = 0; $i < 4; $i++) { | |
$idx = $i + 1; | |
$identifier[$idx] = ''; | |
for ($i2 = 0; $i2 < 4; $i2++) { | |
$identifier[$idx] .= $chars[rand(0, $length)]; | |
} | |
} | |
$identifier = implode('-', $identifier); | |
return $identifier; | |
} | |
/** @inheritdoc} */ | |
public function logAccount($target = '', $action = '', $value = '') | |
{ | |
$this->Log = $this->xpdo->newObject('psAccountLog', array( | |
'target' => $target, | |
'action' => $action, | |
'value' => $value, | |
)); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment