diff --git a/src/CodeGenerator.php b/src/CodeGenerator.php index 7637ebe7..aa82d063 100644 --- a/src/CodeGenerator.php +++ b/src/CodeGenerator.php @@ -168,7 +168,7 @@ public static function appendRoutes($config, $comm = null) $templateDirectory = __DIR__ . '/stubs'; LAHelper::log("info", "Appending routes...", $comm); - if(\Dwij\Laraadmin\Helpers\LAHelper::laravel_ver() == 5.3) { + if(\Dwij\Laraadmin\Helpers\LAHelper::is_recent_laravel_version()) { $routesFile = base_path('routes/admin_routes.php'); } else { $routesFile = app_path('Http/admin_routes.php'); diff --git a/src/Commands/LAInstall.php b/src/Commands/LAInstall.php index f916a152..9c178bd6 100644 --- a/src/Commands/LAInstall.php +++ b/src/Commands/LAInstall.php @@ -6,16 +6,13 @@ * Developed by: Dwij IT Solutions * Developer Website: http://dwijitsolutions.com */ - namespace Dwij\Laraadmin\Commands; - use Exception; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Dwij\Laraadmin\Helpers\LAHelper; use Eloquent; use DB; - /** * Class LAInstall * @package Dwij\Laraadmin\Commands @@ -26,19 +23,19 @@ class LAInstall extends Command { // Model Names to be handled during Install var $modelsInstalled = ["User", "Role", "Permission", "Employee", "Department", "Upload", "Organization", "Backup"]; - + // The command signature. protected $signature = 'la:install'; - + // The command description. protected $description = 'Install LaraAdmin Package. Generate whole structure for /admin.'; - + // Copy From Folder - Package Install Files protected $from; - + // Copy to Folder - Project Folder protected $to; - + /** * Generates and Moves files to install LaraAdmin package. * At the end runs migrations and ask to create Super Admin in order to complete the installation. @@ -49,40 +46,40 @@ public function handle() { try { $this->info('LaraAdmin installation started...'); - + $from = base_path('vendor/dwij/laraadmin/src/Installs'); $to = base_path(); - + $this->info('from: ' . $from . " to: " . $to); - + $this->line("\nDB Assistant:"); - if($this->confirm("Want to set your Database config in the .env file ?", true)) { + if($this->confirm("Want to set your Database config in the .env file?", true)) { $this->line("DB Assistant Initiated...."); $db_data = array(); - - if(LAHelper::laravel_ver() == 5.3) { + + if(LAHelper::is_recent_laravel_version()) { $db_data['host'] = $this->ask('Database Host', '127.0.0.1'); $db_data['port'] = $this->ask('Database Port', '3306'); } $db_data['db'] = $this->ask('Database Name', 'laraadmin1'); $db_data['dbuser'] = $this->ask('Database User', 'root'); $dbpass = $this->ask('Database Password', false); - + if($dbpass !== FALSE) { $db_data['dbpass'] = $dbpass; } else { $db_data['dbpass'] = ""; } - + $default_db_conn = env('DB_CONNECTION', 'mysql'); - - if(LAHelper::laravel_ver() == 5.3) { + + if(LAHelper::is_recent_laravel_version()) { config(['database.connections.' . $default_db_conn . '.host' => $db_data['host']]); config(['database.connections.' . $default_db_conn . '.port' => $db_data['port']]); LAHelper::setenv("DB_HOST", $db_data['host']); LAHelper::setenv("DB_PORT", $db_data['port']); } - + config(['database.connections.' . $default_db_conn . '.database' => $db_data['db']]); config(['database.connections.' . $default_db_conn . '.username' => $db_data['dbuser']]); config(['database.connections.' . $default_db_conn . '.password' => $db_data['dbpass']]); @@ -90,24 +87,27 @@ public function handle() LAHelper::setenv("DB_USERNAME", $db_data['dbuser']); LAHelper::setenv("DB_PASSWORD", $db_data['dbpass']); } - + if(env('CACHE_DRIVER') != "array") { config(['cache.default' => 'array']); LAHelper::setenv("CACHE_DRIVER", "array"); } - + $mix_file = 'gulpfile.js'; + if (LAHelper::laravel_ver() > 5.3) { + $mix_file = 'webpack.mix.js'; + } if($this->confirm("This process may change/append to the following of your existing project files:" . "\n\n\t app/Http/routes.php" . "\n\t app/User.php" . "\n\t database/migrations/2014_10_12_000000_create_users_table.php" - . "\n\t gulpfile.js" + . "\n\t $mix_file" . "\n\n Please take backup or use git. Do you wish to continue ?", true) ) { - + // Controllers $this->line("\n" . 'Generating Controllers...'); $this->copyFolder($from . "/app/Controllers/Auth", $to . "/app/Http/Controllers/Auth"); - if(LAHelper::laravel_ver() == 5.3) { + if(LAHelper::is_recent_laravel_version()) { // Delete Redundant Controllers unlink($to . "/app/Http/Controllers/Auth/PasswordController.php"); unlink($to . "/app/Http/Controllers/Auth/AuthController.php"); @@ -118,23 +118,23 @@ public function handle() unlink($to . "/app/Http/Controllers/Auth/ResetPasswordController.php"); } $this->replaceFolder($from . "/app/Controllers/LA", $to . "/app/Http/Controllers/LA"); - if(LAHelper::laravel_ver() == 5.3) { - $this->copyFile($from . "/app/Controllers/Controller.5.3.php", $to . "/app/Http/Controllers/Controller.php"); - } else { + if(LAHelper::is_recent_laravel_version()) { $this->copyFile($from . "/app/Controllers/Controller.php", $to . "/app/Http/Controllers/Controller.php"); + } else { + $this->copyFile($from . "/app/Controllers/Legacy.Controller.php", $to . "/app/Http/Controllers/Controller.php"); } $this->copyFile($from . "/app/Controllers/HomeController.php", $to . "/app/Http/Controllers/HomeController.php"); - + // Middleware - if(LAHelper::laravel_ver() == 5.3) { + if(LAHelper::is_recent_laravel_version()) { $this->copyFile($from . "/app/Middleware/RedirectIfAuthenticated.php", $to . "/app/Http/Middleware/RedirectIfAuthenticated.php"); } - - + + // Config $this->line('Generating Config...'); $this->copyFile($from . "/config/laraadmin.php", $to . "/config/laraadmin.php"); - + // Models $this->line('Generating Models...'); if(!file_exists($to . "/app/Models")) { @@ -143,10 +143,10 @@ public function handle() } foreach($this->modelsInstalled as $model) { if($model == "User") { - if(LAHelper::laravel_ver() == 5.3) { - $this->copyFile($from . "/app/Models/" . $model . "5.3.php", $to . "/app/" . $model . ".php"); - } else { + if(LAHelper::is_recent_laravel_version()) { $this->copyFile($from . "/app/Models/" . $model . ".php", $to . "/app/" . $model . ".php"); + } else { + $this->copyFile($from . "/app/Models/Legacy." . $model . ".php", $to . "/app/" . $model . ".php"); } } else if($model == "Role" || $model == "Permission") { $this->copyFile($from . "/app/Models/" . $model . ".php", $to . "/app/" . $model . ".php"); @@ -154,7 +154,7 @@ public function handle() $this->copyFile($from . "/app/Models/" . $model . ".php", $to . "/app/Models/" . $model . ".php"); } } - + // Custom Admin Route /* $this->line("\nDefault admin url route is /admin"); @@ -167,7 +167,7 @@ public function handle() config(['laraadmin.adminRoute' => $custom_admin_route]); } */ - + // Generate Uploads / Thumbnails folders in /storage $this->line('Generating Uploads / Thumbnails folders...'); if(!file_exists($to . "/storage/uploads")) { @@ -178,12 +178,12 @@ public function handle() $this->info("mkdir: (" . $to . "/storage/thumbnails)"); mkdir($to . "/storage/thumbnails"); } - + // la-assets $this->line('Generating LaraAdmin Public Assets...'); $this->replaceFolder($from . "/la-assets", $to . "/public/la-assets"); // Use "git config core.fileMode false" for ignoring file permissions - + // check CACHE_DRIVER to be array or else // It is required for Zizaco/Entrust // https://github.com/Zizaco/entrust/issues/468 @@ -192,24 +192,24 @@ public function handle() throw new Exception("Please set Cache Driver to array in .env (Required for Zizaco\Entrust) and run la:install again:" . "\n\n\tCACHE_DRIVER=array\n\n", 1); } - + // migrations $this->line('Generating migrations...'); $this->copyFolder($from . "/migrations", $to . "/database/migrations"); - + $this->line('Copying seeds...'); $this->copyFile($from . "/seeds/DatabaseSeeder.php", $to . "/database/seeds/DatabaseSeeder.php"); - - + + // resources $this->line('Generating resources: assets + views...'); $this->copyFolder($from . "/resources/assets", $to . "/resources/assets"); $this->copyFolder($from . "/resources/views", $to . "/resources/views"); - + // Checking database $this->line('Checking database connectivity...'); DB::connection()->reconnect(); - + // Running migrations... $this->line('Running migrations...'); $this->call('clear-compiled'); @@ -223,35 +223,35 @@ public function handle() $composer_path = "composer"; } $this->info(exec($composer_path . ' dump-autoload')); - + $this->call('migrate:refresh'); // $this->call('migrate:refresh', ['--seed']); - + // $this->call('db:seed', ['--class' => 'LaraAdminSeeder']); - + // $this->line('Running seeds...'); // $this->info(exec('composer dump-autoload')); $this->call('db:seed'); // Install Spatie Backup $this->call('vendor:publish', ['--provider' => 'Spatie\Backup\BackupServiceProvider']); - + // Edit config/database.php for Spatie Backup Configuration if(LAHelper::getLineWithString('config/database.php', "dump_command_path") == -1) { $newDBConfig = " 'driver' => 'mysql',\n" . " 'dump_command_path' => '/opt/lampp/bin', // only the path, so without 'mysqldump' or 'pg_dump'\n" . " 'dump_command_timeout' => 60 * 5, // 5 minute timeout\n" . " 'dump_using_single_transaction' => true, // perform dump using a single transaction\n"; - + $envfile = $this->openFile('config/database.php'); $mysqldriverline = LAHelper::getLineWithString('config/database.php', "'driver' => 'mysql'"); $envfile = str_replace($mysqldriverline, $newDBConfig, $envfile); file_put_contents('config/database.php', $envfile); } - + // Routes $this->line('Appending routes...'); //if(!$this->fileContains($to."/app/Http/routes.php", "laraadmin.adminRoute")) { - if(LAHelper::laravel_ver() == 5.3) { + if(LAHelper::is_recent_laravel_version()) { if(LAHelper::getLineWithString($to . "/routes/web.php", "require __DIR__.'/admin_routes.php';") == -1) { $this->appendFile($from . "/app/routes.php", $to . "/routes/web.php"); } @@ -262,30 +262,30 @@ public function handle() } $this->copyFile($from . "/app/admin_routes.php", $to . "/app/Http/admin_routes.php"); } - + // tests $this->line('Generating tests...'); $this->copyFolder($from . "/tests", $to . "/tests"); - if(LAHelper::laravel_ver() == 5.3) { + if(LAHelper::is_recent_laravel_version()) { unlink($to . '/tests/TestCase.php'); rename($to . '/tests/TestCase5.3.php', $to . '/tests/TestCase.php'); } else { unlink($to . '/tests/TestCase5.3.php'); } - + // Utilities $this->line('Generating Utilities...'); // if(!$this->fileContains($to."/gulpfile.js", "admin-lte/AdminLTE.less")) { - if(LAHelper::getLineWithString($to . "/gulpfile.js", "mix.less('admin-lte/AdminLTE.less', 'public/la-assets/css');") == -1) { - $this->appendFile($from . "/gulpfile.js", $to . "/gulpfile.js"); + if(LAHelper::getLineWithString($to . '/'.$mix_file, "mix.less('admin-lte/AdminLTE.less', 'public/la-assets/css');") == -1) { + $this->appendFile($from . '/'.$mix_file, $to . '/'.$mix_file); } // Creating Super Admin User - + $user = \App\User::where('context_id', "1")->first(); if(!isset($user['id'])) { - + $this->line('Creating Super Admin User...'); - + $data = array(); $data['name'] = $this->ask('Super Admin name', 'Super Admin'); $data['email'] = $this->ask('Super Admin email', 'user@example.com'); @@ -293,10 +293,10 @@ public function handle() $data['context_id'] = "1"; $data['type'] = "Employee"; $user = \App\User::create($data); - + // TODO: This is Not Standard. Need to find alternative Eloquent::unguard(); - + \App\Models\Employee::create([ 'name' => $data['name'], 'designation' => "Super Admin", @@ -313,7 +313,7 @@ public function handle() 'date_left' => date("Y-m-d"), 'salary_cur' => 0, ]); - + $this->info("Super Admin User '" . $data['name'] . "' successfully created. "); } else { $this->info("Super Admin User '" . $user['name'] . "' exists. "); @@ -322,7 +322,7 @@ public function handle() $user->attachRole($role); $this->info("\nLaraAdmin successfully installed."); $this->info("You can now login from yourdomain.com/" . config('laraadmin.adminRoute') . " !!!\n"); - + } else { $this->error("Installation aborted. Please try again after backup / git. Thank you..."); } @@ -336,7 +336,7 @@ public function handle() } } } - + /** * Copy Folder contents * @@ -348,7 +348,7 @@ private function copyFolder($from, $to) // $this->info("copyFolder: ($from, $to)"); LAHelper::recurse_copy($from, $to); } - + /** * Replace Folder contents by deleting content of to folder first * @@ -363,7 +363,7 @@ private function replaceFolder($from, $to) } LAHelper::recurse_copy($from, $to); } - + /** * Copy file contents. If file not exists create it. * @@ -379,7 +379,7 @@ private function copyFile($from, $to) } copy($from, $to); } - + /** * Get file contents * @@ -391,7 +391,7 @@ private function openFile($from) $md = file_get_contents($from); return $md; } - + /** * Append content of 'from' file to 'to' file * @@ -401,12 +401,12 @@ private function openFile($from) private function appendFile($from, $to) { // $this->info("appendFile: ($from, $to)"); - + $md = file_get_contents($from); - + file_put_contents($to, $md, FILE_APPEND); } - + /** * Copy contents from one file to another * @@ -418,7 +418,7 @@ private function writeFile($from, $to) $md = file_get_contents($from); file_put_contents($to, $md); } - + /** * does file contains given text * @@ -429,7 +429,7 @@ private function writeFile($from, $to) private function fileContains($filePath, $text) { // TODO: Method not working properly - + $fileData = file_get_contents($filePath); if(strpos($fileData, $text) === false) { return true; diff --git a/src/Commands/Packaging.php b/src/Commands/Packaging.php index 08a6be0d..57e5b37e 100644 --- a/src/Commands/Packaging.php +++ b/src/Commands/Packaging.php @@ -71,7 +71,7 @@ public function handle() // Routes $this->line('Exporting Routes...'); - if(LAHelper::laravel_ver() == 5.3) { + if(LAHelper::is_recent_laravel_version()) { // $this->copyFile($from."/routes/web.php", $to."/app/routes.php"); // Not needed anymore $this->copyFile($from . "/routes/admin_routes.php", $to . "/app/admin_routes.php"); } else { diff --git a/src/Controllers/ModuleController.php b/src/Controllers/ModuleController.php index 1844b6ce..b201130f 100644 --- a/src/Controllers/ModuleController.php +++ b/src/Controllers/ModuleController.php @@ -165,7 +165,7 @@ public function destroy($id) } // Delete Admin Routes - if(LAHelper::laravel_ver() == 5.3) { + if(LAHelper::is_recent_laravel_version()) { $file_admin_routes = base_path("/routes/admin_routes.php"); } else { $file_admin_routes = base_path("/app/Http/admin_routes.php"); diff --git a/src/Helpers/LAHelper.php b/src/Helpers/LAHelper.php index 3cf4703e..feeaf303 100644 --- a/src/Helpers/LAHelper.php +++ b/src/Helpers/LAHelper.php @@ -22,6 +22,8 @@ */ class LAHelper { + + const RECENT_LARAVEL = 5.3; /** * Gives various names of Module in Object like label, table, model, controller, singular * @@ -36,7 +38,7 @@ public static function generateModuleNames($module_name, $icon) $array = array(); $module_name = trim($module_name); $module_name = str_replace(" ", "_", $module_name); - + $array['module'] = ucfirst(str_plural($module_name)); $array['label'] = ucfirst(str_plural($module_name)); $array['table'] = strtolower(str_plural($module_name)); @@ -45,10 +47,10 @@ public static function generateModuleNames($module_name, $icon) $array['controller'] = $array['module'] . "Controller"; $array['singular_l'] = strtolower(str_singular($module_name)); $array['singular_c'] = ucfirst(str_singular($module_name)); - + return (object)$array; } - + /** * Get list of Database tables excluding LaraAdmin Context tables like * backups, la_configs, la_menus, migrations, modules, module_fields, module_field_types @@ -84,7 +86,7 @@ public static function getDBTables($remove_tables = []) } else { $tables = DB::select('SHOW TABLES'); } - + $tables_out = array(); foreach($tables as $table) { $table = (Array)$table; @@ -112,15 +114,15 @@ public static function getDBTables($remove_tables = []) $remove_tables = array_merge($remove_tables, $remove_tables2); $remove_tables = array_unique($remove_tables); $tables_out = array_diff($tables_out, $remove_tables); - + $tables_out2 = array(); foreach($tables_out as $table) { $tables_out2[$table] = $table; } - + return $tables_out2; } - + /** * Get Array of All Modules * @@ -132,21 +134,21 @@ public static function getDBTables($remove_tables = []) public static function getModuleNames($remove_modules = []) { $modules = Module::all(); - + $modules_out = array(); foreach($modules as $module) { $modules_out[] = $module->name; } $modules_out = array_diff($modules_out, $remove_modules); - + $modules_out2 = array(); foreach($modules_out as $module) { $modules_out2[$module] = $module; } - + return $modules_out2; } - + /** * Method to parse the dropdown, Multiselect, Taginput and radio values which are linked with * either other tables via "@" e.g. "@employees" or string array of values @@ -182,7 +184,7 @@ public static function parseValues($value) } return $valueOut; } - + /** * Log method to log either in command line or in Log file depending on $type. * @@ -203,7 +205,7 @@ public static function log($type, $text, $commandObject) Log::$type($text); } } - + /** * Method copies folder recursively into another * @@ -230,7 +232,7 @@ public static function recurse_copy($src, $dst) } closedir($dir); } - + /** * Method deletes folder and its content * @@ -253,7 +255,7 @@ public static function recurse_delete($dir) rmdir($dir); } } - + /** * Generate Random Password * @@ -283,7 +285,7 @@ public static function gen_password($chars_min = 6, $chars_max = 8, $use_upper_c } return $password; } - + /** * Get url of image by using $upload_id * @@ -301,7 +303,7 @@ public static function img($upload_id) return ""; } } - + /** * Get Thumbnail image path of Uploaded image * @@ -355,7 +357,7 @@ public static function createThumbnail($filepath, $thumbpath, $thumbnail_width, $imgt($new_image, $thumbpath); return file_exists($thumbpath); } - + /** * Print the menu editor view. * This needs to be done recursively @@ -367,9 +369,11 @@ public static function createThumbnail($filepath, $thumbpath, $thumbnail_width, */ public static function print_menu_editor($menu) { + $editing = \Collective\Html\FormFacade::open(['route' => [config('laraadmin.adminRoute') . '.la_menus.destroy', $menu->id], 'method' => 'delete', 'style' => 'display:inline']); $editing .= ''; $editing .= \Collective\Html\FormFacade::close(); + if($menu->type != "module") { $info = (object)array(); $info->id = $menu->id; @@ -377,15 +381,16 @@ public static function print_menu_editor($menu) $info->url = $menu->url; $info->type = $menu->type; $info->icon = $menu->icon; - + $editing .= ''; + // dd($editing); } $str = '
  • ' . $menu->name . ' ' . $editing . '
    '; - + $childrens = \Dwij\Laraadmin\Models\Menu::where("parent", $menu->id)->orderBy('hierarchy', 'asc')->get(); - + if(count($childrens) > 0) { $str .= '
      '; foreach($childrens as $children) { @@ -394,9 +399,10 @@ public static function print_menu_editor($menu) $str .= '
    '; } $str .= '
  • '; + return $str; } - + /** * Print the sidebar menu view. * This needs to be done recursively @@ -420,9 +426,9 @@ public static function print_menu($menu, $active = false) if($active) { $active_str = 'class="active"'; } - + $str = 'url) . '"> ' . LAHelper::real_module_name($menu->name) . ' ' . $subviewSign . ''; - + if(count($childrens)) { $str .= '