/src/ContentType/ContentType.php

https://gitlab.com/mktcode/widget-management-system · PHP · 203 lines · 98 code · 26 blank · 79 comment · 6 complexity · f5e5edf7d7b53e829574eff1f5814c82 MD5 · raw file

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: mkt
  5. * Date: 16.03.16
  6. * Time: 15:21
  7. */
  8. namespace App\ContentType;
  9. use App\Entity\ContentData;
  10. use App\Service\Config;
  11. use App\Service\Database;
  12. use App\Service\Routing;
  13. use App\Service\Translator;
  14. abstract class ContentType
  15. {
  16. /**
  17. * @var Config
  18. */
  19. protected $config;
  20. /**
  21. * @var Database
  22. */
  23. protected $database;
  24. /**
  25. * @var Routing
  26. */
  27. protected $routing;
  28. /**
  29. * @var Translator
  30. */
  31. protected $translator;
  32. /**
  33. * Returns the name of the extending class without the namespace.
  34. *
  35. * @return mixed
  36. */
  37. protected function getClass()
  38. {
  39. return array_pop(explode('\\', get_class($this)));
  40. }
  41. /**
  42. * Set commonly needed services.
  43. *
  44. * @param Config $config
  45. * @param Database $database
  46. * @param Routing $routing
  47. */
  48. public function setServices(Config $config, Database $database, Routing $routing, Translator $translator)
  49. {
  50. $this->config = $config;
  51. $this->database = $database;
  52. $this->routing = $routing;
  53. $this->translator = $translator;
  54. }
  55. /**
  56. * @param $key
  57. * @param $contentId
  58. * @return string
  59. */
  60. public function getData($key, $contentId)
  61. {
  62. $contentData = $this->database->getLoader('App\Entity\ContentData')->load(['content' => $contentId, 'dataKey' => $key]);
  63. if ($contentData) {
  64. return $contentData->getDataValue();
  65. }
  66. return '';
  67. }
  68. /**
  69. * Renders the final output of the content type.
  70. *
  71. * When overriding this method start with: $output = parent::render($contentId);
  72. *
  73. * @param $contentId
  74. * @return string
  75. */
  76. public function render($contentId)
  77. {
  78. $widgetfile = __DIR__ . '/' . $this->getClass() . '/widget.php';
  79. if (file_exists($widgetfile)) {
  80. $content = $this->database->getLoader('App\Entity\Content')->load(['id' => $contentId]);
  81. ob_start();
  82. include $widgetfile;
  83. $output = ob_get_clean();
  84. foreach ($this->config->config as $key => $value) {
  85. if (in_array($key, ['db_host', 'db_user', 'db_pass', 'db_name'])) continue;
  86. $output = str_replace('%%' . $key . '%%', $value, $output);
  87. }
  88. return $output;
  89. }
  90. return '';
  91. }
  92. /**
  93. * Renders the form for backend editing.
  94. *
  95. * @param null $contentId
  96. * @return string
  97. */
  98. public function form($contentId = null)
  99. {
  100. $formfile = __DIR__ . '/' . $this->getClass() . '/form.php';
  101. if (file_exists($formfile)) {
  102. $content = $this->database->getLoader('App\Entity\Content')->load(['id' => $contentId]);
  103. ob_start();
  104. include $formfile;
  105. return ob_get_clean();
  106. }
  107. return '';
  108. }
  109. /**
  110. * Returns the label for the button.
  111. *
  112. * @return string
  113. */
  114. public function getLabel()
  115. {
  116. return $this->getClass();
  117. }
  118. /**
  119. * Returns the icon class for the button.
  120. *
  121. * @return string
  122. */
  123. public function getIcon()
  124. {
  125. return '';
  126. }
  127. /**
  128. * Returns possible css classes for the button.
  129. *
  130. * @return string
  131. */
  132. public function getButtonClasses()
  133. {
  134. return '';
  135. }
  136. /**
  137. * Saves additional data.
  138. *
  139. * @param int $contentId
  140. * @param array $data
  141. * @return bool
  142. */
  143. public function save($contentId, $data = [])
  144. {
  145. $content = $this->database->getLoader('App\Entity\Content')->load(['id' => $contentId]);
  146. $contentData = $this->database->getLoader('App\Entity\ContentData')->loadAll(['content' => $contentId]);
  147. // delete current data
  148. foreach ($contentData as $item) {
  149. $this->database->em->remove($item);
  150. }
  151. $this->database->em->flush();
  152. // save new data
  153. foreach ($data as $key => $value) {
  154. if (in_array($key, ['title', 'submit_action'])) continue;
  155. $contentData = $this->database->getLoader('App\Entity\ContentData')->load(['content' => $contentId, 'dataKey' => $key]);
  156. if (!$contentData) {
  157. $contentData = new ContentData();
  158. $this->database->em->persist($contentData);
  159. $contentData->setContent($content);
  160. $contentData->setDataKey($key);
  161. }
  162. $contentData->setDataValue($value);
  163. }
  164. $this->database->em->flush();
  165. return true;
  166. }
  167. /**
  168. * Translates a string by key.
  169. *
  170. * @param $key
  171. * @return string
  172. */
  173. public function translate($key, $parameters = [])
  174. {
  175. return $this->translator->trans($key, $parameters);
  176. }
  177. }