Author.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Model\Url;
  10. /**
  11. * Blog author model
  12. */
  13. class Author extends \Magento\Framework\Model\AbstractModel
  14. {
  15. /**
  16. * Initialize dependencies.
  17. *
  18. * @param \Magento\Framework\Model\Context $context
  19. * @param \Magento\Framework\Registry $registry
  20. * @param \Magefan\Blog\Model\Url $url
  21. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  22. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  23. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  24. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  25. * @param array $data
  26. */
  27. public function __construct(
  28. \Magento\Framework\Model\Context $context,
  29. \Magento\Framework\Registry $registry,
  30. Url $url,
  31. \Magento\Store\Model\StoreManagerInterface $storeManager,
  32. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  33. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  34. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  35. array $data = []
  36. ) {
  37. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  38. $this->_url = $url;
  39. }
  40. /**
  41. * Initialize user model
  42. *
  43. * @return void
  44. */
  45. protected function _construct()
  46. {
  47. $this->_init('Magefan\Blog\Model\ResourceModel\Author');
  48. }
  49. /**
  50. * Retrieve author name (used in identifier generation)
  51. * @return string | null
  52. */
  53. public function getTitle()
  54. {
  55. return $this->getName();
  56. }
  57. /**
  58. * Retrieve author identifier
  59. * @return string | null
  60. */
  61. public function getIdentifier()
  62. {
  63. return preg_replace(
  64. "/[^A-Za-z0-9\-]/",
  65. '',
  66. strtolower($this->getName('-'))
  67. );
  68. }
  69. /**
  70. * Check if author identifier exist
  71. * return author id if author exists
  72. *
  73. * @param string $identifier
  74. * @return int
  75. */
  76. public function checkIdentifier($identifier)
  77. {
  78. $authors = $this->getCollection();
  79. foreach($authors as $author) {
  80. if ($author->getIdentifier() == $identifier) {
  81. return $author->getId();
  82. }
  83. }
  84. return 0;
  85. }
  86. /**
  87. * Retrieve author url route path
  88. * @return string
  89. */
  90. public function getUrl()
  91. {
  92. return $this->_url->getUrlPath($this, URL::CONTROLLER_AUTHOR);
  93. }
  94. /**
  95. * Retrieve author url
  96. * @return string
  97. */
  98. public function getAuthorUrl()
  99. {
  100. return $this->_url->getUrl($this, URL::CONTROLLER_AUTHOR);
  101. }
  102. /**
  103. * Retrieve author name
  104. *
  105. * @param string $separator
  106. * @return string
  107. */
  108. public function getName($separator = ' ')
  109. {
  110. return $this->getFirstname() . $separator . $this->getLastname();
  111. }
  112. }