123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace Ppb\Service;
- use Cube\Db\Expr,
- Ppb\Db\Table;
- class Vouchers extends AbstractService
- {
-
- public function __construct()
- {
- parent::__construct();
- $this->setTable(
- new Table\Vouchers());
- }
-
- public function save($data)
- {
- $row = null;
- $data = $this->_prepareSaveData($data);
- if (array_key_exists('id', $data)) {
- $select = $this->_table->select()
- ->where("id = ?", $data['id']);
- unset($data['id']);
- $row = $this->_table->fetchRow($select);
- }
- if (count($row) > 0) {
- $data['updated_at'] = new Expr('now()');
- $this->_table->update($data, "id='{$row['id']}'");
- }
- else {
- $data['created_at'] = new Expr('now()');
- $this->_table->insert($data);
- }
- return $this;
- }
-
- public function findBy($voucherCode, $userId = null)
- {
- $select = $this->_table->select()
- ->where('code = ?', strval($voucherCode));
- if ($userId) {
- $select->where('user_id = ?', $userId);
- }
- else {
- $select->where('user_id is null');
- }
- return $this->_table->fetchRow($select);
- }
-
- protected function _prepareSaveData($data = array())
- {
- if ($data['uses_remaining'] == '') {
- $data['uses_remaining'] = null;
- }
- if ($data['expiration_date'] == '') {
- $data['expiration_date'] = null;
- }
- if (empty($data['assigned_listings'])) {
- $data['assigned_listings'] = '';
- }
- if (empty($data['id'])) {
- unset($data['id']);
- }
- return parent::_prepareSaveData($data);
- }
-
- public function delete($id, $userId = null)
- {
- $adapter = $this->_table->getAdapter();
- $where[] = $adapter->quoteInto('id = ?', $id);
- if ($userId !== null) {
- $where[] = $adapter->quoteInto('user_id = ?', $userId);
- }
- return $this->_table->delete($where);
- }
- }
|