PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/src/Entity/Site/Site.php

http://github.com/concrete5/concrete5
PHP | 472 lines | 197 code | 50 blank | 225 comment | 12 complexity | e03727533b8e2b382c77743728004fdb MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Core\Entity\Site;
  3. use Concrete\Core\Attribute\Category\SiteCategory;
  4. use Concrete\Core\Attribute\Key\SiteKey;
  5. use Concrete\Core\Attribute\ObjectInterface;
  6. use Concrete\Core\Attribute\ObjectTrait;
  7. use Concrete\Core\Entity\Attribute\Value\SiteValue;
  8. use Concrete\Core\Permission\ObjectInterface as PermissionObjectInterface;
  9. use Concrete\Core\Permission\Response\SiteResponse;
  10. use Concrete\Core\Site\Config\Liaison;
  11. use Concrete\Core\Site\Tree\TreeInterface;
  12. use Concrete\Core\Support\Facade\Application;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. /**
  16. * @ORM\Entity
  17. * @ORM\Table(name="Sites")
  18. */
  19. class Site implements TreeInterface, ObjectInterface, PermissionObjectInterface
  20. {
  21. use ObjectTrait;
  22. /**
  23. * The site configuration repository.
  24. *
  25. * @var \Concrete\Core\Site\Config\Liaison
  26. */
  27. protected $siteConfig;
  28. /**
  29. * The site identifier.
  30. *
  31. * @ORM\Id @ORM\Column(type="integer", options={"unsigned":true})
  32. * @ORM\GeneratedValue(strategy="AUTO")
  33. *
  34. * @var int
  35. */
  36. protected $siteID;
  37. /**
  38. * The ID of the theme.
  39. *
  40. * @ORM\Column(type="integer", options={"unsigned":true})
  41. *
  42. * @var int
  43. */
  44. protected $pThemeID = 0;
  45. /**
  46. * Is this the default site?
  47. *
  48. * @ORM\Column(type="boolean")
  49. *
  50. * @var bool
  51. */
  52. protected $siteIsDefault = false;
  53. /**
  54. * The site handle.
  55. *
  56. * @ORM\Column(type="string", unique=true)
  57. *
  58. * @var string
  59. */
  60. protected $siteHandle;
  61. /**
  62. * The language sections of this site.
  63. *
  64. * @ORM\OneToMany(targetEntity="Locale", cascade={"all"}, mappedBy="site")
  65. * @ORM\JoinColumn(name="siteLocaleID", referencedColumnName="siteLocaleID")
  66. *
  67. * @var \Concrete\Core\Entity\Site\Locale[]|\Doctrine\Common\Collections\ArrayCollection
  68. **/
  69. protected $locales;
  70. /**
  71. * The site type.
  72. *
  73. * @ORM\ManyToOne(targetEntity="Type", inversedBy="sites")
  74. * @ORM\JoinColumn(name="siteTypeID", referencedColumnName="siteTypeID")
  75. *
  76. * @var \Concrete\Core\Entity\Site\Type|null
  77. */
  78. protected $type;
  79. /**
  80. * Initialize the instance.
  81. *
  82. * @param \Concrete\Core\Config\Repository\Repository $appConfigRepository The site configuration repository
  83. */
  84. public function __construct($appConfigRepository)
  85. {
  86. $this->updateSiteConfigRepository($appConfigRepository);
  87. $this->locales = new ArrayCollection();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. *
  92. * @see \Concrete\Core\Permission\ObjectInterface::getPermissionObjectIdentifier()
  93. *
  94. * @return int
  95. */
  96. public function getPermissionObjectIdentifier()
  97. {
  98. return $this->siteID;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. *
  103. * @see \Concrete\Core\Permission\ObjectInterface::getPermissionResponseClassName()
  104. *
  105. * @return string
  106. */
  107. public function getPermissionResponseClassName()
  108. {
  109. return SiteResponse::class;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. *
  114. * @see \Concrete\Core\Permission\ObjectInterface::getPermissionAssignmentClassName()
  115. *
  116. * @return false
  117. */
  118. public function getPermissionAssignmentClassName()
  119. {
  120. return false;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. *
  125. * @see \Concrete\Core\Permission\ObjectInterface::getPermissionObjectKeyCategoryHandle()
  126. *
  127. * @return false
  128. */
  129. public function getPermissionObjectKeyCategoryHandle()
  130. {
  131. return false;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. *
  136. * @see \Concrete\Core\Attribute\ObjectInterface::getObjectAttributeCategory()
  137. *
  138. * @return \Concrete\Core\Attribute\Category\SiteCategory
  139. */
  140. public function getObjectAttributeCategory()
  141. {
  142. $app = Application::getFacadeApplication();
  143. return $app->make(SiteCategory::class);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. *
  148. * @see \Concrete\Core\Attribute\ObjectInterface::getAttributeValueObject()
  149. *
  150. * @return \Concrete\Core\Entity\Attribute\Value\SiteValue|null
  151. */
  152. public function getAttributeValueObject($ak, $createIfNotExists = false)
  153. {
  154. $result = null;
  155. if (!is_object($ak)) {
  156. $ak = SiteKey::getAttributeKeyByHandle($ak);
  157. }
  158. if ($ak !== null) {
  159. $result = $this->getObjectAttributeCategory()->getAttributeValue($ak, $this);
  160. if ($result === null && $createIfNotExists) {
  161. $result = new SiteValue();
  162. $result->setSite($this);
  163. $result->setAttributeKey($ak);
  164. }
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Set the site configuration repository.
  170. *
  171. * @param \Concrete\Core\Config\Repository\Repository $appConfigRepository
  172. */
  173. public function updateSiteConfigRepository($appConfigRepository)
  174. {
  175. $this->siteConfig = new Liaison($appConfigRepository, $this);
  176. }
  177. /**
  178. * Get the site configuration repository.
  179. *
  180. * @return \Concrete\Core\Site\Config\Liaison
  181. */
  182. public function getConfigRepository()
  183. {
  184. if (!$this->siteConfig) {
  185. $app = Application::getFacadeApplication();
  186. $this->updateSiteConfigRepository($app->make('config'), $this);
  187. }
  188. return $this->siteConfig;
  189. }
  190. /**
  191. * Get the site type object.
  192. *
  193. * @return \Concrete\Core\Entity\Site\Type|null
  194. */
  195. public function getType()
  196. {
  197. return $this->type;
  198. }
  199. /**
  200. * Set the site type object.
  201. *
  202. * @param \Concrete\Core\Entity\Site\Type|null $type
  203. */
  204. public function setType($type)
  205. {
  206. $this->type = $type;
  207. }
  208. /**
  209. * Get the language sections of this site.
  210. *
  211. * @return \Concrete\Core\Entity\Site\Locale[]|\Doctrine\Common\Collections\ArrayCollection
  212. */
  213. public function getLocales()
  214. {
  215. return $this->locales;
  216. }
  217. /**
  218. * Set the language sections of this site.
  219. *
  220. * @param \Concrete\Core\Entity\Site\Locale[]|\Doctrine\Common\Collections\ArrayCollection $locales
  221. */
  222. public function setLocales($locales)
  223. {
  224. $this->locales = $locales;
  225. }
  226. /**
  227. * Get the ID of the home page.
  228. *
  229. * @return int|null
  230. */
  231. public function getSiteHomePageID()
  232. {
  233. $tree = $this->getSiteTreeObject();
  234. return $tree === null ? null : $tree->getSiteHomePageID();
  235. }
  236. /**
  237. * {@inheritdoc}
  238. *
  239. * @see \Concrete\Core\Site\Tree\TreeInterface::getSiteTreeID()
  240. */
  241. public function getSiteTreeID()
  242. {
  243. $tree = $this->getSiteTreeObject();
  244. return $tree === null ? null : $tree->getSiteTreeID();
  245. }
  246. /**
  247. * Get the default locale (if set).
  248. *
  249. * @return \Concrete\Core\Entity\Site\Locale|null
  250. */
  251. public function getDefaultLocale()
  252. {
  253. foreach ($this->locales as $locale) {
  254. if ($locale->getIsDefault()) {
  255. return $locale;
  256. }
  257. }
  258. }
  259. /**
  260. * {@inheritdoc}
  261. *
  262. * @see \Concrete\Core\Site\Tree\TreeInterface::getSiteTreeObject()
  263. */
  264. public function getSiteTreeObject()
  265. {
  266. $locale = $this->getDefaultLocale();
  267. if ($locale === null) {
  268. $locales = $this->getLocales()->toArray();
  269. $locale = array_shift($locales);
  270. }
  271. return $locale === null ? null : $locale->getSiteTree();
  272. }
  273. /**
  274. * Get the home page of the default language.
  275. *
  276. * @param string|int $version 'ACTIVE', 'RECENT' or a specific page version ID
  277. *
  278. * @return \Concrete\Core\Page\Page|null
  279. */
  280. public function getSiteHomePageObject($version = 'RECENT')
  281. {
  282. $tree = $this->getSiteTreeObject();
  283. return $tree === null ? null : $tree->getSiteHomePageObject($version);
  284. }
  285. /**
  286. * Get the ID of the site.
  287. *
  288. * @return int
  289. */
  290. public function getSiteID()
  291. {
  292. return $this->siteID;
  293. }
  294. /**
  295. * Get the handle of the site.
  296. *
  297. * @return string
  298. */
  299. public function getSiteHandle()
  300. {
  301. return $this->siteHandle;
  302. }
  303. /**
  304. * Set the handle of the site.
  305. *
  306. * @param string $siteHandle
  307. */
  308. public function setSiteHandle($siteHandle)
  309. {
  310. $this->siteHandle = $siteHandle;
  311. }
  312. /**
  313. * Is this the default site?
  314. *
  315. * @return bool
  316. */
  317. public function isDefault()
  318. {
  319. return $this->siteIsDefault;
  320. }
  321. /**
  322. * Is this the default site?
  323. *
  324. * @param bool $siteIsDefault
  325. */
  326. public function setIsDefault($siteIsDefault)
  327. {
  328. $this->siteIsDefault = (bool) $siteIsDefault;
  329. }
  330. /**
  331. * Get the name of the site.
  332. *
  333. * @return string|mixed
  334. */
  335. public function getSiteName()
  336. {
  337. return $this->getConfigRepository()->get('name');
  338. }
  339. /**
  340. * Set the name of the site.
  341. *
  342. * @param string|mixed $name
  343. *
  344. * @return bool returns true if the name has been correctly set, false otherwise
  345. */
  346. public function setSiteName($name)
  347. {
  348. return $this->getConfigRepository()->save('name', $name);
  349. }
  350. /**
  351. * Get the main site canonical URL.
  352. *
  353. * @return string empty string if it's not set
  354. */
  355. public function getSiteCanonicalURL()
  356. {
  357. return (string) $this->getConfigRepository()->get('seo.canonical_url');
  358. }
  359. /**
  360. * Get the alternative site canonical URL.
  361. *
  362. * @return string empty string if it's not set
  363. */
  364. public function getSiteAlternativeCanonicalURL()
  365. {
  366. return (string) $this->getConfigRepository()->get('seo.canonical_url_alternative');
  367. }
  368. /**
  369. * Get the HTTPS site canonical URL (it may be the main or the alternative canonical URL).
  370. *
  371. * @return string empty string if it's not set
  372. */
  373. public function getSiteSSLCanonicalURL()
  374. {
  375. $result = '';
  376. $url = $this->getSiteCanonicalURL();
  377. if (stripos($url, 'https:') === 0) {
  378. $result = $url;
  379. } else {
  380. $url = $this->getSiteAlternativeCanonicalURL();
  381. if (stripos($url, 'https:') === 0) {
  382. $result = $url;
  383. }
  384. }
  385. return $result;
  386. }
  387. /**
  388. * Get the site time zone identifier.
  389. *
  390. * @return string
  391. */
  392. public function getTimezone()
  393. {
  394. $timezone = null;
  395. $config = $this->getConfigRepository();
  396. if ($config) {
  397. $timezone = $config->get('timezone');
  398. }
  399. if (!$timezone) {
  400. $timezone = date_default_timezone_get();
  401. }
  402. return $timezone;
  403. }
  404. /**
  405. * Get the ID of the theme.
  406. *
  407. * @return int
  408. */
  409. public function getThemeID()
  410. {
  411. return $this->pThemeID;
  412. }
  413. /**
  414. * Set the ID of the theme.
  415. *
  416. * @param int $pThemeID
  417. */
  418. public function setThemeID($pThemeID)
  419. {
  420. $this->pThemeID = $pThemeID;
  421. }
  422. }