|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @Author: Ngo Quang Cuong |
| 5 | + * @Date: 2017-10-21 11:24:32 |
| 6 | + * @Last Modified by: https://www.facebook.com/giaphugroupcom |
| 7 | + * @Last Modified time: 2017-10-21 18:50:07 |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHPCuong\ProductQuestionAndAnswer\Block\Customer; |
| 11 | + |
| 12 | +use Magento\Customer\Api\AccountManagementInterface; |
| 13 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Customer Questions list block |
| 17 | + */ |
| 18 | +class ListCustomer extends \Magento\Customer\Block\Account\Dashboard |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Product questions collection |
| 22 | + * |
| 23 | + * @var \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\Collection |
| 24 | + */ |
| 25 | + protected $collection; |
| 26 | + |
| 27 | + /** |
| 28 | + * Question resource model |
| 29 | + * |
| 30 | + * @var \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\CollectionFactory |
| 31 | + */ |
| 32 | + protected $questionColFactory; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \Magento\Customer\Helper\Session\CurrentCustomer |
| 36 | + */ |
| 37 | + protected $currentCustomer; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Answer\CollectionFactory |
| 41 | + */ |
| 42 | + protected $answerColFactory; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var \PHPCuong\ProductQuestionAndAnswer\Model\Config\Source\FormatDateTime |
| 46 | + */ |
| 47 | + protected $formatDateTime; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var \PHPCuong\ProductQuestionAndAnswer\Model\UserType |
| 51 | + */ |
| 52 | + protected $userType; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var \Magento\Framework\DataObjectFactory |
| 56 | + */ |
| 57 | + protected $dataObjectFactory; |
| 58 | + |
| 59 | + /** |
| 60 | + * @param \Magento\Framework\View\Element\Template\Context $context |
| 61 | + * @param \Magento\Customer\Model\Session $customerSession |
| 62 | + * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory |
| 63 | + * @param CustomerRepositoryInterface $customerRepository |
| 64 | + * @param AccountManagementInterface $customerAccountManagement |
| 65 | + * @param \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\CollectionFactory $collectionFactory |
| 66 | + * @param \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Answer\CollectionFactory $answerColFactory |
| 67 | + * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer |
| 68 | + * @param \PHPCuong\ProductQuestionAndAnswer\Model\Config\Source\FormatDateTime $formatDateTime |
| 69 | + * @param \PHPCuong\ProductQuestionAndAnswer\Model\UserType $userType |
| 70 | + * @param \Magento\Framework\DataObjectFactory $dataObjectFactory |
| 71 | + * @param array $data |
| 72 | + */ |
| 73 | + public function __construct( |
| 74 | + \Magento\Framework\View\Element\Template\Context $context, |
| 75 | + \Magento\Customer\Model\Session $customerSession, |
| 76 | + \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory, |
| 77 | + CustomerRepositoryInterface $customerRepository, |
| 78 | + AccountManagementInterface $customerAccountManagement, |
| 79 | + \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\CollectionFactory $collectionFactory, |
| 80 | + \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Answer\CollectionFactory $answerColFactory, |
| 81 | + \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, |
| 82 | + \PHPCuong\ProductQuestionAndAnswer\Model\Config\Source\FormatDateTime $formatDateTime, |
| 83 | + \PHPCuong\ProductQuestionAndAnswer\Model\UserType $userType, |
| 84 | + \Magento\Framework\DataObjectFactory $dataObjectFactory, |
| 85 | + array $data = [] |
| 86 | + ) { |
| 87 | + $this->questionColFactory = $collectionFactory; |
| 88 | + $this->answerColFactory = $answerColFactory; |
| 89 | + $this->formatDateTime = $formatDateTime; |
| 90 | + $this->userType = $userType; |
| 91 | + $this->dataObjectFactory = $dataObjectFactory; |
| 92 | + parent::__construct( |
| 93 | + $context, |
| 94 | + $customerSession, |
| 95 | + $subscriberFactory, |
| 96 | + $customerRepository, |
| 97 | + $customerAccountManagement, |
| 98 | + $data |
| 99 | + ); |
| 100 | + $this->currentCustomer = $currentCustomer; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get html code for toolbar |
| 105 | + * |
| 106 | + * @return string |
| 107 | + */ |
| 108 | + public function getToolbarHtml() |
| 109 | + { |
| 110 | + return $this->getChildHtml('toolbar'); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Initializes toolbar |
| 115 | + * |
| 116 | + * @return \Magento\Framework\View\Element\AbstractBlock |
| 117 | + */ |
| 118 | + protected function _prepareLayout() |
| 119 | + { |
| 120 | + if ($this->getQuestions()) { |
| 121 | + $toolbar = $this->getLayout()->createBlock( |
| 122 | + 'Magento\Theme\Block\Html\Pager', |
| 123 | + 'customer_review_list.toolbar' |
| 124 | + )->setCollection( |
| 125 | + $this->getQuestions() |
| 126 | + ); |
| 127 | + |
| 128 | + $this->setChild('toolbar', $toolbar); |
| 129 | + } |
| 130 | + return parent::_prepareLayout(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get reviews |
| 135 | + * |
| 136 | + * @return bool|\PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\Collection |
| 137 | + */ |
| 138 | + public function getQuestions() |
| 139 | + { |
| 140 | + if (!($customerId = $this->currentCustomer->getCustomerId())) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + if (!$this->collection) { |
| 144 | + $this->collection = $this->questionColFactory->create(); |
| 145 | + $this->collection |
| 146 | + ->addStoreFilter($this->_storeManager->getStore()->getId()) |
| 147 | + ->addCustomerFilter($customerId) |
| 148 | + ->setProductName() |
| 149 | + ->setDateOrder(); |
| 150 | + } |
| 151 | + return $this->collection; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get product link |
| 156 | + * |
| 157 | + * @param int $productId |
| 158 | + * @return string |
| 159 | + */ |
| 160 | + public function getProductUrl($productId) |
| 161 | + { |
| 162 | + return $this->getUrl('catalog/product/view/', ['id' => $productId]); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Format date in short format |
| 167 | + * |
| 168 | + * @param string $date |
| 169 | + * @return string |
| 170 | + */ |
| 171 | + public function dateFormat($date) |
| 172 | + { |
| 173 | + return $this->formatDateTime->formatCreatedAt($date); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Get list of answers of the question |
| 178 | + * |
| 179 | + * @param int $questionId |
| 180 | + * @return array |
| 181 | + */ |
| 182 | + public function getAnswerList($questionId) |
| 183 | + { |
| 184 | + $collection = $this->answerColFactory->create()->addFieldToFilter( |
| 185 | + 'main_table.question_id', (int) $questionId |
| 186 | + )->addStatusFilter( |
| 187 | + \PHPCuong\ProductQuestionAndAnswer\Model\Status::STATUS_APPROVED |
| 188 | + )->addVisibilityFilter( |
| 189 | + \PHPCuong\ProductQuestionAndAnswer\Model\Visibility::VISIBILITY_VISIBLE |
| 190 | + ); |
| 191 | + |
| 192 | + $answers = []; |
| 193 | + foreach ($collection as $answer) { |
| 194 | + $answers[] = $this->getAnswerInfo($answer); |
| 195 | + } |
| 196 | + |
| 197 | + return $answers; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Retrieve the answer information |
| 202 | + * |
| 203 | + * @param \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Answer\CollectionFactory $answer |
| 204 | + * @return array |
| 205 | + */ |
| 206 | + protected function getAnswerInfo($answer) |
| 207 | + { |
| 208 | + /** @var \Magento\Framework\DataObjectFactory $dataObjectFactory */ |
| 209 | + return $this->dataObjectFactory->create() |
| 210 | + ->setId($answer->getAnswerId()) |
| 211 | + ->setContent(nl2br($answer->getAnswerDetail())) |
| 212 | + ->setAuthorName(ucwords(strtolower($answer->getAnswerAuthorName()))) |
| 213 | + ->setFirstCharacter(substr($answer->getAnswerAuthorName(), 0, 1)) |
| 214 | + ->setLikes($answer->getAnswerLikes()) |
| 215 | + ->setDislikes($answer->getAnswerDislikes()) |
| 216 | + ->setAnsweredBy($this->userType->getUserTypeText($answer->getAnswerUserTypeId())) |
| 217 | + ->setCreatedAt($this->formatDateTime->formatCreatedAt($answer->getAnswerCreatedAt())) |
| 218 | + ->getData(); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Get URL for likes and dislikes |
| 223 | + * |
| 224 | + * @return string |
| 225 | + */ |
| 226 | + public function getLikeDislikeUrl() |
| 227 | + { |
| 228 | + return $this->getUrl( |
| 229 | + 'question/product/likeDislike', |
| 230 | + [ |
| 231 | + '_secure' => $this->getRequest()->isSecure() |
| 232 | + ] |
| 233 | + ); |
| 234 | + } |
| 235 | +} |
0 commit comments