zaherkhirullah /
laravel-helpers
| 1 | <?php |
||||||||||||||||
| 2 | |||||||||||||||||
| 3 | /** |
||||||||||||||||
| 4 | * Author: Zahir Hayrullah |
||||||||||||||||
| 5 | * create date : 10/04/2020 07:00 AM |
||||||||||||||||
| 6 | * Last Modified Date: 10/04/2020 07:00 AM. |
||||||||||||||||
| 7 | */ |
||||||||||||||||
| 8 | |||||||||||||||||
| 9 | use Illuminate\Support\Facades\File; |
||||||||||||||||
| 10 | use Illuminate\Support\Str; |
||||||||||||||||
| 11 | |||||||||||||||||
| 12 | if (!function_exists('version')) { |
||||||||||||||||
| 13 | |||||||||||||||||
| 14 | /** |
||||||||||||||||
| 15 | * @param $file |
||||||||||||||||
| 16 | * |
||||||||||||||||
| 17 | * @return mixed |
||||||||||||||||
| 18 | */ |
||||||||||||||||
| 19 | function version($file) |
||||||||||||||||
| 20 | { |
||||||||||||||||
| 21 | return File::exists($file) ? File::lastModified($file) : '1'; |
||||||||||||||||
| 22 | } |
||||||||||||||||
| 23 | } |
||||||||||||||||
| 24 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 25 | |||||||||||||||||
| 26 | if (!function_exists('asset_v')) { |
||||||||||||||||
| 27 | function asset_v($path, $prefix = 'v') |
||||||||||||||||
| 28 | { |
||||||||||||||||
| 29 | return asset($path)."?{$prefix}=".version(public_path($path)); |
||||||||||||||||
| 30 | } |
||||||||||||||||
| 31 | } |
||||||||||||||||
| 32 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 33 | |||||||||||||||||
| 34 | if (!function_exists('get_folder_path')) { |
||||||||||||||||
| 35 | function get_folder_path($folder) |
||||||||||||||||
| 36 | { |
||||||||||||||||
| 37 | // get folder public path |
||||||||||||||||
| 38 | $path = public_path($folder); |
||||||||||||||||
| 39 | |||||||||||||||||
| 40 | // check if folder exist |
||||||||||||||||
| 41 | if (!file_exists($path)) { |
||||||||||||||||
| 42 | File::makeDirectory($path, 0777, true); |
||||||||||||||||
| 43 | } |
||||||||||||||||
| 44 | |||||||||||||||||
| 45 | return $path; |
||||||||||||||||
| 46 | } |
||||||||||||||||
| 47 | } |
||||||||||||||||
| 48 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 49 | |||||||||||||||||
| 50 | if (!function_exists('store_file')) { |
||||||||||||||||
| 51 | /** |
||||||||||||||||
| 52 | * @param $path |
||||||||||||||||
| 53 | * @param $file |
||||||||||||||||
| 54 | * |
||||||||||||||||
| 55 | * @return string |
||||||||||||||||
| 56 | */ |
||||||||||||||||
| 57 | function store_file($path, $file) |
||||||||||||||||
| 58 | { |
||||||||||||||||
| 59 | // get file extension |
||||||||||||||||
| 60 | $extension = $file->getClientOriginalExtension(); |
||||||||||||||||
| 61 | // filename to store |
||||||||||||||||
| 62 | // time +_+ 00 + XXX + 000 + x + 0000 = // time_00XXX000x0000.png |
||||||||||||||||
| 63 | $hash = md5(time()).'_'.rand(10, 99).Str::random(3).rand(100, 999).chr(rand(65, 90)).rand(1000, 9999); |
||||||||||||||||
| 64 | |||||||||||||||||
| 65 | $filename = $hash.'.'.$extension; |
||||||||||||||||
| 66 | |||||||||||||||||
| 67 | $file->move($path, $filename); |
||||||||||||||||
| 68 | |||||||||||||||||
| 69 | return $filename; |
||||||||||||||||
| 70 | } |
||||||||||||||||
| 71 | } |
||||||||||||||||
| 72 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 73 | |||||||||||||||||
| 74 | if (!function_exists('upload_image')) { |
||||||||||||||||
| 75 | /** |
||||||||||||||||
| 76 | * @param $request |
||||||||||||||||
| 77 | * @param $field_name |
||||||||||||||||
| 78 | * @param $folder |
||||||||||||||||
| 79 | * @param $old_image |
||||||||||||||||
| 80 | * |
||||||||||||||||
| 81 | * @return string |
||||||||||||||||
| 82 | */ |
||||||||||||||||
| 83 | function upload_image($request, $field_name, $folder, $old_image = null) |
||||||||||||||||
| 84 | { |
||||||||||||||||
| 85 | //delete old file |
||||||||||||||||
| 86 | if ($old_image) { |
||||||||||||||||
| 87 | unlink_file($old_image, $folder); |
||||||||||||||||
| 88 | } |
||||||||||||||||
| 89 | |||||||||||||||||
| 90 | // public path for folder |
||||||||||||||||
| 91 | $path = get_folder_path($folder); |
||||||||||||||||
| 92 | |||||||||||||||||
| 93 | // get file from request |
||||||||||||||||
| 94 | $file = $request->file($field_name); |
||||||||||||||||
| 95 | |||||||||||||||||
| 96 | // save file in folder and return name |
||||||||||||||||
| 97 | return store_file($path, $file); |
||||||||||||||||
| 98 | } |
||||||||||||||||
| 99 | } |
||||||||||||||||
| 100 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 101 | |||||||||||||||||
| 102 | if (!function_exists('upload_images')) { |
||||||||||||||||
| 103 | /** |
||||||||||||||||
| 104 | * @param $request |
||||||||||||||||
| 105 | * @param $field_name |
||||||||||||||||
| 106 | * @param $folder |
||||||||||||||||
| 107 | * |
||||||||||||||||
| 108 | * @return array |
||||||||||||||||
| 109 | */ |
||||||||||||||||
| 110 | function upload_images($request, $field_name, $folder) |
||||||||||||||||
| 111 | { |
||||||||||||||||
| 112 | // public path for folder |
||||||||||||||||
| 113 | $path = get_folder_path($folder); |
||||||||||||||||
| 114 | |||||||||||||||||
| 115 | $arrFileNames = []; |
||||||||||||||||
| 116 | // get just extension |
||||||||||||||||
| 117 | foreach ($request->file($field_name) as $file) { |
||||||||||||||||
| 118 | // save file in folder and return name |
||||||||||||||||
| 119 | $arrFileNames[] = store_file($path, $file); |
||||||||||||||||
| 120 | } |
||||||||||||||||
| 121 | |||||||||||||||||
| 122 | return $arrFileNames; |
||||||||||||||||
| 123 | } |
||||||||||||||||
| 124 | } |
||||||||||||||||
| 125 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 126 | |||||||||||||||||
| 127 | if (!function_exists('attach_files')) { |
||||||||||||||||
| 128 | /** |
||||||||||||||||
| 129 | * @param $request |
||||||||||||||||
| 130 | * @param $field_name |
||||||||||||||||
| 131 | * @param $folder |
||||||||||||||||
| 132 | * @param $row |
||||||||||||||||
| 133 | * @param $type |
||||||||||||||||
| 134 | * |
||||||||||||||||
| 135 | * @return array |
||||||||||||||||
| 136 | */ |
||||||||||||||||
| 137 | function attach_files($request, $field_name, $folder, $row = null) |
||||||||||||||||
| 138 | { |
||||||||||||||||
| 139 | |||||||||||||||||
| 140 | // public path for folder |
||||||||||||||||
| 141 | $path = get_folder_path($folder); |
||||||||||||||||
| 142 | |||||||||||||||||
| 143 | $arrFileNames = []; |
||||||||||||||||
| 144 | // get just extension |
||||||||||||||||
| 145 | foreach ($request->file($field_name) as $file) { |
||||||||||||||||
| 146 | // filename to store |
||||||||||||||||
| 147 | $original_name = $file->getClientOriginalName(); |
||||||||||||||||
| 148 | $size = $file->getSize(); |
||||||||||||||||
| 149 | |||||||||||||||||
| 150 | $type = getItemIfExists(explode('/', $file->getMimeType()), 0); |
||||||||||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||||||||||
| 151 | |||||||||||||||||
| 152 | // save file in folder and return name |
||||||||||||||||
| 153 | $storage_name = store_file($path, $file); |
||||||||||||||||
| 154 | |||||||||||||||||
| 155 | $arrFileNames[] = saveAttachments($request, $row, $original_name, $storage_name, $folder, $size, $type); |
||||||||||||||||
|
0 ignored issues
–
show
Are you sure the assignment to
$arrFileNames[] is correct as saveAttachments($request... $folder, $size, $type) seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||||||||||||
| 156 | } |
||||||||||||||||
| 157 | |||||||||||||||||
| 158 | return $arrFileNames; |
||||||||||||||||
| 159 | } |
||||||||||||||||
| 160 | } |
||||||||||||||||
| 161 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 162 | |||||||||||||||||
| 163 | if (!function_exists('saveAttachments')) { |
||||||||||||||||
| 164 | function saveAttachments($request, $row, $original_name, $storage_name, $folder, $size, $type) |
||||||||||||||||
|
0 ignored issues
–
show
The parameter
$storage_name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$folder is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$size is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$original_name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$type is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||||||||||
| 165 | { |
||||||||||||||||
| 166 | // $file = new Attachment(); |
||||||||||||||||
| 167 | // $file->file_name = $original_name; |
||||||||||||||||
| 168 | // $file->storage_name = $storage_name; |
||||||||||||||||
| 169 | // $file->path = $folder; |
||||||||||||||||
| 170 | // $file->size = $size; |
||||||||||||||||
| 171 | // $file->type = $type; |
||||||||||||||||
| 172 | // if ($row) { |
||||||||||||||||
| 173 | // $row->attachments()->save($file); |
||||||||||||||||
| 174 | // } else { |
||||||||||||||||
| 175 | // $file->attachable_id = $request->attachable_id; |
||||||||||||||||
| 176 | // $file->attachable_type = $request->attachable_type; |
||||||||||||||||
| 177 | // $file->save(); |
||||||||||||||||
| 178 | // } |
||||||||||||||||
| 179 | // |
||||||||||||||||
| 180 | // return $file; |
||||||||||||||||
| 181 | } |
||||||||||||||||
| 182 | } |
||||||||||||||||
| 183 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 184 | |||||||||||||||||
| 185 | if (!function_exists('unlink_file')) { |
||||||||||||||||
| 186 | /** |
||||||||||||||||
| 187 | * for delete file from directory. |
||||||||||||||||
| 188 | * |
||||||||||||||||
| 189 | * @param $fileName ( obj->file ) |
||||||||||||||||
|
0 ignored issues
–
show
|
|||||||||||||||||
| 190 | * @param $folderName ('uploads/folderName') |
||||||||||||||||
| 191 | */ |
||||||||||||||||
| 192 | function unlink_file($fileName, $folderName) |
||||||||||||||||
| 193 | { |
||||||||||||||||
| 194 | // get file source |
||||||||||||||||
| 195 | if ($fileName && $fileName != '') { |
||||||||||||||||
| 196 | $old = public_path($folderName.'/'.$fileName); |
||||||||||||||||
| 197 | if (File::exists($old)) { |
||||||||||||||||
| 198 | // unlink or remove previous image from folder |
||||||||||||||||
| 199 | unlink($old); |
||||||||||||||||
| 200 | } |
||||||||||||||||
| 201 | } |
||||||||||||||||
| 202 | } |
||||||||||||||||
| 203 | } |
||||||||||||||||
| 204 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 205 | |||||||||||||||||
| 206 | if (!function_exists('uploadFromTiny')) { |
||||||||||||||||
| 207 | /** |
||||||||||||||||
| 208 | * @param $request |
||||||||||||||||
| 209 | * @param string $field_name |
||||||||||||||||
| 210 | * @param $folder |
||||||||||||||||
| 211 | * |
||||||||||||||||
| 212 | * @return mixed |
||||||||||||||||
| 213 | */ |
||||||||||||||||
| 214 | function uploadFromTiny($request, $field_name, $folder) |
||||||||||||||||
| 215 | { |
||||||||||||||||
| 216 | try { |
||||||||||||||||
| 217 | $folder = "uploads/{$folder}"; |
||||||||||||||||
| 218 | |||||||||||||||||
| 219 | $file = $request->file($field_name); |
||||||||||||||||
| 220 | |||||||||||||||||
| 221 | $path = get_folder_path($folder); |
||||||||||||||||
| 222 | |||||||||||||||||
| 223 | $hash = 'image_'.time().'_'.$file->hashName(); |
||||||||||||||||
| 224 | |||||||||||||||||
| 225 | $filename = $file->move($path, $hash); |
||||||||||||||||
| 226 | |||||||||||||||||
| 227 | $path = asset("uploads/{$folder}/{$filename}"); |
||||||||||||||||
| 228 | |||||||||||||||||
| 229 | return response(['location' => $path], 200); |
||||||||||||||||
| 230 | } catch (Exception $exp) { |
||||||||||||||||
| 231 | return response(['location' => $exp], 401); |
||||||||||||||||
| 232 | } |
||||||||||||||||
| 233 | } |
||||||||||||||||
| 234 | } |
||||||||||||||||
| 235 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 236 | |||||||||||||||||
| 237 | if (!function_exists('fancyImage')) { |
||||||||||||||||
| 238 | /** |
||||||||||||||||
| 239 | * @param $prefix |
||||||||||||||||
| 240 | * @param $imageName |
||||||||||||||||
| 241 | * @param int $width |
||||||||||||||||
| 242 | * @param $alt |
||||||||||||||||
| 243 | * @param $className |
||||||||||||||||
| 244 | * |
||||||||||||||||
| 245 | * @return string |
||||||||||||||||
| 246 | */ |
||||||||||||||||
| 247 | function fancyImage($prefix, $imageName, $width = 100, $alt = null, $className = null) |
||||||||||||||||
| 248 | { |
||||||||||||||||
| 249 | $className = $className != null ? $className : 'img-thumbnail'; |
||||||||||||||||
| 250 | $height = $className == 'img-circle' ? $width : 'auto'; |
||||||||||||||||
| 251 | if (!file_exists((public_path("{$prefix}/{$imageName}")))) { |
||||||||||||||||
| 252 | return ''; |
||||||||||||||||
| 253 | } |
||||||||||||||||
| 254 | $output = "<a class='grouped_elements' data-fancybox='group' data-caption='{$imageName}' href='/{$prefix}/{$imageName}'>"; |
||||||||||||||||
| 255 | $output .= "<img src='/{$prefix}/{$imageName}' class='{$className}' width='{$width}' height='{$height}' alt='{$alt}'/>"; |
||||||||||||||||
| 256 | $output .= '</a>'; |
||||||||||||||||
| 257 | |||||||||||||||||
| 258 | return $output; |
||||||||||||||||
| 259 | } |
||||||||||||||||
| 260 | } |
||||||||||||||||
| 261 | /*---------------------------------- </> ----------------------------------*/ |
||||||||||||||||
| 262 |