1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image module controller |
4
|
|
|
* |
5
|
|
|
* Controller for module Image |
6
|
|
|
* |
7
|
|
|
* @category Controller |
8
|
|
|
* @subpackage Admin |
9
|
|
|
* @package Olapus |
10
|
|
|
* @author Jan Drda <[email protected]> |
11
|
|
|
* @copyright Jan Drda |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace App\Http\Controllers\Admin; |
16
|
|
|
|
17
|
|
|
use App\Helpers; |
18
|
|
|
use Illuminate\Http\Request; |
19
|
|
|
use Illuminate\Support\Facades\Validator; |
20
|
|
|
|
21
|
|
|
class ImageController extends AdminModuleController |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
*/ |
27
|
|
|
public function __construct() { |
28
|
|
|
parent::__construct(); |
29
|
|
|
|
30
|
|
|
$this->middleware('add.lookup.tables:ImageCategory', ['only' => ['index','create','edit']]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Validation rules |
35
|
|
|
*/ |
36
|
|
|
protected $arValidationArray = [ |
37
|
|
|
'name' => 'required|max:255', |
38
|
|
|
'description' => 'max:255', |
39
|
|
|
'alt' => 'max:255', |
40
|
|
|
'url' => 'max:255|unique:image,url' |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Binary fields |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $binaryFields = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get number of pagination rows |
52
|
|
|
* |
53
|
|
|
* @return integer |
54
|
|
|
*/ |
55
|
|
|
public function getRowsToPaginate(){ |
56
|
|
|
|
57
|
|
|
return env('ADMIN_MEDIA_PAGINATE', 12); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Associate relationships to other table |
62
|
|
|
* |
63
|
|
|
* @param object $object |
64
|
|
|
* @param Request $request |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function associateRelationships($object, Request $request){ |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate category ID, if failed set to default |
70
|
|
|
*/ |
71
|
|
|
$validator = Validator::make($request->all(), [ |
72
|
|
|
'imagecategory_id' => 'required|integer|min:1|exists:imagecategory,id', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Validator fails - set category to default |
77
|
|
|
*/ |
78
|
|
|
if ($validator->fails()) { |
79
|
|
|
|
80
|
|
|
$object->imagecategories()->associate(1); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Validator OK - save it |
85
|
|
|
*/ |
86
|
|
|
else{ |
87
|
|
|
|
88
|
|
|
$object->imagecategories()->associate($request->input('imagecategory_id')); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Save media to storage |
94
|
|
|
* |
95
|
|
|
* @param object $object |
96
|
|
|
* @param Request $request |
97
|
|
|
* @param boolean $update |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
|
|
public function saveMediaToStorage($object, $request, $update = FALSE) { |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if requested and then save |
104
|
|
|
*/ |
105
|
|
|
if ($update == FALSE || ($update == TRUE && $request->has('image'))) { |
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Image has an url |
109
|
|
|
*/ |
110
|
|
|
if(strlen($request->input('url') > 5)){ |
111
|
|
|
$object->addMedia($request->file('image'))->toMediaCollection('images') |
112
|
|
|
->usingName($request->input('url')); |
113
|
|
|
} |
114
|
|
|
else{ |
115
|
|
|
$object->addMedia($request->file('image'))->toMediaCollection('images'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Update meta information |
120
|
|
|
*/ |
121
|
|
|
return TRUE; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return FALSE; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.