Advertising.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ X7/VeXWQuCRAKRgSVih4AFdn1nKetUt8N1z6IAF35DA=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.1
  11. */
  12. /**
  13. * advertising table service class
  14. */
  15. namespace Ppb\Service;
  16. use Cube\Db\Expr,
  17. Ppb\Db\Table\Advertising as AdvertisingTable;
  18. class Advertising extends AbstractService
  19. {
  20. /**
  21. *
  22. * default advert sections - will return in case
  23. * the active theme doesnt have a valid "adverts.txt" file set
  24. *
  25. * @var array
  26. */
  27. protected $_defaultSections = array(
  28. 'header' => 'Site Header',
  29. 'footer' => 'Site Footer',
  30. );
  31. /**
  32. *
  33. * class constructor
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. $this->setTable(
  39. new AdvertisingTable());
  40. }
  41. /**
  42. *
  43. * create or update an advert
  44. *
  45. * @param array $data
  46. * @return $this
  47. */
  48. public function save($data)
  49. {
  50. $row = null;
  51. $data = $this->_prepareSaveData($data);
  52. if (array_key_exists('id', $data)) {
  53. $select = $this->_table->select()
  54. ->where("id = ?", $data['id']);
  55. unset($data['id']);
  56. $row = $this->_table->fetchRow($select);
  57. }
  58. if (array_key_exists('language', $data) && empty($data['language'])) {
  59. unset($data['language']);
  60. }
  61. if (count($row) > 0) {
  62. $data['updated_at'] = new Expr('now()');
  63. $this->_table->update($data, "id='{$row['id']}'");
  64. }
  65. else {
  66. $data['created_at'] = new Expr('now()');
  67. $this->_table->insert($data);
  68. }
  69. return $this;
  70. }
  71. /**
  72. *
  73. * save settings
  74. *
  75. * @param array $data
  76. * @return $this
  77. * @throws \InvalidArgumentException
  78. */
  79. public function saveSettings(array $data)
  80. {
  81. if (!isset($data['id'])) {
  82. throw new \InvalidArgumentException("The form must use an element with the name 'id'.");
  83. }
  84. $columns = array_keys($data);
  85. foreach ((array)$data['id'] as $key => $value) {
  86. $row = $this->_table->fetchRow("id='{$value}'");
  87. $input = array();
  88. foreach ($columns as $column) {
  89. if (is_array($data[$column]) && isset($data[$column][$key])) {
  90. $input[$column] = $data[$column][$key];
  91. }
  92. }
  93. $input = parent::_prepareSaveData($input);
  94. if (count($row) > 0) {
  95. $this->_table->update($input, "id='{$value}'");
  96. }
  97. }
  98. return $this;
  99. }
  100. /**
  101. *
  102. * get the available advertising sections for the currently active theme
  103. *
  104. * @return array
  105. */
  106. public function getSections()
  107. {
  108. $settings = $this->getSettings();
  109. $fileName = \Ppb\Utility::getPath('themes') . DIRECTORY_SEPARATOR . $settings['default_theme'] . DIRECTORY_SEPARATOR . 'adverts.txt';
  110. if (file_exists($fileName)) {
  111. $output = array();
  112. if (($handle = fopen($fileName, "r")) !== false) {
  113. while (($data = fgetcsv($handle)) !== false) {
  114. $output[$data[0]] = $data[1];
  115. }
  116. fclose($handle);
  117. }
  118. return $output;
  119. }
  120. return $this->_defaultSections;
  121. }
  122. /**
  123. *
  124. * delete an advert from the table
  125. *
  126. * @param integer $id the id of the advert
  127. * @return integer returns the number of affected rows
  128. */
  129. public function delete($id)
  130. {
  131. return $this->_table->delete(
  132. $this->_table->getAdapter()->quoteInto('id = ?', $id));
  133. }
  134. }