|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\Redirect; |
|
4
|
|
|
|
|
5
|
|
|
use Validator; |
|
6
|
|
|
use File; |
|
7
|
|
|
use Hideyo\Ecommerce\Framework\Services\Redirect\Entity\RedirectRepository; |
|
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
|
9
|
|
|
|
|
10
|
|
|
class RedirectService extends BaseService |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct(RedirectRepository $taxRate) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->repo = $taxRate; |
|
|
|
|
|
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The validation rules for the model. |
|
19
|
|
|
* |
|
20
|
|
|
* @param integer $redirectId id attribute model |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function rules($redirectId = false) |
|
24
|
|
|
{ |
|
25
|
|
|
$rules = array( |
|
26
|
|
|
'url' => 'required|unique_with:'.$this->repo->getModel()->getTable().', shop_id' |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
if ($redirectId) { |
|
|
|
|
|
|
30
|
|
|
$rules['url'] = 'required|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$redirectId.' = id'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $rules; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function create(array $attributes) |
|
37
|
|
|
{ |
|
38
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
39
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
|
40
|
|
|
|
|
41
|
|
|
if ($validator->fails()) { |
|
42
|
|
|
return $validator; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
46
|
|
|
$this->repo->getModel()->fill($attributes); |
|
47
|
|
|
$this->repo->getModel()->save(); |
|
48
|
|
|
return $this->repo->getModel(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function updateById(array $attributes, $id) |
|
52
|
|
|
{ |
|
53
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
|
|
54
|
|
|
$validator = Validator::make($attributes, $this->rules($id)); |
|
55
|
|
|
if ($validator->fails()) { |
|
56
|
|
|
return $validator; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$model = $this->find($id); |
|
60
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
if (count($attributes) > 0) { |
|
63
|
|
|
$model->fill($attributes); |
|
64
|
|
|
$model->save(); |
|
65
|
|
|
} |
|
66
|
|
|
return $model; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function importCsv($results, $shopId) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($results as $row) { |
|
72
|
|
|
|
|
73
|
|
|
$attributes = $row->toArray(); |
|
74
|
|
|
$attributes['shop_id'] = $shopId; |
|
75
|
|
|
$attributes['active'] = 0; |
|
76
|
|
|
|
|
77
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
|
78
|
|
|
|
|
79
|
|
|
if ($validator->fails()) { |
|
80
|
|
|
|
|
81
|
|
|
$result = $this->repo->getModel()->where('url', '=', $attributes['url'])->get()->first(); |
|
82
|
|
|
if ($result) { |
|
83
|
|
|
$attributes['active'] = 0; |
|
84
|
|
|
if($attributes['redirect_url']) { |
|
85
|
|
|
$attributes['active'] = 1; |
|
86
|
|
|
} |
|
87
|
|
|
$model = $this->find($result->id); |
|
88
|
|
|
$model->fill($attributes); |
|
89
|
|
|
$model->save(); |
|
90
|
|
|
|
|
91
|
|
|
return $model; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} else { |
|
95
|
|
|
$redirect = new Redirect; |
|
96
|
|
|
$redirect->fill($attributes); |
|
97
|
|
|
$redirect->save(); |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function updateClicks($url) |
|
106
|
|
|
{ |
|
107
|
|
|
$result = $this->repo->findByUrl($url); |
|
108
|
|
|
if ($result) { |
|
109
|
|
|
$model = $this->find($result->id); |
|
110
|
|
|
$model->fill(array('clicks' => $result->clicks + 1)); |
|
111
|
|
|
$model->save(); |
|
112
|
|
|
|
|
113
|
|
|
return $model; |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function findByUrl($url) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->repo->findByUrl($url); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
public function findByUrlAndActive($url) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->repo->findByUrlAndActive($url); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
} |