ListingsMedia.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ J+WpUccI8EfmMR4qFBWatlZ2WpQN1bJZzHI61BMnZZ0=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.4
  11. */
  12. /**
  13. * listings media table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\ListingsMedia as ListingsMediaTable,
  17. Cube\Db\Expr;
  18. class ListingsMedia extends AbstractService
  19. {
  20. /**
  21. * media types
  22. */
  23. const TYPE_IMAGE = 'image';
  24. const TYPE_VIDEO = 'video';
  25. const TYPE_DOWNLOAD = 'download';
  26. const TYPE_CSV = 'csv';
  27. /**
  28. *
  29. * available media types array
  30. *
  31. * @var array
  32. */
  33. protected static $_types = array(
  34. self::TYPE_IMAGE => 'Image',
  35. self::TYPE_VIDEO => 'Video',
  36. self::TYPE_DOWNLOAD => 'Download',
  37. self::TYPE_CSV => 'CSV',
  38. );
  39. /**
  40. *
  41. * class constructor
  42. */
  43. public function __construct()
  44. {
  45. parent::__construct();
  46. $this->setTable(
  47. new ListingsMediaTable());
  48. }
  49. /**
  50. *
  51. * get listings media types
  52. *
  53. * @return array
  54. */
  55. public static function getTypes()
  56. {
  57. return self::$_types;
  58. }
  59. /**
  60. *
  61. * the input array will save the data from any of the array keys matching the types array
  62. *
  63. * @param int $listingId
  64. * @param array $post
  65. *
  66. * @return $this
  67. */
  68. public function save($listingId, $post)
  69. {
  70. $ids = array(0);
  71. foreach (self::getTypes() as $type => $desc) {
  72. if (!empty($post[$type])) {
  73. $data = (is_array($post[$type])) ? $post[$type] : (array)\Ppb\Utility::unserialize($post[$type]);
  74. $ids = array_merge($ids, $this->_saveByType($listingId, $data, $type));
  75. }
  76. }
  77. // if (count($ids) > 0) {
  78. // delete all media that was removed by the edit process
  79. $this->fetchAll(
  80. $this->getTable()->select()
  81. ->where('listing_id = ?', $listingId)
  82. ->where('id NOT IN (?)', $ids))->delete();
  83. // }
  84. return $this;
  85. }
  86. /**
  87. *
  88. * save data in listings_media table
  89. *
  90. * @param int $listingId the id of the listing
  91. * @param array $data array data
  92. * @param string $type image, video, download
  93. *
  94. * @return array ids resulted from the insert/update queries
  95. */
  96. protected function _saveByType($listingId, array $data, $type = 'image')
  97. {
  98. $orderId = 0;
  99. $ids = array();
  100. $table = $this->getTable();
  101. // save media
  102. foreach ((array)$data as $value) {
  103. if (!empty($value)) {
  104. $row = $table->fetchRow(
  105. $table->select()
  106. ->where("listing_id = ?", $listingId)
  107. ->where("value = ?", $value)
  108. ->where('type = ?', $type));
  109. if (count($row) > 0) {
  110. $table->update(
  111. array('order_id' => $orderId++), "id = '{$row['id']}'");
  112. $ids[] = $row['id'];
  113. }
  114. else {
  115. $table->insert(array(
  116. 'value' => $value,
  117. 'listing_id' => $listingId,
  118. 'type' => $type,
  119. 'order_id' => $orderId++,
  120. 'created_at' => new Expr('now()'),
  121. ));
  122. $ids[] = $table->lastInsertId();
  123. }
  124. }
  125. }
  126. return $ids;
  127. }
  128. /**
  129. *
  130. * prepare custom field data for when saving to the table
  131. *
  132. * @param array $data
  133. *
  134. * @return array
  135. */
  136. protected function _prepareSaveData($data = array())
  137. {
  138. $data = parent::_prepareSaveData($data);
  139. return $data;
  140. }
  141. }