Last active
August 29, 2015 14:01
-
-
Save tsmsogn/bdc5e25f7c2b355b47c3 to your computer and use it in GitHub Desktop.
CakePHP: CartComponent
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 | |
App::uses('Component', 'Controller'); | |
class CartComponent extends Component { | |
/** | |
* @var array | |
*/ | |
public $components = array('Session'); | |
/** | |
* @var array | |
*/ | |
public $settings = array('name' => 'Cart'); | |
/** | |
* @param ComponentCollection $collection | |
* @param array $settings | |
*/ | |
public function __construct(ComponentCollection $collection, $settings = array()) { | |
$this->settings = Set::merge($this->settings, $settings); // @todo | |
} | |
/** | |
* @param Controller $controller | |
*/ | |
public function initialize(Controller $controller) { | |
$this->Controller = $controller; | |
} | |
/** | |
* @param $id | |
* @param int $quantity | |
* @param null $property | |
* @return bool | |
*/ | |
public function add($id, $quantity = 1, $property = null) { | |
// @todo Check weather valid id | |
if (!is_numeric($quantity)) { | |
$quantity = 1; | |
} | |
$quantity = abs($quantity); | |
if ($quantity == 0) { | |
return $this->remove($id); | |
} | |
if ($this->Controller->Session->check($this->settings['name'] . '.' . $id)) { | |
$quantity = $this->Controller->Session->read($this->settings['name'] . '.' . $id . '.quantity') + $quantity; | |
} | |
$this->Controller->Session->write($this->settings['name'] . '.' . $id . '.quantity', $quantity); | |
if (isset($property)) { | |
$this->Controller->Session->write($this->settings['name'] . '.' . $id . '.property', $property); | |
} | |
return true; | |
} | |
/** | |
* @param $id | |
* @param $quantity | |
* @param null $property | |
* @return bool | |
*/ | |
public function update($id, $quantity, $property = null) { | |
// @todo Check weather valid id | |
if (!$this->Controller->Session->check($this->settings['name'] . '.' . $id) || !is_numeric($quantity)) { | |
return false; | |
} | |
$quantity = abs($quantity); | |
if ($quantity == 0) { | |
return $this->remove($id); | |
} | |
$this->Controller->Session->write($this->settings['name'] . '.' . $id . '.quantity', $quantity); | |
if (isset($property)) { | |
$this->Controller->Session->write($this->settings['name'] . '.' . $id . '.property', $property); | |
} | |
return true; | |
} | |
/** | |
* @param $id | |
* @return bool | |
*/ | |
public function remove($id) { | |
if ($this->Controller->Session->check($this->settings['name'] . '.' . $id)) { | |
$this->Controller->Session->delete($this->settings['name'] . '.' . $id); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @param $id | |
* @return mixed | |
*/ | |
public function get($id) { | |
return $this->Controller->Session->read($this->settings['name'] . '.' . $id); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getAll() { | |
return $this->Controller->Session->read($this->settings['name']); | |
} | |
/** | |
* | |
*/ | |
public function clear() { | |
$this->Controller->Session->delete($this->settings['name']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment