PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/streams_core/src/Pyro/Module/Streams/Entry/EntryPresenter.php

https://github.com/asalem/pyrocms
PHP | 266 lines | 129 code | 45 blank | 92 comment | 5 complexity | 02039dfbed6a2b61b9b3652bac89d77f MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, CC0-1.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT
  1. <?php namespace Pyro\Module\Streams\Entry;
  2. use Carbon\Carbon;
  3. use Illuminate\Support\Str;
  4. use Pyro\Module\Users\Model\User;
  5. use Pyro\Support\Presenter;
  6. class EntryPresenter extends Presenter
  7. {
  8. /**
  9. * Entry view options
  10. *
  11. * @var EntryViewOptions
  12. */
  13. protected $entryViewOptions;
  14. /**
  15. * Appends to array
  16. *
  17. * @var array
  18. */
  19. protected $appends = array(
  20. 'createdByUser',
  21. 'linkEdit',
  22. 'linkDetails',
  23. );
  24. /**
  25. * Create a new EntryPresenter instance
  26. *
  27. * @param EntryModel $model
  28. * @param EntryViewOptions $entryViewOptions
  29. */
  30. public function __construct(EntryModel $model, EntryViewOptions $entryViewOptions)
  31. {
  32. $this->resource = $model;
  33. $this->entryViewOptions = $entryViewOptions;
  34. }
  35. /**
  36. * Convert the object to an array
  37. *
  38. * @return array
  39. */
  40. public function toArray()
  41. {
  42. $resourceArray = $this->getResourceArray();
  43. foreach (array_keys($resourceArray) as $key) {
  44. $resourceArray[Str::snake($key)] = $this->getPresenterAttribute($key, $resourceArray);
  45. }
  46. return $resourceArray;
  47. }
  48. /**
  49. * Get presenter formatted attribute
  50. *
  51. * @param string $key
  52. * @return mixed
  53. */
  54. public function getPresenterAttribute($key, $resourceArray = null)
  55. {
  56. $method = Str::camel($key);
  57. $viewOption = $this->entryViewOptions->getBySlug($key);
  58. if ($viewOption and $callback = $viewOption->getCallback()) {
  59. return call_user_func($callback, $this->resource);
  60. } elseif ($viewOption and $template = $viewOption->getTemplate()) {
  61. return ci()->parser->parse_string(
  62. $template,
  63. array('entry' => $resourceArray ? : $this->getResourceArray()),
  64. true,
  65. false,
  66. array(
  67. 'stream' => $this->resource->getStreamSlug(),
  68. 'namespace' => $this->resource->getStreamNamespace()
  69. ),
  70. false
  71. );
  72. } elseif (method_exists($this, $method)) {
  73. return $this->{$method}();
  74. } elseif ($viewOption and $format = $viewOption->getFormat()) {
  75. $fieldTypeMethod = Str::studly($format) . 'Output';
  76. $method = 'get' . $fieldTypeMethod;
  77. if (method_exists($this, $method)) {
  78. return $this->{$method}($key);
  79. }
  80. if ($fieldType = $this->resource->getFieldType($key)) {
  81. return $fieldType->{$fieldTypeMethod}();
  82. }
  83. }
  84. return $this->resource->getAttribute($key);
  85. }
  86. /**
  87. * Get created by user
  88. *
  89. * @return string
  90. */
  91. public function createdByUser()
  92. {
  93. return $this->getUserOutput($this->resource->createdByUser);
  94. }
  95. /**
  96. * Get user output
  97. *
  98. * @param $value
  99. * @return string
  100. */
  101. protected function getUserOutput($value)
  102. {
  103. return ci()->parser->parse_string('<a href="admin/users/edit/{{ id }}">{{ username }}</a>', $value, true);
  104. }
  105. /**
  106. * Link edit
  107. *
  108. * @return string
  109. */
  110. public function linkEdit()
  111. {
  112. $url = $this->urlEdit();
  113. $string = $this->resource->getTitleColumnValue();
  114. return anchor($url, $string, 'class="link"');
  115. }
  116. /**
  117. * Url edit
  118. *
  119. * @return string
  120. */
  121. public function urlEdit()
  122. {
  123. $stream = $this->resource->getStream();
  124. $key = $this->resource->getKey();
  125. return site_url('admin/' . $stream->stream_namespace . '/' . $stream->stream_slug . '/edit/' . $key);
  126. }
  127. /**
  128. * Link details
  129. *
  130. * @return string
  131. */
  132. public function linkDetails()
  133. {
  134. $url = $this->urlDetails();
  135. $string = $this->resource->getTitleColumnValue();
  136. return anchor($url, $string, 'class="link"');
  137. }
  138. /**
  139. * Url details
  140. *
  141. * @return string
  142. */
  143. public function urlDetails()
  144. {
  145. $stream = $this->resource->getStream();
  146. $key = $this->resource->getKey();
  147. return site_url('admin/' . $stream->stream_namespace . '/' . $stream->stream_slug . '/details/' . $key);
  148. }
  149. /**
  150. * String output
  151. *
  152. * @param string
  153. * @return string
  154. */
  155. protected function getStringOutput($key = null)
  156. {
  157. if ($type = $this->resource->getFieldType($key)) {
  158. return $type->stringOutput();
  159. } elseif ($this->isDate($value = $this->resource->getAttribute($key))) {
  160. return $this->getDateOutput($value);
  161. } elseif ($this->isUser($value)) {
  162. return $this->getUserOutput($value);
  163. }
  164. return $value;
  165. }
  166. /**
  167. * Its datetime object?
  168. *
  169. * @param null $value
  170. * @return bool
  171. */
  172. protected function isDate($value = null)
  173. {
  174. return $value instanceof Carbon;
  175. }
  176. /**
  177. * Get date output
  178. *
  179. * @param $value
  180. * @return mixed
  181. */
  182. protected function getDateOutput($value)
  183. {
  184. return $value->format(\Settings::get('date_format'));
  185. }
  186. /**
  187. * Its a User model?
  188. *
  189. * @param null $value
  190. * @return bool
  191. */
  192. protected function isUser($value = null)
  193. {
  194. return $value instanceof User;
  195. }
  196. public function getResourceTitle()
  197. {
  198. if (method_exists($this->resource, 'getTitleColumnValue')) {
  199. return $this->resource->getTitleColumnValue();
  200. }
  201. return null;
  202. }
  203. /**
  204. * Get field type relationship item template
  205. *
  206. * @return string
  207. */
  208. public function getFieldTypeRelationshipItemTemplate()
  209. {
  210. return "'<div>' + item.{$this->resource->getTitleColumn()} + '</div>'";
  211. }
  212. /**
  213. * Get field type relationship option template
  214. *
  215. * @return string
  216. */
  217. public function getFieldTypeRelationshipOptionTemplate()
  218. {
  219. return "'<div>' + item.{$this->resource->getTitleColumn()} + '</div>'";
  220. }
  221. }