Skip to content

Commit 62f1bd3

Browse files
committed
init the first commit
0 parents  commit 62f1bd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+5631
-0
lines changed

Block/Customer/ListCustomer.php

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
}

Block/Customer/Recent.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* @Author: Ngo Quang Cuong
5+
* @Date: 2017-10-23 08:46:03
6+
* @Last Modified by: https://www.facebook.com/giaphugroupcom
7+
* @Last Modified time: 2017-10-23 10:22:37
8+
*/
9+
10+
namespace PHPCuong\ProductQuestionAndAnswer\Block\Customer;
11+
12+
/**
13+
* Recent Customer Questions Block
14+
*/
15+
class Recent extends \Magento\Framework\View\Element\Template
16+
{
17+
/**
18+
* Product questions collection
19+
*
20+
* @var \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\Collection
21+
*/
22+
protected $collection;
23+
24+
/**
25+
* Review resource model
26+
*
27+
* @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
28+
*/
29+
protected $questionColFactory;
30+
31+
/**
32+
* @var \Magento\Customer\Helper\Session\CurrentCustomer
33+
*/
34+
protected $currentCustomer;
35+
36+
/**
37+
* @var \PHPCuong\ProductQuestionAndAnswer\Model\Config\Source\FormatDateTime
38+
*/
39+
protected $formatDateTime;
40+
41+
/**
42+
* @param \Magento\Framework\View\Element\Template\Context $context
43+
* @param \PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\CollectionFactory $questionColFactory
44+
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
45+
* @param \PHPCuong\ProductQuestionAndAnswer\Model\Config\Source\FormatDateTime $formatDateTime
46+
* @param array $data
47+
*/
48+
public function __construct(
49+
\Magento\Framework\View\Element\Template\Context $context,
50+
\PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\CollectionFactory $questionColFactory,
51+
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
52+
\PHPCuong\ProductQuestionAndAnswer\Model\Config\Source\FormatDateTime $formatDateTime,
53+
array $data = []
54+
) {
55+
$this->questionColFactory = $questionColFactory;
56+
$this->currentCustomer = $currentCustomer;
57+
$this->formatDateTime = $formatDateTime;
58+
parent::__construct($context, $data);
59+
}
60+
61+
/**
62+
* Return collection of questions
63+
*
64+
* @return array|\PHPCuong\ProductQuestionAndAnswer\Model\ResourceModel\Question\Collection
65+
*/
66+
public function getQuestions()
67+
{
68+
if (!($customerId = $this->currentCustomer->getCustomerId())) {
69+
return [];
70+
}
71+
if (!$this->collection) {
72+
$this->collection = $this->questionColFactory->create();
73+
$this->collection
74+
->addStoreFilter($this->_storeManager->getStore()->getId())
75+
->addCustomerFilter($customerId)
76+
->setProductName()
77+
->setPageSize(5)
78+
->load();
79+
}
80+
return $this->collection;
81+
}
82+
83+
/**
84+
* Return question customer url
85+
*
86+
* @return string
87+
*/
88+
public function getAllQuestionsUrl()
89+
{
90+
return $this->getUrl('question/customer');
91+
}
92+
93+
/**
94+
* Truncate string
95+
*
96+
* @param string $value
97+
* @param int $length
98+
* @param string $etc
99+
* @param string &$remainder
100+
* @param bool $breakWords
101+
* @return string
102+
*/
103+
public function truncateString($value, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
104+
{
105+
return $this->filterManager->truncate(
106+
$value,
107+
['length' => $length, 'etc' => $etc, 'remainder' => $remainder, 'breakWords' => $breakWords]
108+
);
109+
}
110+
111+
/**
112+
* Get product link
113+
*
114+
* @param int $productId
115+
* @return string
116+
*/
117+
public function getProductUrl($productId)
118+
{
119+
return $this->getUrl('catalog/product/view/', ['id' => $productId]);
120+
}
121+
122+
/**
123+
* Format date in short format
124+
*
125+
* @param string $date
126+
* @return string
127+
*/
128+
public function dateFormat($date)
129+
{
130+
return $this->formatDate($date, \IntlDateFormatter::SHORT);
131+
}
132+
}

0 commit comments

Comments
 (0)