AbstractManagement.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model;
  9. use Magefan\Blog\Api\ManagementInterface;
  10. /**
  11. * Abstract management model
  12. */
  13. abstract class AbstractManagement implements ManagementInterface
  14. {
  15. /**
  16. * @var Magento\Framework\Model\AbstractModel
  17. */
  18. protected $_itemFactory;
  19. /**
  20. * Create new item using data
  21. *
  22. * @param string $data
  23. * @return string || false
  24. */
  25. public function create($data)
  26. {
  27. try {
  28. $data = json_decode($data, true);
  29. $item = $this->_itemFactory->create();
  30. $item->setData($data)->save();
  31. return json_encode($item->getData());
  32. } catch (\Exception $e) {
  33. return false;
  34. }
  35. }
  36. /**
  37. * Update item using data
  38. *
  39. * @param int $id
  40. * @param string $data
  41. * @return string || false
  42. */
  43. public function update($id, $data)
  44. {
  45. try {
  46. $item = $this->_itemFactory->create();
  47. $item->load($id);
  48. if (!$item->getId()) {
  49. return false;
  50. }
  51. $data = json_decode($data, true);
  52. $item->addData($data)->save();
  53. return json_encode($item->getData());
  54. } catch (\Exception $e) {
  55. return false;
  56. }
  57. }
  58. /**
  59. * Delete item by id
  60. *
  61. * @param int $id
  62. * @return bool
  63. */
  64. public function delete($id)
  65. {
  66. try {
  67. $item = $this->_itemFactory->create();
  68. $item->load($id);
  69. if ($item->getId()) {
  70. $item->delete();
  71. return true;
  72. }
  73. return false;
  74. } catch (\Exception $e) {
  75. return false;
  76. }
  77. }
  78. /**
  79. * Get item by id
  80. *
  81. * @param int $id
  82. * @return bool
  83. */
  84. public function get($id)
  85. {
  86. try {
  87. $item = $this->_itemFactory->create();
  88. $item->load($id);
  89. if (!$item->getId()) {
  90. return false;
  91. }
  92. return json_encode($item->getData());
  93. } catch (\Exception $e) {
  94. return false;
  95. }
  96. }
  97. /**
  98. * Get item by id and store id, only if item published
  99. *
  100. * @param int $id
  101. * @param int $storeId
  102. * @return bool
  103. */
  104. public function view($id, $storeId)
  105. {
  106. try {
  107. $item = $this->_itemFactory->create();
  108. $item->load($id);
  109. if (!$item->isVisibleOnStore($storeId)) {
  110. return false;
  111. }
  112. $item->initDinamicData();
  113. return json_encode($item->getData());
  114. } catch (\Exception $e) {
  115. return false;
  116. }
  117. }
  118. }