· 7 years ago · Sep 03, 2018, 01:36 PM
1class Data extends AbstractHelper
2{
3
4 /**
5 * Data constructor.
6 * @param MagentoFrameworkAppHelperContext $context
7 * @param MagentoFrameworkObjectManagerInterface $objectManager
8 * @param MagentoStoreModelStoreManagerInterface $storeManager
9 */
10 public function __construct(
11 Context $context,
12 ObjectManagerInterface $objectManager,
13 StoreManagerInterface $storeManager
14 )
15 {
16 parent::__construct($context, $objectManager, $storeManager);
17 }
18
19 public function createAutherizeSession($authentication_key, $device_id){
20
21 if($authentication_key !='' && $device_id !=''){
22 $authentication_key = '$9384598$-'.rand(1,999999999).'-'.$device_id;
23 $session_authentication_key = $authentication_key;
24 } else {
25 $session_authentication_key = 0;
26 }
27 return $session_authentication_key;
28 }
29
30 public function authenticate($authentication_key = null, $device_id = null){
31 $key = $device_id;
32 $objectManager = MagentoFrameworkAppObjectManager::getInstance();
33 $resource = $objectManager->create('MagentoFrameworkAppResourceConnection');
34 $connection = $resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
35
36 $query = $connection->query("select session_id from tbl_authorization where device_id='".$device_id."'");
37 $result = $query->fetch();
38 $session_authentication_key = $result['session_id'];
39
40 $session_data = $session_authentication_key;
41 $ex = explode(':', $session_data);
42 if(count($ex) == 2){
43 $new_authentication_key = $authentication_key . ':' . end($ex);
44 } else {
45 $new_authentication_key = $authentication_key;
46 }
47
48 if($new_authentication_key == $session_authentication_key){
49 $mes = 'true';
50 } else {
51 $mes = 'false';
52 }
53 return $mes;
54 }
55
56 public function convertJson($code=null, $data=null){
57 $message = $this->messageList($code);
58 if($data == ''){
59 $data = array();
60 }
61 if($code[0] == 2){
62 $code = 200;
63 }
64 header('Content-Type: application/json');
65 return json_encode(array("status"=>$code,"message"=>$message,"data"=>$data));
66
67 }
68
69 /* Message list function */
70
71 public function messageList($messId = null){
72 $message = array(
73 '4000' => 'Authorize to Access',
74 '4001' => 'Not authorize to Access',
75 '2000' => 'Success',
76 '0000' => 'Error',
77 '0001' => 'Invalid Parameter',
78 '0002' => 'No record found',
79 '0007' => 'Please try again',
80
81 '2001' => 'Password changed Successfully',
82 '0003' => 'New and confirm password not match',
83 '0004' => 'Old password incorrect',
84 '0006' => 'Password length must be greater or equal than 6.',
85
86 '2002' => 'User registered Successfully',
87 '0005' => 'Email id already exists. Try a different email id.',
88 '2019' => 'Updated Successfully',
89
90 '2005' => 'Login successfully',
91 '0008' => 'Invalid credentials. please try again!',
92 '2011' => 'Logout successfully',
93 '2056' => 'Review Saved Successfully',
94 '2017' => 'Products Detail',
95 '2018' => 'Package List',
96 '2028' => 'List',
97 '2029' => 'Details',
98 '2032' => 'Category List',
99 '2030' => 'Comment was successfully saved',
100 '2031' => 'Post Comments Details',
101 '3001' => 'Forum Topic',
102 '3002' => 'Forum Details',
103
104 '8001' => 'All City(State) List',
105 '8002' => 'All Country(State) List',
106 '2007' => 'Product has been added Successfully in a cart',
107 '2008' => 'Product has been deleted Successfully from cart',
108 '2012' => 'Product has been update Successfully in a cart.',
109 '2024' => 'Coupon Code Applied Successfully',
110
111 '2040' => 'List of Results',
112 '2009' => 'Newsletter subscription Successfully',
113 '2010' => 'Newsletter has been Successfully un-subscribed.',
114
115 '0009' => 'Address can not be more than 50 character.',
116 '2013' => 'Customer address saved Successfully',
117 '2014' => 'Address Deleted Successfully.',
118 '2015' => 'Order Canceled Successfully',
119 '2016' => 'Forgot password link send Successfully. Please check mail.',
120 '2017' => 'Thank you for your query. We will get back to you soon.',
121 '0010' => 'Email id not exists.Try again.',
122
123 );
124 return $message[$messId];
125 }
126
127
128}
129
130<?php
131
132namespace VendorModulenameControllerAuthorization;
133
134use MagentoFrameworkAppActionAction;
135use MagentoFrameworkAppActionContext;
136use MagentoFrameworkControllerResultForwardFactory;
137use MagentoFrameworkViewResultPageFactory;
138
139class Index extends Action
140{
141 /**
142 * @var MagentoFrameworkViewResultPageFactory
143 */
144 public $resultPageFactory;
145
146 /**
147 * @type MagentoFrameworkControllerResultForwardFactory
148 */
149 protected $resultForwardFactory;
150
151
152
153 /**
154 * View constructor.
155 * @param Context $context
156 * @param PageFactory $resultPageFactory
157 * @param ForwardFactory $resultForwardFactory
158
159 */
160 public function __construct(
161 Context $context,
162 PageFactory $resultPageFactory,
163 ForwardFactory $resultForwardFactory
164 )
165 {
166 parent::__construct($context);
167
168 $this->resultPageFactory = $resultPageFactory;
169 $this->resultForwardFactory = $resultForwardFactory;
170
171 }
172
173 /**
174 * @return $this|MagentoFrameworkViewResultPage
175 */
176 public function execute()
177 {
178 header('Access-Control-Allow-Origin: *');
179 header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
180 header('Access-Control-Allow-Headers: Origin,Authorization, Content-Type, X-Auth-Token');
181
182 $helper = $this->_objectManager->create(
183 'VendorModuleHelperData'
184 );
185 $data = array();
186 $zendRequest = new Zend_Controller_Request_Http();
187 $authentication_key = $zendRequest->getHeader('Authorization');
188 $device_id = $this->getRequest()->getParam('device_id');
189 // die;
190 if($authentication_key == '34F435436345685345345F23'){
191 /* create authorization table */
192 $resource = $this->_objectManager->create('MagentoFrameworkAppResourceConnection');
193 $connection = $resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
194
195 $query = $connection->query("create table if not exists tbl_authorization(id INT NOT NULL AUTO_INCREMENT primary key,device_id varchar(200) not null default '' ,session_id text not null)");
196
197 $status = $helper->createAutherizeSession($authentication_key,$device_id);
198 if($status == ''){
199 echo $message = $helper->convertJson('0001');
200 die;
201 } else if($status !=''){
202 $data[]['Authorization_key'] = $status;
203
204 $connection->query("delete from tbl_authorization where device_id='".$device_id."'");
205
206 $connection->query("insert into tbl_authorization(device_id,session_id) values('".$device_id."','".$status."')");
207
208 echo $message = $helper->convertJson('2000',$data);
209 }
210 } else {
211 echo $message = $helper->convertJson('4001');
212 }
213
214 }
215}
216
217<?php
218namespace VendorNameModuleNameControllerAuthorization;
219
220use MagentoFrameworkAppActionAction;
221
222class Globals extends Action
223{
224
225 public $auth;
226 public $uid;
227
228 public function execute()
229 {
230
231 $zendRequest = new Zend_Controller_Request_Http();
232 $authentication_key = $zendRequest->getHeader('Authorization');
233 $device_id = $this->getRequest()->getParam('device_id');
234
235 $helper = $this->_objectManager->create(
236 'VendorNameModuleNameHelperData'
237 );
238 $status = $helper->authenticate($authentication_key,$device_id);
239 if($status == 'false'){
240 echo $message = $helper->convertJson('4001');
241 die;
242 } else if($status == 'true'){
243 $message = $helper->convertJson('4000');
244 }
245
246
247 }
248}
249
250<?php
251
252namespace VendorNameModuleNameControllerApi;
253
254use MagentoFrameworkAppActionAction;
255
256class Productlist extends Action
257{
258
259 public function execute()
260 {
261 header('Access-Control-Allow-Origin: *');
262 header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
263 header('Access-Control-Allow-Headers: Origin,Authorization, Content-Type, X-Auth-Token');
264
265 $result = array();
266 $final = array();
267 /* get list of category banner */
268 $objectManager = MagentoFrameworkAppObjectManager::getInstance();
269
270 $helper = $objectManager->create(
271 'VendorNameModuleNameHelperData'
272 );
273 if($this->getRequest()->getParam('category_id')){
274 if($this->getRequest()->getParam('sort')){
275 $sortdata = explode('|', $this->getRequest()->getParam('sort'));
276 }
277 $cateinstance = $this->_objectManager->create('MagentoCatalogModelCategoryFactory');
278 $category_id = $this->getRequest()->getParam('category_id');
279 $collection = $cateinstance->create()->load($category_id)->getProductCollection()->addAttributeToSelect('*');
280 $collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
281 $collection->addAttributeToFilter('status',MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
282 $collection->setOrder('name','DESC');
283 $list = array();
284 $store = $objectManager->get('MagentoStoreModelStoreManagerInterface')->getStore();
285 $priceHelper = $objectManager->create('MagentoFrameworkPricingHelperData');
286 foreach ($collection as $product) {
287 $each = array();
288 $each['id'] = $product->getId();
289 $each['name'] = $product->getName();
290 $each['price'] = $priceHelper->currency($product->getFinalPrice(), true, false);
291 $each['image'] = $store->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
292 $list[] = $each;
293 }
294 $result['product'] = $list;
295 $final[] = $result;
296 $message = $helper->convertJson('2000',$final);
297 }else{
298 $message = $helper->convertJson('0001');
299 }
300 echo $message;
301 }
302}