123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace Ppb\Service;
- use Cube\Db\Expr,
- Ppb\Db\Table\Advertising as AdvertisingTable;
- class Advertising extends AbstractService
- {
-
- protected $_defaultSections = array(
- 'header' => 'Site Header',
- 'footer' => 'Site Footer',
- );
-
- public function __construct()
- {
- parent::__construct();
- $this->setTable(
- new AdvertisingTable());
- }
-
- 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 (array_key_exists('language', $data) && empty($data['language'])) {
- unset($data['language']);
- }
- 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 saveSettings(array $data)
- {
- if (!isset($data['id'])) {
- throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
- }
- $columns = array_keys($data);
- foreach ((array)$data['id'] as $key => $value) {
- $row = $this->_table->fetchRow("id='{$value}'");
- $input = array();
- foreach ($columns as $column) {
- if (is_array($data[$column]) && isset($data[$column][$key])) {
- $input[$column] = $data[$column][$key];
- }
- }
- $input = parent::_prepareSaveData($input);
- if (count($row) > 0) {
- $this->_table->update($input, "id='{$value}'");
- }
- }
- return $this;
- }
-
- public function getSections()
- {
- $settings = $this->getSettings();
- $fileName = \Ppb\Utility::getPath('themes') . DIRECTORY_SEPARATOR . $settings['default_theme'] . DIRECTORY_SEPARATOR . 'adverts.txt';
- if (file_exists($fileName)) {
- $output = array();
- if (($handle = fopen($fileName, "r")) !== false) {
- while (($data = fgetcsv($handle)) !== false) {
- $output[$data[0]] = $data[1];
- }
- fclose($handle);
- }
- return $output;
- }
- return $this->_defaultSections;
- }
-
- public function delete($id)
- {
- return $this->_table->delete(
- $this->_table->getAdapter()->quoteInto('id = ?', $id));
- }
- }
|