PageRepository.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Webkul\CMS\Repositories;
  3. use Illuminate\Database\Eloquent\ModelNotFoundException;
  4. use Webkul\CMS\Models\PageTranslationProxy;
  5. use Webkul\Core\Eloquent\Repository;
  6. class PageRepository extends Repository
  7. {
  8. /**
  9. * Specify Model class name
  10. */
  11. public function model(): string
  12. {
  13. return 'Webkul\CMS\Contracts\Page';
  14. }
  15. /**
  16. * @return \Webkul\CMS\Contracts\Page
  17. */
  18. public function create(array $data)
  19. {
  20. $model = $this->getModel();
  21. foreach (core()->getAllLocales() as $locale) {
  22. foreach ($model->translatedAttributes as $attribute) {
  23. if (isset($data[$attribute])) {
  24. $data[$locale->code][$attribute] = $data[$attribute];
  25. }
  26. }
  27. $data[$locale->code]['html_content'] = str_replace('=&gt;', '=>', $data[$locale->code]['html_content']);
  28. }
  29. $page = parent::create($data);
  30. $page->channels()->sync($data['channels']);
  31. return $page;
  32. }
  33. /**
  34. * @param int $id
  35. * @return \Webkul\CMS\Contracts\Page
  36. */
  37. public function update(array $data, $id)
  38. {
  39. $page = $this->find($id);
  40. $locale = $data['locale'] ?? app()->getLocale();
  41. $data[$locale]['html_content'] = str_replace('=&gt;', '=>', $data[$locale]['html_content']);
  42. $page = parent::update($data, $id);
  43. $page->channels()->sync($data['channels']);
  44. return $page;
  45. }
  46. /**
  47. * Checks slug is unique or not based on locale
  48. *
  49. * @param int $id
  50. * @param string $urlKey
  51. * @return bool
  52. */
  53. public function isUrlKeyUnique($id, $urlKey)
  54. {
  55. $exists = PageTranslationProxy::modelClass()::where('cms_page_id', '<>', $id)
  56. ->where('url_key', $urlKey)
  57. ->limit(1)
  58. ->select(\DB::raw(1))
  59. ->exists();
  60. return ! $exists;
  61. }
  62. /**
  63. * Retrieve category from slug
  64. *
  65. * @param string $urlKey
  66. * @return \Webkul\CMS\Contracts\Page
  67. */
  68. public function findByUrlKey($urlKey)
  69. {
  70. return $this->model->whereTranslation('url_key', $urlKey)->first();
  71. }
  72. /**
  73. * Retrieve category from slug
  74. *
  75. * @param string $urlKey
  76. * @return \Webkul\CMS\Contracts\Page|\Exception
  77. */
  78. public function findByUrlKeyOrFail($urlKey)
  79. {
  80. $page = $this->model->whereTranslation('url_key', $urlKey)->first();
  81. if ($page) {
  82. return $page;
  83. }
  84. throw (new ModelNotFoundException)->setModel(
  85. get_class($this->model), $urlKey
  86. );
  87. }
  88. }