· 9 years ago · Feb 02, 2017, 02:10 PM
1<?php
2
3class AppController extends Controller
4{
5
6 private $err = '';
7 private $model = null;
8 public function accessRules()
9 {
10 return array(
11
12 array('allow', // allow authenticated user to perform 'create' and 'update' actions
13 'actions'=>array('rescheduledOrders','changePassword','pausedProductList','unsuspendProduct','suspendProduct','removeImage','image','newOrders','readyForShipment','deliveredOrders','dashboard', 'newProduct', 'editProduct', 'fetchSubCat', 'syncSell', 'imgUpload', 'save', 'approvedProductList', 'unApprovedProductList', 'deleteImage', 'myProfile', 'uploadedProductList', 'rejectedProductList', 'shippedOrders', 'soldOutOrders', 'cancelledOrders', 'rejectedOrders', 'returnedOrders'),
14 'users'=>array('@'),
15 ),
16 array('deny', // deny all users
17 'users'=>array('*'),
18 ),
19 );
20 }
21
22 public function actionNewOrders(){
23
24
25 $model=new OcOrderProduct('search');
26 $model->unsetAttributes(); // clear any default values
27 if(isset($_GET['OcOrderProduct'])){
28
29 $model->attributes=$_GET['OcOrderProduct'];
30 }
31
32 $this->render('orders_new',array(
33 'model'=>$model,
34 ));
35 }
36
37 public function actionReadyForShipment(){
38 $model=new ItemQueue('readyForShipment');
39 $model->unsetAttributes(); // clear any default values
40 if(isset($_GET['ItemQueue'])){
41 $model->attributes=$_GET['ItemQueue'];
42 }
43
44 $this->render('orders_ready_for_shipment',array(
45 'model'=>$model,
46 ));
47 }
48 public function actionDeliveredOrders(){
49
50 if(isset($_GET['update'])) {
51 $count = 0;
52 $images = OcProductImage::model()->findAll('length(image) < 2');
53 foreach($images as $img) {
54 $count++;
55 $prod = OcProduct::model()->find('product_id='.$img->product_id);
56 $img->image = $prod->image;
57 if(!$img->save()){
58 echo $this->fetchErrors($img).' -- '.$prod->product_id.'<br>';
59 }
60 }
61 echo 'completed: '.$count;
62 exit;
63 }
64
65 $model=new ItemQueue('delivered');
66 $model->unsetAttributes(); // clear any default values
67 if(isset($_GET['ItemQueue'])){
68 $model->attributes=$_GET['ItemQueue'];
69 }
70
71 $total_sales = Yii::app()->db->createCommand('select sum(product_price) from item_queue where delivery_status = 2 and seller_id='.Yii::app()->user->id)->queryScalar();
72 $total_expected = Yii::app()->db->createCommand('select sum(seller_total) from oc_order_product where processing >= 0 and seller_paid_status >= 0 and seller_id='.Yii::app()->user->id)->queryScalar();
73 $total_paid = Yii::app()->db->createCommand('select sum(seller_total) from oc_order_product where processing >= 0 and seller_paid_status != 0 and seller_id='.Yii::app()->user->id)->queryScalar();
74 $total_unpaid = Yii::app()->db->createCommand('select sum(seller_total) from oc_order_product where processing >= 0 and seller_paid_status = 0 and seller_id='.Yii::app()->user->id)->queryScalar();
75
76 $this->render('orders_delivered',array(
77 'model'=>$model,'ts'=>$total_sales, 'te'=>$total_expected, 'tp'=>$total_paid, 'tu'=>$total_unpaid
78 ));
79 }
80
81 //Added Actions Begin
82
83 public function actionShippedOrders(){
84 $model=new ItemQueue('shipped');
85 $model->unsetAttributes(); // clear any default values
86 if(isset($_GET['ItemQueue'])){
87 $model->attributes=$_GET['ItemQueue'];
88 }
89
90 $this->render('orders_shipped',array(
91 'model'=>$model
92 ));
93 }
94
95
96 public function actionCancelledOrders(){
97 $model=new ItemQueue('cancelled');
98 $model->unsetAttributes(); // clear any default values
99 if(isset($_GET['ItemQueue'])){
100 $model->attributes=$_GET['ItemQueue'];
101 }
102
103
104 $this->render('orders_cancelled',array(
105 'model'=>$model
106 ));
107 }
108
109 public function actionRejectedOrders(){
110 $model=new ItemQueue('rejected');
111 $model->unsetAttributes(); // clear any default values
112 if(isset($_GET['ItemQueue'])){
113 $model->attributes=$_GET['ItemQueue'];
114 }
115
116 $this->render('orders_rejected',array(
117 'model'=>$model
118 ));
119 }
120
121 public function actionRescheduledOrders(){
122 $model=new ItemQueue('rescheduled');
123 $model->unsetAttributes(); // clear any default values
124 if(isset($_GET['ItemQueue'])){
125 $model->attributes=$_GET['ItemQueue'];
126 }
127
128 $this->render('orders_rescheduled',array(
129 'model'=>$model
130 ));
131 }
132
133 public function actionUploadedProductList(){
134 $model=new OcProduct('uploaded');
135 $model->unsetAttributes(); // clear any default values
136 if(isset($_GET['OcProduct'])){
137 $model->attributes=$_GET['OcProduct'];
138 }
139
140 $this->render('uploadedlist',array(
141 'model'=>$model,
142 ));
143 }
144
145 public function actionRejectedProductList(){
146 $model=new OcProduct('rejected');
147 $model->unsetAttributes(); // clear any default values
148 if(isset($_GET['OcProduct'])){
149 $model->attributes=$_GET['OcProduct'];
150 }
151
152 $this->render('rejectedlist',array(
153 'model'=>$model,
154 ));
155 }
156
157
158
159 // Added Actions End
160
161 public function actionPausedProductList(){
162 $model=new OcProduct('paused');
163 $model->unsetAttributes(); // clear any default values
164 if(isset($_GET['OcProduct'])){
165 $model->attributes=$_GET['OcProduct'];
166 }
167
168 $this->render('pausedlist',array(
169 'model'=>$model,
170 ));
171 }
172
173 public function actionApprovedProductList(){
174 $model=new OcProduct('approved');
175 $model->unsetAttributes(); // clear any default values
176 if(isset($_GET['OcProduct'])){
177 $model->attributes=$_GET['OcProduct'];
178 }
179
180 $this->render('productlist',array(
181 'model'=>$model,
182 ));
183 }
184
185 public function actionUnApprovedProductList(){
186 $model=new OcProduct('unapproved');
187 $model->unsetAttributes(); // clear any default values
188 if(isset($_GET['OcProduct'])){
189 $model->attributes=$_GET['OcProduct'];
190 }
191
192 $this->render('productlist_inreview',array(
193 'model'=>$model,
194 ));
195 }
196
197 //dont implement this.. just move the record temporarily to some temp table
198 private function deleteProduct($product_id) {
199 $this->db->query("DELETE FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
200 /*code start*/
201 $this->db->query("DELETE FROM " . DB_PREFIX . "seller WHERE vproduct_id = '" . (int)$product_id . "'");
202 /*code end*/
203 $this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "'");
204 $this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
205 $this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
206 $this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
207 $this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
208 $this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
209 $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
210 $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE related_id = '" . (int)$product_id . "'");
211 $this->db->query("DELETE FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$product_id . "'");
212 $this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
213
214 $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
215 $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
216 $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "'");
217 $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_store WHERE product_id = '" . (int)$product_id . "'");
218 $this->db->query("DELETE FROM " . DB_PREFIX . "review WHERE product_id = '" . (int)$product_id . "'");
219
220 $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");
221
222 $this->cache->delete('product');
223 }
224
225 public function actionDashboard()
226 {
227 $format = '{y:"%s",a:%s,b:%s}';
228 $str = '';
229 $year = date('Y');
230 $months = array('Jan'=>'01','Feb'=>'02','Mar'=>'03','Apr'=>'04','May'=>'05','Jun'=>'06','Jul'=>'07','Aug'=>'08','Sep'=>'09','Oct'=>'10','Nov'=>'11','Dec'=>'12');
231
232 foreach($months as $k=>$v){
233 $delivered = Yii::app()->db->createCommand('select count(*) from oc_order_product where processing=2 and seller_id='.Yii::app()->user->id.' and year(ts)='.$year.' and month(ts)='.$v)->queryScalar();
234 $orders = Yii::app()->db->createCommand('select count(*) from oc_order_product p, oc_order o where p.order_id=o.order_id and o.order_status_id>0 and seller_id='.Yii::app()->user->id.' and year(ts)='.$year.' and month(ts)='.$v)->queryScalar();
235 if($k != 'Dec'){
236 $str .= sprintf($format, $k, $orders, $delivered).',';
237 }else{
238 $str .= sprintf($format, $k, $orders, $delivered);
239 }
240 }
241 $this->render('dashboard', array('data'=>$str, 'year'=>$year));
242
243 }
244
245 private function getBrandId(){
246 $brand = OcManufacturer::model()->find('name=:a', array(':a'=>$_POST['brand']));
247 if(isset($brand)){
248 return $brand->manufacturer_id;
249 }else{
250 $brand = new OcManufacturer;
251 $brand->name = ucfirst($_POST['brand']);
252 $brand->sort_order = 1;
253 $brand->save();
254 return $brand->manufacturer_id;
255 }
256 }
257
258 //not approved
259 private function saveProduct($update = false) {
260 $this->model->manufacturer_id = $this->getBrandId();
261 $this->model->model = $_POST['model'];
262 $this->model->quantity = $_POST['quantity'];
263
264 $this->model->return_policy = $_POST['returns'];
265
266 //only set this on new
267 if(!$update){
268 $this->model->minimum = 1;
269 $this->model->subtract = 0;
270 $this->model->date_available = date('Y-m-d', time()) ;
271 $this->model->status = 0;
272 $this->model->seller_id = Yii::app()->user->id;
273 }
274 if($update){
275 $this->model->approve = 0;
276 $this->model->status = 0;
277 $this->model->date_modified = date('Y-m-d', time()) ;
278 }
279
280 $this->model->price = str_replace(",", "", $_POST['price']);
281
282 $this->model->weight = $_POST['weight'];
283
284
285 if($this->model->save()){
286 $this->initsku($this->model->product_id);
287 $this->model->sku = $this->sku;
288 $this->model->save();
289 if($this->saveProductImage($this->model, $update) != 0){
290 //$transaction->commit();
291 return $this->model->product_id;
292 }else{
293 return 0;
294 }
295 }else{
296 foreach($this->model->getErrors() as $value){
297 foreach ($value as $e) {
298 $this->err.=$e.'<br/>';
299 }
300 }
301 }
302 return 0;
303 }
304
305
306 private function saveSellerToProduct($productId){
307 $this->model = new OcSeller();
308 $this->model->vproduct_id = $productId;
309 $this->model->seller_id = Yii::app()->user->id;
310 if($this->model->save()){
311 return 1;
312 }else{
313 foreach($this->model->getErrors() as $value){
314 foreach ($value as $e) {
315 $this->err.=$e.'<br/>';
316 }
317 }
318 }
319 return 0;
320 }
321
322 public function actionDeleteImage(){
323
324 if(isset($_GET['file'])){
325 foreach($_SESSION as $k=>$v){
326 if($this->startsWith($k, 'img_')){
327 if($v == $_GET['file']){
328 unset($_SESSION[$k]);
329 echo 'true';
330 exit;
331 }
332 }
333 }
334 }
335 echo 'false';
336 }
337
338 private function getFirstImage(){
339 foreach($_SESSION as $k=>$v){
340 if($this->startsWith($k, 'img_')){
341 return $v;
342 }
343 }
344 return null;
345 }
346
347 private function wipeAllImages(){
348 $arr = array();
349 foreach($_SESSION as $k=>$v){
350 if($this->startsWith($k, 'img_')){
351 unset($_SESSION[$k]);
352 }
353 }
354 return $arr;
355 }
356
357 private function getAllImages(){
358 $arr = array();
359 foreach($_SESSION as $k=>$v){
360 if($this->startsWith($k, 'img_')){
361 $arr[] = $v;
362 }
363 }
364 return $arr;
365 }
366
367 private function startsWith($haystack, $needle){
368 return (substr($haystack, 0, strlen($needle)) === $needle);
369 }
370
371 private function saveProductImage($product, $update=false){
372 //save main image
373 $filename = $this->getFirstImage();
374 if($filename != null){
375 $seller = OcSellers::model()->find('seller_id=:a', array(':a'=>Yii::app()->user->id));
376 $product->image = html_entity_decode('seller/'.$seller->foldername.'/'.$filename, ENT_QUOTES, 'UTF-8') ;
377 if($product->save()){
378 return 1;
379 }else{
380 foreach($product->getErrors() as $value){
381 foreach ($value as $e) {
382 $this->err.=$e.'<br/>';
383 }
384 }
385 }
386 }else{
387 if($update){
388 $images = OcProductImage::model()->findAll('product_id='.$product->product_id);
389 if(isset($images) && count($images) > 0){
390 $product->image = $images[0]->image;
391 if($product->save()){
392 return 1;
393 }else{
394 foreach($product->getErrors() as $value){
395 foreach ($value as $e) {
396 $this->err.=$e.'<br/>';
397 }
398 }
399 }
400 }
401 }else{
402 $this->err .= 'Please upload at least one image';
403 }
404 }
405 return 0;
406 }
407
408 private function saveProductOptions($productId, $update=false){
409 $found_po = array();
410 $found_pov = array();
411 $optionz = array();
412 //print_r($_POST); exit;
413 if(isset($_POST['product_option'])){
414 foreach($_POST['product_option'] as $e){
415
416 //optimise later to only run this query on update
417 $po_db = OcProductOption::model()->find('product_id=:a and option_id=:b', array(':a'=>$productId,':b'=>$e['option_id']));
418
419 $po = isset($po_db) ? $po_db : new OcProductOption();
420 $po->required = $e['required'];
421 if(!isset($po_db)){
422 $po->option_id = $e['option_id'];
423 $po->product_id = $productId;
424 $po->value = "";
425 }else{
426 $found_po[] = $po_db->product_option_id;
427 }
428
429 if($e['type'] == 'text' && isset($e['value'])) {
430 $po->value = $e['value'];
431 $po->sku = $this->sku.'-'.sprintf('%03d', $e['option_id']).'-'.sprintf('%01d',$e['value']);
432 $po->save();
433 //this snippet below is to prevent new records being deleted by the remove snippet at the end of this function
434 if(!isset($po_db)){
435 $found_po[] = $po->product_option_id;
436 }
437 }
438
439
440
441 if($e['type'] == 'select' && isset($e['product_option_value'])) {
442 $po->save();
443 if(!isset($po_db)){
444 $found_po[] = $po->product_option_id;
445 }
446 foreach($e['product_option_value'] as $f){
447 $optionz[] = $f['option_value_id'];
448 $pov_db = OcProductOptionValue::model()->find('product_id=:a and product_option_id=:b and option_value_id=:c', array(':a'=>$productId,':b'=>$po->product_option_id,':c'=>$f['option_value_id']));
449 $pov = isset($pov_db) ? $pov_db : new OcProductOptionValue();
450 $pov->sku = $this->sku.'-'.sprintf('%03d', $e['option_id']).'-'.sprintf('%01d', $f['option_value_id']);
451 $pov->option_id = $e['option_id'];
452 if(!isset($pov_db)){
453 $pov->option_value_id = $f['option_value_id'];
454 $pov->product_option_id = $po->product_option_id;
455 $pov->product_id = $productId;
456 }else{
457 $found_pov[] = $pov_db->product_option_value_id;
458 }
459 $pov->points = 0;
460 $pov->points_prefix = '+';
461
462
463 $price = floatval(str_replace(",", "", $_POST['price']));
464 $povprice = isset($f['price']) && strlen($f['price']) > 0 ? floatval(str_replace(",", "", $f['price'])) : $price;
465 $diff = $price - $povprice; //if doesnt have its own price use its master's price :)
466 if($diff >= 0){
467 $pov->price = $diff;
468 $pov->price_prefix = '-';
469 }else {
470 $pov->price = $diff * -1;
471 $pov->price_prefix = '+';
472 }
473
474 $weight = str_replace(",", "", $_POST['weight']);
475 $povweight = isset($f['weight']) && strlen($f['weight']) > 0 ? str_replace(",", "", $f['weight']) : $weight;
476 $wdiff = $weight - $povweight;
477 if($wdiff >= 0){
478 $pov->weight = $wdiff;
479 $pov->weight_prefix = '-';
480 }else {
481 $pov->weight = $wdiff * -1;
482 $pov->weight_prefix = '+';
483 }
484
485
486 $pov->subtract = $f['subtract'];
487 $pov->quantity = $f['quantity'];
488
489 $pov->save();
490 if(!isset($pov_db)){
491 $found_pov[] = $pov->product_option_value_id;
492 }
493 }
494
495
496 }
497 }
498 }
499
500
501
502
503 //yank off all that were not selected
504
505 $alloptions = OcProductOption::model()->findAll('product_id=:a', array(':a'=>$productId));
506 foreach ($alloptions as $opt){
507 if(!in_array($opt->product_option_id, $found_po)){
508 $opt->delete();
509 }
510 }
511
512
513 $alloption_values = OcProductOptionValue::model()->findAll('product_id=:a', array(':a'=>$productId));
514 foreach ($alloption_values as $optval){
515 if(!in_array($optval->product_option_value_id, $found_pov)){
516 $optval->delete();
517 }
518 }
519
520
521
522 }
523
524
525
526 private function saveProductImages($productId, $update = false){
527 $arr = $this->getAllImages();
528 echo 'i';
529 foreach($arr as $filename){
530 $seller = OcSellers::model()->find('seller_id=:a', array(':a'=>Yii::app()->user->id));
531 $image = html_entity_decode('seller/'.$seller->foldername.'/'.$filename, ENT_QUOTES, 'UTF-8') ;
532 echo 'j';
533 $this->model = new OcProductImage();
534 $this->model->name = $filename;
535 $this->model->product_id = $productId;
536 $this->model->sort_order = 1;
537 $this->model->image = $image;
538 if(!$this->model->save()){
539 echo 'k';
540 Yii::app()->session['err'] = $this->fetchErrors($this->model);
541 }else{
542 //upload to amazon
543 echo 'o';
544 //$local = Yii::app()->params['filedir'].$seller->foldername.'/'.$filename;
545 //$remote = 'images/'.$seller->foldername.'/'.$filename;
546 //Yii::app()->s3->process($remote, $local);
547 echo 'l';
548 }
549 echo 'm';
550 unset($_SESSION['img_'.$filename]);
551 }
552 return 1;
553 }
554
555 private function saveProductDescription($productId, $update = false){
556 $d = OcProductDescription::model()->find('product_id='.$productId);
557
558
559 $this->model = isset($d) ? $d : new OcProductDescription;
560 $this->model->name = $_POST['name'];
561 $this->model->description = $_POST['description'];
562 $this->model->meta_title = $_POST['name'];
563 $this->model->meta_description = $_POST['description'];
564 $this->model->tag = str_replace(" ", ",", $_POST['name']);
565 $this->model->meta_keyword = str_replace(" ", ",", $_POST['name']);
566 if(!$update){
567 $this->model->product_id = $productId;
568 $this->model->language_id=1;
569 }
570 if($this->model->save()){
571 return 1;
572 }else{
573 foreach($this->model->getErrors() as $value){
574 foreach ($value as $e) {
575 $this->err.=$e.'<br/>';
576 }
577 }
578 }
579 return 0;
580 }
581
582 private function saveProductToStore($productId){
583 $this->model = new OcProductToStore();
584 $this->model->store_id = 0;
585 $this->model->product_id = $productId;
586 if($this->model->save()){
587 return 1;
588 }else{
589 foreach($this->model->getErrors() as $value){
590 foreach ($value as $e) {
591 $this->err.=$e.'<br/>';
592 }
593 }
594 }
595 return 0;
596 }
597
598 //validate that discounted price cant be more than main price
599 //discount expiry is valid date
600 //discount expiry is in future
601 private function saveProductSpecial($productId, $update=false){
602 if(!$update && isset($_POST['discount_price']) && $_POST['discount_price'] > 0 ){
603 $this->model = new OcProductSpecial();
604 $this->model->customer_group_id = 1;
605 $this->model->date_start = date('Y-m-d H:i:s');
606 $this->model->date_end = isset($_POST['discount_expiry']) && $_POST['discount_expiry'] ? $_POST['discount_expiry'] : date('y:m:d', strtotime("+60 days"));
607 $this->model->price = str_replace(",", "", $_POST['discount_price']);
608 $this->model->priority = 1;
609 $this->model->product_id = $productId;
610 if($this->model->save()){
611 return 1;
612 }else{
613 foreach($this->model->getErrors() as $value){
614 foreach ($value as $e) {
615 $this->err.=$e.'<br/>';
616 }
617 }
618 }
619 return 0;
620 }else if($update){
621 $d = OcProductSpecial::model()->find('product_id='.$productId);
622 if(isset($d)){
623 if(isset($_POST['discount_price']) && $_POST['discount_price'] > 0){
624 $this->model = $d;
625 $this->model->date_start = date('Y-m-d H:i:s');
626 $this->model->date_end = isset($_POST['discount_expiry']) && $_POST['discount_expiry'] ? $_POST['discount_expiry'] : date('y:m:d', strtotime("+60 days"));
627 $this->model->price = str_replace(",", "", $_POST['discount_price']);
628 if($this->model->save()){
629 return 1;
630 }else{
631 foreach($this->model->getErrors() as $value){
632 foreach ($value as $e) {
633 $this->err.=$e.'<br/>';
634 }
635 }
636 }
637 return 0;
638 }else{ //means the discount has been removed
639 if($d->delete()){
640 return 1;
641 }else{
642 return 0;
643 }
644 }
645 }else{
646 return 1;
647 }
648
649
650 }else{
651 return 1;
652 }
653 }
654
655 private function saveProductCategory($productId, $update = false){
656 $d = OcProductToCategory::model()->find('product_id='.$productId);
657
658 $this->model = isset($d) ? $d : new OcProductToCategory();
659 $this->model->category_id = Yii::app()->session['s_cat'];
660 if(!$update){
661 $this->model->product_id = $productId;
662 }
663 if($this->model->save()){
664 return 1;
665 }else{
666 foreach($this->model->getErrors() as $value){
667 foreach ($value as $e) {
668 $this->err.=$e.'<br/>';
669 }
670 }
671 }
672 return 0;
673 }
674
675
676 private function removeProductImages($productId){
677 if(isset($_POST['rmv'])){
678 foreach($_POST['rmv'] as $imgId){
679 $productImage = OcProductImage::model()->find('product_id=:a and product_image_id=:b', array(':a'=>$productId, ':b'=>$imgId));
680 if(isset($productImage)){
681 $product = OcProduct::model()->find('product_id=:a and seller_id=:b', array(':a'=>$productId, ':b'=>Yii::app()->user->id));
682 if(isset($product)){
683 $productImage->delete();
684 }
685 }
686 }
687 }
688 }
689
690 public function handleProductUpdate(){
691
692 $this->model = OcProduct::model()->find('product_id=:a', array(':a'=>$_GET['p']));
693 $transaction = $this->model->dbConnection->beginTransaction();
694
695 $productId = $this->saveProduct(true);
696 if($productId != 0){
697 if($this->saveProductDescription($productId, true) != 0){
698 if($this->saveProductSpecial($productId, true) != 0){
699 $this->removeProductImages($productId);
700 $this->saveProductOptions($productId, true);
701 $this->saveProductImages($productId, true);
702 $this->saveProductCategory($productId, true);
703 $transaction->commit();
704 $this->cleanSession();
705 Yii::app()->session['msg'] = "Cool. You've updated your product details successfully. It is now awaiting approval";
706 $this->redirect(array('app/unApprovedProductList'));
707 return;
708 }
709 }
710 }
711 $transaction->rollback();
712
713 $url = Yii::app()->createUrl('app/editProduct', array('p'=>$_GET['p']));
714 $this->redirect($url);
715
716 }
717
718 private function initsku($productId){
719
720
721 $words = explode(" ", $_POST['name']);
722 $sku = "";
723 foreach ($words as $w) {
724 if(preg_match("/^[a-zA-Z]$/", $w[0])){
725 $sku .= $w[0];
726 }
727 }
728 $this->sku = strtolower($sku).'-';
729 $this->sku .= sprintf('%05d', Yii::app()->user->id); //99999max
730 $this->sku .= sprintf('%05d', $productId); //99999max
731 $this->sku .= sprintf('%03d', Yii::app()->session['s_cat']);
732 }
733
734 private $sku = "";
735
736 public function actionSave(){
737
738 $this->model = new OcProduct;
739 $transaction = $this->model->dbConnection->beginTransaction();
740
741 $productId = $this->saveProduct();
742 if($productId != 0){
743 if($this->saveSellerToProduct($productId) != 0){
744 if($this->saveProductDescription($productId) != 0){
745 if($this->saveProductToStore($productId) != 0){
746 if($this->saveProductSpecial($productId) != 0){
747 $this->saveProductOptions($productId);
748 $this->saveProductImages($productId);
749 $this->saveProductCategory($productId);
750 $transaction->commit();
751 $this->cleanSession();
752 Yii::app()->session['msg'] = "Cool. You've added your product successfully. It is now awaiting approval";
753 $this->redirect(array('app/unApprovedProductList'));
754 return;
755 }
756 }
757 }
758 }
759 }
760 $transaction->rollback();
761
762 $this->redirect(array('app/newProduct'));
763
764 }
765
766 public $optcount = 0;
767 public $start = 0;
768
769 public function actionEditProduct() {
770
771
772
773 $product = OcProduct::model()->find('product_id=:a and seller_id=:b',array(':a'=>$_GET['p'], ':b'=>Yii::app()->user->id));
774 //print_r($product); exit;
775 if(isset($product)){
776 if(isset($_POST['returns'])){
777 $this->handleProductUpdate();
778 }
779
780
781 $cat = OcCategory::model()->findAll('status=1 and parent_id=0');
782 $brands = OcManufacturer::model()->findAll();
783 $images = OcProductImage::model()->findAll('product_id=:a', array(':a'=>$product->product_id));
784 //....$category = OcProductToCategory::model()->findAll('product_id=:a', array(':a'=>$product->product_id));
785 $brand_concat = '';
786 foreach ($brands as $br){
787 $brand_concat .= sprintf('"%s",', $br->name);
788 }
789
790 $data = $this->fetchProductDataArray($product);
791 //$data2 = $this->fetchProductOptionDataArray($product);
792 $return = $this->liAndVariations($data);
793
794 $catg = isset(Yii::app()->session['s_cat']) ? $this->fetCategoryHtml(Yii::app()->session['s_cat']) : $this->fetCategoryHtml($product->category[0]->category_id);
795
796
797 $this->render('edit_product', array('options'=>$this->getOptions(), 'cats'=>$cat, 'brands'=>$brand_concat, 'data'=> $data, 'cathtml'=>$catg, 'images'=>$images, 'variations'=>$return[0], 'li'=>$return[1]));
798 }else{
799 Yii::app()->session['err'] = "This product is unavailable";
800 $this->redirect(array('unApprovedProductList'));
801 }
802 }
803
804
805
806 public function actionNewProduct(){
807 $this->wipeAllImages();
808 if(isset($_GET['reset']) && $_GET['reset'] == 1){
809 $this->cleanSession();
810 $this->redirect(array('newProduct'));
811 }
812 //$this->cleanSession();
813
814 $cat = OcCategory::model()->findAll('status=1 and parent_id=0');
815 $brands = OcManufacturer::model()->findAll();
816 $brand_concat = '';
817 foreach ($brands as $br){
818 $brand_concat .= sprintf('"%s",', $br->name);
819 }
820 $data = isset(Yii::app()->session['sync_store']) ? unserialize(Yii::app()->session['sync_store']) : array();
821 $return = $this->liAndVariations($data);
822
823 //$return = $this->liAndVariations($this->convert($db));
824 //print_r($data); exit;
825
826 $catg = isset(Yii::app()->session['s_cat']) ? $this->fetCategoryHtml(Yii::app()->session['s_cat']) : null;
827
828 $this->render('new_product', array('cats'=>$cat, 'brands'=>$brand_concat, 'data'=> $data, 'cathtml'=>$catg, 'options'=>$this->getOptions(), 'variations'=>$return[1], 'optcount'=>$this->optcount, 'li'=>$return[1] ));
829
830 //print_r($_POST['product_option']); exit;
831
832 }
833
834 private function liAndVariations($data){
835
836 $ht = '';
837 $li = '';
838
839 //$arr = unserialize($data['product_option']);
840 //print_r($arr); exit;
841
842 if(isset($data['product_option']) && strlen($data['product_option']) > 0){
843
844 $arr = unserialize($data['product_option']);
845 $this->start = 0;
846 $this->optcount = 0;
847 //print_r($arr); exit;
848
849 foreach($arr as $a){
850
851 $optdata = OcOption::model()->find('option_id='.$a['option_id']);
852
853
854 $li .= sprintf('<li class="">'
855 . '<a href="#tab3%s" data-toggle="tab" aria-expanded="false">'
856 . '<i class="fa fa-minus-circle" onclick="$(\'a[href=\\\'#tab3%s\\\']\').parent().remove();'
857 . '$(\'#tab-option%s\').remove(); $(\'#option a:first\').tab(\'show\')"></i> %s</a></li>',$this->start,$this->start,$this->start, $a['name']);
858
859
860
861
862 $ht .= sprintf('<div class="tab-pane %s" id="tab3%s">', $this->start==0? 'active' : '',$this->start);
863 $ht .= sprintf('<input type="hidden" name="product_option[%s][product_option_id]" value="">',$this->start);
864 $ht .= sprintf('<input type="hidden" name="product_option[%s][name]" value="%s">',$this->start, $a['name']);
865 $ht .= sprintf('<input type="hidden" name="product_option[%s][option_id]" value="%s">',$this->start, $a['option_id']);
866 $ht .= sprintf('<input type="hidden" name="product_option[%s][type]" value="%s">',$this->start, $a['type']);
867 $ht .= '<div class="form-group">';
868 $ht .= sprintf('<label class="col-sm-2 control-label" for="input-required%s">Required</label>', $this->start);
869 $ht .= sprintf('<div class="col-sm-10"><select name="product_option[%s][required]" id="input-required%s" class="form-control" aria-invalid="false">', $this->start, $this->start);
870 $ht .= '<option value="1">Yes</option><option value="0">No</option> </select></div></div>';
871
872 if($a['type'] == 'text' && isset($a['value'])){
873 $ht .= '<div class="form-group"> '
874 . ' <label class="col-sm-2 control-label" for="input-value3">Option Value</label> ';
875 $ht .= sprintf(' <div class="col-sm-10"><input type="text" name="product_option[%s][value]" value="%s" placeholder="Option Value" id="input-value%s" class="form-control" aria-invalid="false"></div> </div>', $this->start,$a['value'], $this->start);
876 }
877
878 if (isset($a['product_option_value'])) {
879
880 //echo 'f'; exit;
881 $opts = $a['product_option_value'];
882 $ht .= '<div class="table-responsive">';
883 $ht .= sprintf('<table id="option-value%s" class="table table-striped table-bordered table-hover"> ',$this->start );
884 $ht .= ' <thead> <tr> <td class="text-left" style="width: 20%;">Option Value</td> <td class="text-right" style="width: 17%;">Quantity</td> <td class="text-left" style="width: 17%;">Subtract Stock</td> <td class="text-right">Price</td><td class="text-right">Weight</td> <td></td></tr> </thead> ';
885 $ht .= '<tbody>';
886
887 foreach($opts as $o){
888
889 $ht .= sprintf('<tr id="option-value-row%s">', $this->optcount);
890 $ht .= '<td class="text-left">';
891
892 $ht .= sprintf('<select name="product_option[%s][product_option_value][%s][option_value_id]" class="form-control">', $this->start, $this->optcount);
893 foreach($optdata->getValues() as $val){
894 $ht .= sprintf('<option value="%s" %s>%s</option>', $val->option_value_id, $val->option_value_id==$o['option_value_id'] ? 'selected="selected"' : '', $val->name);
895 }
896 $ht .= sprintf('</select><input type="hidden" name="product_option[%s][product_option_value][%s][product_option_value_id]" value=""></td>',$this->start,$this->optcount);
897 $ht .= sprintf(' <td class="text-right"><input type="text" name="product_option[%s][product_option_value][%s][quantity]" value="%s" placeholder="Quantity" class="form-control" aria-invalid="false"></td>',$this->start,$this->optcount,$o['quantity']);
898 $ht .= sprintf('<td class="text-left"><select name="product_option[%s][product_option_value][%s][subtract]" class="form-control"> ',$this->start,$this->optcount);
899 $ht .= sprintf('<option value="1" %s>Yes</option> <option value="0" %s>No</option> </select></td> <td class="text-right">', $o['subtract'] == 1 ? 'selected="selected"' : '',$o['subtract'] == 0 ? 'selected="selected"' : '');
900 $ht .= sprintf('<input type="text" name="product_option[%s][product_option_value][%s][price]" value="%s" placeholder="Price" class="form-control" aria-invalid="false"></td>', $this->start, $this->optcount, $o['price']);
901 $ht .= '<td class="text-right">';
902 $ht .= sprintf('<input type="text" name="product_option[%s][product_option_value][%s][weight]" value="%s" placeholder="Weight" class="form-control" aria-invalid="false"></td> ', $this->start, $this->optcount,$o['weight']);
903 $ht .= sprintf(' <td class="text-left"><button type="button" onclick="$(\'#option-value-row%s\').remove();" data-toggle="tooltip" rel="tooltip" title="Remove" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td> ',$this->optcount);
904 $ht .= '</tr></tbody>';
905 $this->optcount++;
906 }
907
908
909
910 $ht .= '<tfoot> <tr> <td colspan="5"></td> <td class="text-left">';
911 $ht .= sprintf('<button type="button" onclick="addOptionValue(%s);" data-toggle="tooltip" title="Add Option Value" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td></tr></tfoot></table></div> ', $this->start);
912
913 }
914
915
916 $ht .= sprintf('<select id="option-values%s" style="display: none;"> ', $this->optcount);
917 $ht .= ' <option value="42">Yellow</option> <option value="41">Green</option> <option value="39">Red</option> <option value="40">Blue</option> </select></div>';
918 $this->start++;
919 }
920
921 }
922
923 $arrr = array($ht, $li);
924 return $arrr;
925
926 }
927
928 private function getOptions(){
929 $html = '';
930 $options = OcOption::model()->findAll();
931 foreach ($options as $o){
932 $html .= $this->popOptions('<option value="%s" data-category="Choose" data-label="%s" data-type="%s" data-ovalue="%s">'.$o->name.'</option>', $o);
933 }
934 return $html;
935 }
936
937 private function popOptions($string, $o){
938 return sprintf($string, $o->option_id, $o->name, $o->type, $this->getOptionValueInJson($o->getValues()));
939 }
940
941 private function getOptionValueInJson($values){
942 $str = '[';
943 $count = 1;
944 foreach($values as $v){
945 if($count == count($values)){
946 $str .= sprintf('{\'option_value_id\':\'%s\',\'name\':\'%s\'}', $v->option_value_id, $v->name);
947 }else{
948 $str .= sprintf('{\'option_value_id\':\'%s\',\'name\':\'%s\'},', $v->option_value_id, $v->name);
949 }
950 $count++;
951 }
952 $str .= ']';
953 return $str;
954 }
955
956
957 public function actionFetchSubCat(){
958 if(isset($_POST['e']) && isset($_POST['i'])){
959 $str = '<div class="form-group cat_%s catt" >
960 <label class="control-label col-md-3"></label>
961 <div class="col-md-4">
962 <select class="form-control" name="category_%s" id="cat_%s">
963 <option></option>
964 %s
965
966 </select>
967 <span class="help-block">
968 Select sub category (not required) but makes your product very easy to find </span>
969
970 </div>
971
972 </div> ';
973
974
975 $cats = OcCategory::model()->findAll('status=1 and parent_id='.$_POST['e']);
976 $cat_array = explode('_',$_POST['i']);
977
978 $cat_id = $cat_array[1] + 1;
979 if(isset($cats) && count($cats) > 0){
980 $opts = '';
981 foreach($cats as $cat){
982 $opts.= '<option value="'.$cat->category_id.'">'.$cat->name.'</option>';
983 }
984
985 Yii::app()->session['s_cat'] = $_POST['e'];
986 echo json_encode(array('resp'=>0,'after'=>'cat_'.$cat_array[1],'curr'=>$cat_id, 'dat'=>sprintf($str,$cat_id, $cat_id, $cat_id,$opts)));
987
988 Yii::app()->end();
989 }else{
990 //well, if the category has no child, we still need to remove anything that exists (After) the current changed element
991 //so we need to pass curr
992 Yii::app()->session['s_cat'] = $_POST['e'];
993 echo json_encode(array('resp'=>200,'curr'=>$cat_id));
994 Yii::app()->end();
995 }
996
997 }
998 echo json_encode(array('resp'=>100));
999 Yii::app()->end();
1000 }
1001
1002 private function fetchProductDataArray($product){
1003 $p = array();
1004
1005 $p['description'] = $product->description[0]->description;
1006 $p['price'] = $product->price;
1007 $p['name'] = $product->description[0]->name;
1008 $p['brand'] = OcManufacturer::model()->findByPk($product->manufacturer_id)->name;
1009 $special = OcProductSpecial::model()->find('product_id=:a', array(':a'=>$product->product_id));
1010 $p['discount_price'] = isset($special) ? $special->price : 0;
1011 $p['discount_expiry'] = isset($special) ? $special->date_end : '';
1012 $p['quantity'] = $product->quantity;
1013 $p['weight'] = $product->weight;
1014 $p['model'] = $product->model;
1015 $p['returns'] = $product->return_policy;
1016 $p['image'] = $product->image;
1017 $p['pid'] = $product->product_id;
1018 $p['product_option'] = $this->formatProductOption($product);
1019 return $p;
1020 }
1021
1022
1023
1024 private function formatProductOption($product) {
1025
1026 $arr = array();
1027 $opts = OcProductOption::model()->findAll('product_id=:a', array(':a'=>$product->product_id));
1028 //echo count($opts); exit;
1029 $p = array();
1030
1031
1032 if (isset($opts)) {
1033
1034 foreach($opts as $oo) {
1035
1036
1037 $p['option_id'] = $oo->option_id;
1038 $p['productOption'] = $oo->product_option_id;
1039 $desc = OcOptionDescription::model()->find('option_id='.$oo->option_id);
1040 $opt = OcOption::model()->find('option_id='.$oo->option_id);
1041 $p['name'] = $desc->name ;
1042 $p['type'] = $opt->type;
1043 $p['value'] = $oo->value;
1044 $p['product_option_value'] = array();
1045 $opts_vals = OcProductOptionValue::model()->findAll('product_id=:a and product_option_id=:b', array(':a'=>$product->product_id, ':b'=>$oo->product_option_id));
1046 foreach($opts_vals as $ov) {
1047 $oov = array();
1048 $oov['option_value_id'] = $ov->option_value_id;
1049 $oov['product_option_value_id'] = $ov->product_option_value_id;
1050 $oov['quantity'] = $ov->quantity;
1051 $oov['price'] = $ov->price == 0 ? '' : number_format($this->actualValue($product->price, $ov->price, $ov->price_prefix),2);
1052 $oov['weight'] = $ov->weight == 0 ? '' : number_format($this->actualValue($product->weight, $ov->weight, $ov->weight_prefix),1);
1053 $oov['subtract'] = $ov->subtract;
1054 $p['product_option_value'][] = $oov;
1055 }
1056 $arr[] = $p;
1057 }
1058 return serialize($arr);
1059
1060 } else {
1061 return '';
1062 }
1063 }
1064
1065 private function actualValue($productValue, $optionValue, $pointPrefix ){
1066 if($optionValue > 0){
1067 if($pointPrefix == '+'){
1068 return $productValue + $optionValue;
1069 }else{
1070 return $productValue - $optionValue;
1071 }
1072 }
1073 return 0;
1074 }
1075
1076 public function actionSyncSell() {
1077 $p = array();
1078
1079 if(isset($_POST['description'])){
1080 $p['description'] = $_POST['description'];
1081 }
1082 if(isset($_POST['price'])){
1083 $p['price'] = $_POST['price'];
1084 }
1085 if(isset($_POST['name'])){
1086 $p['name'] = $_POST['name'];
1087 }
1088 if(isset($_POST['brand'])){
1089 $p['brand'] = $_POST['brand'];
1090 }
1091 if(isset($_POST['discount_price'])){
1092 $p['discount_price'] = $_POST['discount_price'];
1093 }
1094 if(isset($_POST['discount_expiry'])){
1095 $p['discount_expiry'] = $_POST['discount_expiry'];
1096 }
1097 if(isset($_POST['quantity'])){
1098 $p['quantity'] = $_POST['quantity'];
1099 }
1100 if(isset($_POST['weight'])){
1101 $p['weight'] = $_POST['weight'];
1102 }
1103 if(isset($_POST['returns'])){
1104 $p['returns'] = $_POST['returns'];
1105 }
1106 if(isset($_POST['model'])){
1107 $p['model'] = $_POST['model'];
1108 }
1109 if(isset($_POST['product_option'])){
1110 $p['product_option'] = serialize($_POST['product_option']);
1111
1112 }
1113 //print_r($_POST['product_option']); exit;
1114 //echo ($_POST['product_option']); exit;
1115 //Yii::log(serialize($_POST),CLogger::LEVEL_WARNING,'shoe');
1116
1117 Yii::app()->session['sync_store'] = serialize($p);
1118 Yii::app()->end();
1119 }
1120
1121
1122 public function actionImage(){
1123 $seller = OcSellers::model()->find('seller_id=:a', array(':a'=>Yii::app()->user->id));
1124 $file = Yii::app()->params['filedir'].$seller->foldername.'/thumbnail/'.$_GET['i'];
1125 if(file_exists($file)){
1126 $info = getimagesize($file); //extract the image content type
1127 ob_clean();
1128 header('Content-Type:' . $info['mime']);
1129 readfile($file);
1130 exit;
1131 }
1132 }
1133
1134 //consider seller id
1135 public function actionRemoveImage(){
1136 if(isset($_POST['k'])){
1137 $product = OcProduct::model()->find('product_id=:a and seller_id=:b', array(':a'=>$_POST['k'], ':b'=>Yii::app()->user->id));
1138 if(isset($product) && isset($_POST['j'])){
1139 $productImage = OcProductImage::model()->find('product_id=:a and product_image_id=:b', array(':a'=>$_POST['k'], ':b'=>$_POST['j']));
1140 if(isset($productImage)){
1141 if($productImage->delete()){
1142 echo "3";
1143 Yii::app()->end();
1144 }
1145 }
1146 }
1147 }
1148 echo "4";
1149 Yii::app()->end();
1150 }
1151
1152 public function fetCategoryHtml($cat) {
1153 $holder = array();
1154
1155 $catObj = OcCategory::model()->find('category_id='.$cat);
1156 if(isset($catObj)){
1157 $curr = $catObj;
1158
1159 while($curr != null){
1160 $holder[] = $curr;
1161 $curr = $curr->parent;
1162 }
1163
1164 $holder = array_reverse($holder);
1165 $str = '<div class="form-group cat_%s catt" >
1166 <label class="control-label col-md-3">%s</label>
1167 <div class="col-md-4">
1168 <select class="form-control" name="cat_%s" id="cat_%s">
1169 <option></option>
1170 %s
1171
1172 </select>
1173 <span class="help-block">
1174 %s </span>
1175
1176 </div>
1177
1178 </div> ';
1179 $output = '';
1180 for($i=1; $i <= count($holder); $i++){
1181 $currCat = $holder[$i-1];
1182 $cats = OcCategory::model()->findAll('status=1 and parent_id='.$currCat->parent_id);
1183 $opts = '';
1184 if(isset($cats) && count($cats) > 0){
1185 foreach($cats as $cat){
1186 if($cat->category_id != $currCat->category_id){
1187 $opts.= '<option value="'.$cat->category_id.'">'.$cat->name.'</option>';
1188 }else{
1189 $opts.= '<option selected="selected" value="'.$cat->category_id.'">'.$cat->name.'</option>';
1190 }
1191 }
1192 }
1193 if($i == 1){
1194 $output .= sprintf($str, $i, 'Category <span class="required" aria-required="true">* </span>', $i, $i, $opts,'Select base category' );
1195 }else{
1196 $output .= sprintf($str, $i, '', $i, $i, $opts,'Select sub category (not required) but makes your product very easy to find' );
1197 }
1198 }
1199
1200 }
1201 return $output;
1202
1203 }
1204
1205
1206
1207 private function cleanSession() {
1208 unset(Yii::app()->session['s_cat']);
1209 unset(Yii::app()->session['sync_store']);
1210 $keys = array();
1211 foreach($_SESSION as $k=>$v){
1212 if($this->startsWith($k, "img_")){
1213 $keys[] = $k;
1214 }
1215 }
1216
1217 foreach($keys as $kk){
1218 unset(Yii::app()->session[$kk]);
1219 }
1220 }
1221
1222 public function actionSuspendProduct() {
1223 $model = OcProduct::model()->find('product_id=:a', array(':a'=>$_GET['p']));
1224 if(isset($model)){
1225 $model->status = 0;
1226 if($model->save()){
1227 Yii::app()->session['msg'] = "Product has been paused";
1228 }else{
1229 Yii::app()->session['err'] = "Could not pause product, please contact customer service";
1230 }
1231 }
1232 $this->redirect(Yii::app()->request->getUrlReferrer());
1233 }
1234
1235 public function actionUnsuspendProduct() {
1236 $model = OcProduct::model()->find('product_id=:a', array(':a'=>$_GET['p']));
1237 if(isset($model)){
1238 $model->status = 1;
1239 if($model->save()){
1240 Yii::app()->session['msg'] = "Product has been resumed";
1241 }else{
1242 Yii::app()->session['err'] = "Could not resume product, please contact customer service";
1243 }
1244 }
1245 $this->redirect(Yii::app()->request->getUrlReferrer());
1246 }
1247
1248 public function actionChangePassword(){
1249 if(isset($_POST['old_password'])){
1250 $seller = OcSellers::model()->find("seller_id = " .Yii::app()->user->id . " AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . addslashes($_POST['old_password']) . "')))))) AND status = '1' AND approved = '1'");
1251 if(isset($seller)){
1252 if(isset($_POST['new_password']) && isset($_POST['confirm_password'])){
1253 if($_POST['new_password'] == $_POST['confirm_password']){
1254 if( strlen($_POST['new_password']) > 5 ){
1255 $salt = substr(md5(uniqid(rand(), true)), 0, 9);
1256 $seller->password = sha1($salt . sha1($salt . sha1($_POST['new_password'])));
1257 $seller->salt = $salt;
1258 if(strlen($seller->token) < 2){ //temp hack for tokenless records
1259 $seller->token = $this->token();
1260 }
1261 if($seller->save()){
1262 Yii::app()->session['msg'] = "Password update successful";
1263 }else{
1264 Yii::app()->session['err'] = "Could not update password";
1265 }
1266 }else{
1267 Yii::app()->session['err'] = "Password should be at least 6 characters";
1268 }
1269 }else{
1270 Yii::app()->session['err'] = "Please confirm your new password";
1271 }
1272 }else{
1273 Yii::app()->session['err'] = "Please enter your new password";
1274 }
1275 }else{
1276 Yii::app()->session['err'] = "Old password incorrect";
1277 }
1278 }
1279 $this->render('changepassword');
1280 return;
1281
1282 }
1283
1284
1285 //Added lines begin
1286
1287 public function actionMyProfile(){
1288
1289 $seller = OcSellers::model()->find("seller_id = " .Yii::app()->user->id);
1290 $s = $this->getSellerObject($seller);
1291 $seller_id = $s->seller_id;
1292 $address = OcSaddress::model()->find("seller_id = " .$seller_id);
1293 //$s = $this->getSellerObject($seller);
1294 //print_r($seller); exit;
1295 //$seller_id = $s->seller_id;
1296
1297 //$address = OcSaddress::model()->find("seller_id = " .$seller_id);
1298
1299 $conf = filter_input(INPUT_POST, 'confirm');
1300 $uname = filter_input(INPUT_POST, 'u');
1301 $fname = filter_input(INPUT_POST, 'firstname');
1302
1303 if(isset($fname)) {
1304 //$this->validateUsername($uname);
1305
1306 //if(isset($fname)){
1307
1308 //if(isset($seller)){
1309
1310
1311
1312 if($s->save()) {
1313
1314 $saddr = $this->getSellersAddressObject($s->seller_id);
1315 $saddr->save();
1316 Yii::app()->session['msg'] = "Your details were updated successfully!";
1317 $this->redirect(array('dashboard'));
1318
1319 } else {
1320 //Yii::app()->session['err'] = $err;
1321 print_r($s->getErrors()); exit;
1322 }
1323
1324 //}
1325
1326 //}
1327
1328 }
1329 //if(strlen($err) == 0){
1330 //Yii::app()->session['regmsg'] = "Update successful";
1331 //$this->redirect(array('dashboard'));
1332 //}else{
1333 //Yii::app()->session['err'] = $err;
1334 //}
1335 //$this->render('myProfile', array('seller'=>$seller, 'states'=> OcCountry::model()->findAll()));
1336 $this->render('myProfile', array('seller'=>$seller, 'states'=> OcCountry::model()->findAll(), 'address'=>$address));
1337 //return;
1338
1339 }
1340
1341 private function validateUsername($uname){
1342 if(isset($uname)){
1343 $e = Yii::app()->db->createCommand('select count(*) from oc_sellers where LOWER(username)=:a');
1344 $e->bindValue(':a',filter_input(INPUT_POST, 'u'));
1345 echo $e->queryScalar() == 0 ? "true" : "false";
1346 }else{
1347 echo false;
1348 }
1349 Yii::app()->end();
1350 }
1351
1352
1353 private function getSellerObject($seller){
1354 $seller_id = $seller->seller_id;
1355 $s = OcSellers::model()->find("seller_id = " .$seller_id);
1356 //$s->aboutus = Yii::app()->getRequest()->getPost('description');
1357 $s->account_name = Yii::app()->getRequest()->getPost('account_name');
1358 $s->account_number = Yii::app()->getRequest()->getPost('account_number');
1359 $s->bank_name = Yii::app()->getRequest()->getPost('bank_name');
1360 $s->telephone = Yii::app()->getRequest()->getPost('phone');
1361 $s->firstname = Yii::app()->getRequest()->getPost('firstname');
1362 $s->lastname = Yii::app()->getRequest()->getPost('lastname');
1363
1364 $token = $s->token;
1365
1366 if(!isset($token) || strlen($token) < 2){
1367 $s->token = $this->token();
1368 }
1369
1370 return $s;
1371 }
1372
1373 private function getSellersAddressObject($seller_id){
1374
1375 $saddr = OcSaddress::model()->find("seller_id = " .$seller_id);
1376
1377 $saddr->address_1 = Yii::app()->getRequest()->getPost('address_1');
1378 $saddr->address_2 = Yii::app()->getRequest()->getPost('address_2');
1379 $saddr->city = Yii::app()->getRequest()->getPost('city');
1380 $saddr->country_id = Yii::app()->getRequest()->getPost('state');
1381 $saddr->firstname = Yii::app()->getRequest()->getPost('firstname');
1382 $saddr->lastname = Yii::app()->getRequest()->getPost('lastname');
1383 //$saddr->seller_id = $sellerid;
1384 //$saddr->zone_id = 0;
1385
1386 return $saddr;
1387 }
1388
1389 protected function token(){
1390 $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
1391 $image_name = '';
1392 for($i = 0; $i < 30; $i++){
1393 $image_name .= $letters[mt_rand(0, 61)];
1394 }
1395 return $image_name;
1396 }
1397
1398
1399 public function actionUpdateSeller()
1400 {
1401 $seller = OcSellers::model()->find("seller_id = " .Yii::app()->user->id);
1402 //$seller_id = $seller->seller_id;
1403 //$address = OcSaddress::model()->find("seller_id = " .$seller_id);
1404
1405 if(isset($seller)){
1406
1407 //print_r($seller); exit;
1408 }
1409 $s = $this->getSellerObject();
1410 $err = $this->saveSeller($s);
1411 if(strlen($err) == 0){
1412 Yii::app()->session['regmsg'] = "Update successful";
1413 $this->redirect(array('dashboard'));
1414 }else{
1415 Yii::app()->session['err'] = $err;
1416 }
1417 //$r = new OcProduct;
1418
1419 //$this->layout = "landing";
1420 $this->render('register', array('states'=> OcCountry::model()->findAll()));
1421 }
1422
1423
1424
1425
1426 private function fetchErrors(&$model){
1427 $err = '';
1428 foreach($model->getErrors() as $value){
1429 foreach ($value as $e) {
1430 $err.=$e.'<br/>';
1431 }
1432 }
1433
1434 return $err;
1435 }
1436
1437 //Added lines end
1438
1439
1440
1441}