Changeset c6b84df for app


Ignore:
Timestamp:
10/21/21 23:45:59 (3 years ago)
Author:
beratkjufliju <kufliju@…>
Branches:
develop, master
Children:
4b7e2d3
Parents:
6b95845
Message:

added fileTypes controller, notifications, excel export, edited views

Location:
app
Files:
13 added
2 deleted
10 edited
2 moved

Legend:

Unmodified
Added
Removed
  • app/Http/Controllers/Dashboard/DepartmentsController.php

    r6b95845 rc6b84df  
    77use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
    88use App\Models\Department;
    9 use App\Models\Document;
    109use App\Models\File;
    1110use App\Models\User;
     11use App\Notifications\NewDepartmentCreated;
    1212use Carbon\Carbon;
    13 use Illuminate\Http\Request;
    1413use App\Http\Controllers\Controller;
    15 use Illuminate\Support\Facades\Auth;
     14use Illuminate\Support\Facades\Notification;
    1615use Illuminate\Support\Facades\Storage;
    17 use function Illuminate\Events\queueable;
     16use Illuminate\Filesystem\Filesystem;
    1817
    1918class DepartmentsController extends Controller
     
    4039        $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
    4140
    42         if(!Storage::disk('local')->has($location)){
    43             Storage::disk('local')->makeDirectory($location);
     41        if(!Storage::disk('uploads')->has($location)){
     42            Storage::disk('uploads')->makeDirectory($location);
    4443
    4544        }
    46         $department->location = Storage::disk('local')->path('') . $location;
     45        $department->location = $location;
    4746        $department->user_id = auth()->id();
     47
     48        $users = User::all();
     49        Notification::send($users, new NewDepartmentCreated("New department created"));
    4850
    4951        $department->save();
     
    6567        $department = Department::findOrFail($id);
    6668
    67         $documents = $department->document;
     69        $folders = $department->folder;
    6870        $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
    6971
    7072        $department->name = $request->name;
    7173        $department->code = $request->code;
     74        $department->updated_at = Carbon::now();
    7275
    7376        if($department->isDirty('code'))
    7477        {
    7578            $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
    76             if(!Storage::disk('local')->has($location)){
    77                 Storage::disk('local')->move($oldLocation, $location);
    78                 $department->location = Storage::disk('local')->path('') . $location;
     79            if(!Storage::disk('uploads')->has($location)){
     80                Storage::disk('uploads')->move($oldLocation, $location);
     81                $department->location = $location;
    7982            }
    8083
    81             foreach ($documents as $document) {
    82                 foreach($document->files as $file) {
    83                     $file->location = $location . DIRECTORY_SEPARATOR . $document->name . DIRECTORY_SEPARATOR . $file->name;
     84            foreach ($folders as $folder) {
     85                    $currArchId = explode('/', $folder->arch_id)[1];
     86                    $folder->arch_id = $department->code . '/' . $currArchId;
     87                    $folder->save();
     88                foreach($folder->files as $file) {
     89                    $file->location = $location . DIRECTORY_SEPARATOR . $folder->name . DIRECTORY_SEPARATOR . $file->name;
    8490                    $file->save();
    8591                }
     
    98104        $department = Department::find($id);
    99105        //$department->delete();
    100         $documents = $department->document()->count();
     106        $folders = $department->folder()->count();
    101107
    102         if($documents > 0){
    103             Alert::flash($department->name . " has " . $documents . " document/s associated", "error");
     108        if($folders > 0){
     109            Alert::flash($department->name . " has " . $folders . " document/s associated", "error");
    104110        }
    105111        else {
     
    111117        return redirect()->route("dashboard.departments.index");
    112118    }
     119
     120    public function downloadAll()
     121    {
     122        $zip_file=Storage::disk('uploads')->path('Departments.zip');
     123        $zip = new \ZipArchive();
     124        $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
     125        $path = Storage::disk('uploads')->path('Departments');
     126        $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
     127        $flag=false;
     128
     129        foreach ($files as $file)
     130        {
     131            if(File::all()->count() > 0) {
     132
     133                // We're skipping all subfolders
     134                if (!$file->isDir()) {
     135                    $filePath = $file->getRealPath();
     136                    // extracting filename with substr/strlen
     137                    $relativePath = substr($filePath, strlen($path) + 1);
     138                    $zip->addFile($filePath, $relativePath);
     139                }
     140            }
     141            else
     142            {
     143                $flag=true;
     144            break;
     145            }
     146        }
     147        if(!$flag) {
     148            $zip->close();
     149            $headers = array('Content-Type' => 'application/octet-stream',);
     150            $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
     151            return response()->download($zip_file, $zip_new_name, $headers);
     152        }
     153        else {
     154            Alert::flash("All departments are empty", "warning");
     155            return redirect()->route("dashboard.departments.index");
     156        }
     157    }
     158
     159    public function downloadDepartment($id)
     160    {
     161        $department = Department::find($id);
     162
     163        $FileSystem = new Filesystem();
     164        $zip_file = Storage::disk('uploads')->path('Department.zip');
     165        $zip = new \ZipArchive();
     166        $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
     167        $path = Storage::disk('uploads')->path($department->location) . DIRECTORY_SEPARATOR;
     168        $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
     169
     170        $filesInDept = $FileSystem->allFiles($path);
     171
     172        if(!empty($filesInDept)) {
     173        foreach ($files as $file) {
     174            if (!$file->isDir()) {
     175                $filePath = $file->getRealPath();
     176                // extracting filename with substr/strlen
     177                $relativePath = substr($filePath, strlen($path) + 1);
     178                $zip->addFile($filePath, $relativePath);
     179            }
     180        }
     181        $zip->close();
     182        $headers = array('Content-Type' => 'application/octet-stream',);
     183            $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . $department->name . '.zip';
     184        return response()->download($zip_file, $zip_new_name, $headers);
     185        }
     186        else{
     187            Alert::flash("This department has no files", "warning");
     188            return redirect()->route("dashboard.departments.index");
     189        }
     190
     191    }
    113192}
  • app/Http/Controllers/Dashboard/IndexController.php

    r6b95845 rc6b84df  
    44
    55use App\Helpers\Alert;
     6use App\Models\Department;
     7use App\Models\Folder;
     8use App\Models\File;
    69use App\Models\User;
    710use App\Http\Controllers\Controller;
     
    1720        return view("dashboard.index")->with([
    1821            "counters" => $counters,
     22            "departments" => Department::all(),
     23            "folders" => Folder::all(),
     24            "files" => File::all()
    1925        ]);
    2026    }
  • app/Http/Controllers/Dashboard/SettingsController.php

    r6b95845 rc6b84df  
    55use App\Helpers\Alert;
    66use App\Http\Requests\Dashboard\EmailSettingsRequest;
     7use App\Http\Requests\Dashboard\FileTypeRequest;
    78use App\Http\Requests\Dashboard\PasswordSettingsRequest;
    89use App\Http\Requests\Dashboard\UsernameSettingsRequest;
     10use App\Models\FileType;
    911use App\Models\User;
    1012use App\Http\Controllers\Controller;
     
    1921    public function settings()
    2022    {
     23        $fileType = FileType::find("1");
     24
    2125        return view("dashboard.settings.index")->with([
    2226            "user" => auth()->user(),
    2327            "adminAndReferents" => User::where("role_id", 1)->orWhere("role_id", 2)->get(),
    24             "active_tab" => "account"
     28            "active_tab" => "account",
     29            "fileType" => $fileType
    2530        ]);
    2631    }
     
    3035            $user = auth()->user();
    3136            $user->username = $request->username;
     37            $user->updated_at = Carbon::now();
    3238            $user->save();
    3339
     
    4248            $user = auth()->user();
    4349            $user->password = bcrypt($request->password);
     50            $user->updated_at = Carbon::now();
    4451            $user->save();
    4552
     
    5865        $user->security_code = $user->generateSecurityCode();
    5966        $user->verify_token = $user->generateVerifyToken();
     67        $user->updated_at = Carbon::now();
    6068
    6169        $user->save();
     
    6977    }
    7078
     79
     80
     81    public function fileTypes(FileTypeRequest $request)
     82    {
     83        $fileType = FileType::find("1");
     84
     85        $fileType->mimes = $request->mimes;
     86        $fileType->max_size = $request->max_size;
     87        $fileType->user_id = auth()->id();
     88        $fileType->updated_at = Carbon::now();
     89
     90        if(auth()->user()->hasPermission("manage_file_types")) {
     91            $fileType->save();
     92
     93            Alert::flash("File validations updated successfully");
     94
     95            return redirect()->back();
     96        }
     97        else {
     98            Alert::flash("You don't have permission to change file validations", "error");
     99
     100            return redirect()->back();
     101        }
     102    }
     103
    71104}
  • app/Http/Controllers/Dashboard/UsersController.php

    r6b95845 rc6b84df  
    2424    {
    2525        return view("dashboard.users.index")->with([
    26             "users" => User::all()
     26            "users" => User::all(),
     27            "roles" => Role::all(),
    2728        ]);
    2829    }
     
    6263            $user->avatar = $avatarName;
    6364        }
     65
     66        $user->created_by = auth()->user()->id;
    6467        $user->save();
    6568
     
    8891        $user->phone_number = $request->phone_number;
    8992        $user->role_id = $request->userRole;
    90         $user->updated_at = $request->Carbon::now();;
     93        $user->updated_at = Carbon::now();
    9194
    9295        if ($request->hasFile("avatar")) {
  • app/Http/Requests/Dashboard/FileRequest.php

    r6b95845 rc6b84df  
    33namespace App\Http\Requests\Dashboard;
    44
     5use App\Helpers\Alert;
     6use App\Models\FileType;
     7use App\Rules\UploadCount;
    58use Illuminate\Foundation\Http\FormRequest;
    69
     
    2427    public function rules()
    2528    {
    26         return [
    27             "name" => "required|max:255|regex:/^[^.]+$/",
    28         ];
     29        $rules = [
     30            "folder" => "required|integer|exists:folders,id",
     31            ];
     32
     33        $mimes = FileType::find("1")->mimes;
     34        $maxSize = FileType::find("1")->max_size;
     35
     36        if ($this->isMethod("patch")) {
     37            $fileRules = [
     38                "file_item.*" => "mimes:{$mimes}|max:{$maxSize}"
     39            ];
     40        }
     41
     42        else {
     43            $fileRules = [
     44                "file_item.*" => "mimes:{$mimes}|max:{$maxSize}"
     45            ];
     46        }
     47
     48        $rules = array_merge(
     49            $rules,
     50            $fileRules,
     51        );
     52
     53        return $rules;
    2954    }
    3055}
  • app/Http/Requests/Dashboard/NewUserRequest.php

    r6b95845 rc6b84df  
    2929            "phone_number" => "required|unique:users,phone_number",
    3030            "email" => "required|string|email|max:50|unique:users,email",
    31             "username" => "required|min:5|max:30|unique:users,username",
     31            "username" => "required|alpha_dash|min:5|unique:users,username",
    3232            "userRole" => "required|exists:roles,id",
    33             "avatar" => "mimes:jpeg,png,gif|max:5000",
     33            "avatar" => "image|max:5000",
    3434        ];
    3535    }
  • app/Http/Requests/Dashboard/UpdateUserRequest.php

    r6b95845 rc6b84df  
    2929            "phone_number" => "required|unique:users,phone_number,$this->id,id",
    3030            "email" => "required|string|email|max:50|unique:users,email,$this->id,id",
    31             "username" => "required|min:5|unique:users,username,$this->id,id",
     31            "username" => "required|alpha_dash|min:5|unique:users,username,$this->id,id",
    3232            "userRole" => "required|exists:roles,id",
    3333            "avatar" => "mimes:jpeg,png,gif|max:5000",
  • app/Models/Department.php

    r6b95845 rc6b84df  
    2525    }
    2626
    27     public function document(){
    28         return $this->hasMany(Document::class);
     27    public function folder(){
     28        return $this->hasMany(Folder::class);
    2929    }
    3030}
  • app/Models/File.php

    r6b95845 rc6b84df  
    1111    protected $fillable = ["name", "location", "document_id"];
    1212
    13     public function document()
     13    public function folder()
    1414    {
    15         return $this->belongsTo(Document::class);
     15        return $this->belongsTo(Folder::class);
    1616    }
    1717
    1818    public function getSize($location)
    1919    {
    20         $fileSize = Storage::disk('local')->size($location) / 1024 / 1024;
     20        $fileSize = Storage::disk('uploads')->size($location) / 1024 / 1024;
    2121        $fileSize = round($fileSize, 2);
    2222        return $fileSize;
  • app/Models/Folder.php

    r6b95845 rc6b84df  
    66use Illuminate\Database\Eloquent\Model;
    77
    8 class Document extends Model
     8class Folder extends Model
    99{
    1010    use HasFactory;
    11     protected $table = "documents";
     11    protected $table = "folders";
    1212
    13     protected $fillable = ["arch_id", "name", "description", "user_id", "department_id", "is_important"];
     13    protected $fillable = ["arch_id", "name", "description", "location", "user_id", "department_id", "is_important"];
    1414
    1515
  • app/Models/User.php

    r6b95845 rc6b84df  
    2424        "surname",
    2525        "username",
     26        "phone_number",
     27        "email",
    2628        "password",
    27         "email",
    28         "phone_number",
    2929        "avatar",
    3030        "role_id"
  • app/Notifications/NewFileCreated.php

    r6b95845 rc6b84df  
    77use Illuminate\Notifications\Messages\MailMessage;
    88
    9 class NewDocumentCreated extends Notification
     9class NewFileCreated extends Notification
    1010{
    1111    use Queueable;
     
    5757    {
    5858        return [
    59             "url" => "/dashboard/documents",
     59            "url" => "/dashboard/files",
    6060            "message" => $this->text
    6161        ];
Note: See TracChangeset for help on using the changeset viewer.