Example #1
0
 public static function uninstall($package)
 {
     $package_folder = PKGPATH . $package;
     // Check to see if this package is already installed
     if (in_array($package, static::$protected)) {
         throw new Exception('Package "' . $package . '" cannot be uninstalled.');
         return false;
     }
     // Check to see if this package is already installed
     if (!is_dir($package_folder)) {
         throw new Exception('Package "' . $package . '" is not installed.');
         return false;
     }
     \Cli::write('Package "' . $package . '" was uninstalled.', 'yellow');
     \File::delete_dir($package_folder);
 }
Example #2
0
	public function uninstall($package)
	{
		$package_folder = PKGPATH . $package;

		// Check to see if this package is already installed
		if ( ! is_dir($package_folder))
		{
			\Cli::write(\Cli::color('Package "' . $package . '" is not installed.', 'red'));
			return;
		}

		\Cli::write('Uninstalling package "' . $package . '"');

		\File::delete_dir($package_folder);
	}
Example #3
0
 /**
  * Purge all caches
  *
  * @param	limit purge to subsection
  * @return	bool
  * @throws	Cache_Exception
  */
 public function delete_all($section)
 {
     $path = rtrim(static::$path, '\\/') . DS;
     $section = static::identifier_to_path($section);
     return \File::delete_dir($path . $section, true, false);
 }
Example #4
0
 /**
  * delete database documentation
  *
  * @access public
  * @return Response
  */
 public function action_delete($name = null)
 {
     $name === null and Response::redirect();
     try {
         File::delete_dir(DOCROOT . 'dbdocs' . DS . $name . DS);
         Session::set_flash('success', "Deleted database documentation \"{$name}\".");
     } catch (Fuel\Core\InvalidPathException $e) {
         Session::set_flash('error', "Documentation \"{$name}\" does not exist.");
     }
     Response::redirect();
 }
Example #5
0
    /**
     * Generate MySQL Documentation for HTML.
     *
     * Usage (from command line):
     *
     * php oil refine mydoc:html <table_schema> <output_dir = "app/tmp/">
     */
    public static function html($table_schema = null, $dir = null)
    {
        if (empty($table_schema)) {
            static::help();
            exit;
        }
        empty($dir) and $dir = APPPATH . 'tmp' . DS;
        $dir = rtrim($dir, DS) . DS . 'mydoc' . DS;
        /**
         * connect to db
         */
        $ret = static::connect($table_schema);
        /**
         * delete and create mydoc dir
         */
        if (file_exists($dir)) {
            if (!\Cli::option('f') and !\Cli::option('force')) {
                \Cli::write(realpath($dir) . ' already exist, please use -f option to force delete and generate.', 'red');
                exit;
            }
            $ret = \File::delete_dir($dir);
            if ($ret === false) {
                \Cli::write("Could not delete directory \"{$dir}\"", 'red');
                exit;
            }
        }
        $ret = mkdir($dir, 0777, true);
        if ($ret === false) {
            \Cli::write("Could not create directory \"{$dir}\"", 'red');
            exit;
        }
        \File::copy_dir(__DIR__ . DS . '..' . DS . 'assets', $dir . 'assets');
        /**
         * generate index.html
         */
        $migration_table_name = \Config::get('migrations.table', 'migration');
        $migration = array();
        if (\DBUtil::table_exists($migration_table_name)) {
            $migration = \Db::select()->from($migration_table_name)->order_by('migration', 'desc')->limit(1)->execute()->as_array();
        }
        $html = \View::forge('mydoc/index', array('migration' => $migration))->render();
        \File::create($dir, 'index.html', $html);
        /**
         * get tables
         */
        $tables = array_flip(\DB::list_tables());
        /**
         * unset ignore tables
         */
        foreach (\Config::get('mydoc.ignore_tables', array()) as $ignore_table_name) {
            if (isset($tables[$ignore_table_name])) {
                unset($tables[$ignore_table_name]);
            }
        }
        $ignore_table_regex = \Config::get('mydoc.ignore_table_regex');
        foreach ($tables as $table_name => $tmp) {
            if (!empty($ignore_table_regex)) {
                if (preg_match($ignore_table_regex, $table_name)) {
                    unset($tables[$table_name]);
                    continue;
                }
            }
            $tables[$table_name] = array('indexes' => array(), 'foreign_keys' => array(), 'triggers' => array());
        }
        /**
         * check table count
         */
        if (count($tables) === 0) {
            \Cli::write("No tables in \"{$table_schema}\"", 'red');
            exit;
        }
        /**
         * get foreign keys
         */
        $sql = 'select distinct
					table_name,
					column_name,
					referenced_table_name,
					referenced_column_name
				from
					information_schema.key_column_usage
				where
					referenced_table_name is not null
				and
					referenced_column_name is not null
				and
					table_schema = :table_schema';
        $foreign_keys = \Db::query($sql)->bind('table_schema', $table_schema)->execute()->as_array();
        foreach ($foreign_keys as $foreign_key) {
            if (isset($tables[$foreign_key['table_name']])) {
                $tables[$foreign_key['table_name']]['foreign_keys'][$foreign_key['column_name']] = $foreign_key;
            }
        }
        /**
         * get indexes
         */
        $sql = 'select distinct
					table_name,
					index_name,
					non_unique,
					column_name,
					comment
				from
					information_schema.statistics
				where
					table_schema = :table_schema';
        $indexes = \Db::query($sql)->bind('table_schema', $table_schema)->execute()->as_array();
        foreach ($indexes as $index) {
            if (isset($tables[$index['table_name']])) {
                $tables[$index['table_name']]['indexes'][$index['index_name']][$index['column_name']] = $index;
            }
        }
        /**
         * get triggers
         */
        $sql = 'select distinct
					trigger_name,
					event_manipulation,
					event_object_table,
					action_statement,
					action_timing,
					definer
				from
					information_schema.triggers
				where
					trigger_schema = :trigger_schema';
        $triggers = \Db::query($sql)->bind('trigger_schema', $table_schema)->execute()->as_array();
        foreach ($triggers as $trigger) {
            if (isset($tables[$trigger['event_object_table']])) {
                $tables[$trigger['event_object_table']]['triggers'][] = $trigger;
            }
        }
        /**
         * generate tables.html
         */
        $html = \View::forge('mydoc/tables', array('tables' => array_keys($tables)))->render();
        \File::create($dir, 'tables.html', $html);
        /**
         * generate table_*.html
         */
        foreach ($tables as $table_name => $infos) {
            $columns = \DB::list_columns($table_name);
            foreach ($columns as &$column) {
                // do we have a data_type defined? If not, use the generic type
                isset($column['data_type']) or $column['data_type'] = $column['type'];
                if ($column['data_type'] == 'enum') {
                    $column['data_type'] .= "('" . implode("', '", $column['options']) . "')";
                }
                $column['_length'] = null;
                foreach (array('length', 'character_maximum_length', 'display') as $idx) {
                    // check if we have such a column, and filter out some default values
                    if (isset($column[$idx]) and !in_array($column[$idx], array('65535', '16777215', '4294967295'))) {
                        $column['_length'] = $column[$idx];
                        break;
                    }
                }
                $column['_extras'] = array();
                if (strpos(\Str::lower($column['key']), 'pri') !== false) {
                    $column['_extras'][] = 'PK';
                }
                if (strpos(\Str::lower($column['key']), 'uni') !== false) {
                    $column['_extras'][] = 'UI';
                }
                if (!empty($column['extra'])) {
                    if (strpos($column['extra'], 'auto_increment') !== false) {
                        $column['_extras'][] = 'AI';
                    }
                }
                $column['_foreign_key'] = null;
                if (!empty($infos['foreign_keys'])) {
                    $foreign_key = \Arr::get($infos['foreign_keys'], $column['name'], array());
                    if (!empty($foreign_key)) {
                        $column['_foreign_key'] = $foreign_key;
                        $column['_extras'][] = 'FK';
                    }
                }
                if (!empty($column['_foreign_key'])) {
                    $column['_parent_table_name'] = $column['_foreign_key']['referenced_table_name'];
                } else {
                    $column['_foreign_key'] = array('referenced_table_name' => null, 'referenced_column_name' => null);
                    if (0 < preg_match('/^.+_id$/', $column['name'])) {
                        $parent_table_name = str_replace('_id', '', $column['name']);
                        if (isset($tables[$parent_table_name = \Inflector::singularize($parent_table_name)])) {
                            $column['_foreign_key'] = array('referenced_table_name' => $parent_table_name, 'referenced_column_name' => 'id');
                        } else {
                            if (isset($tables[$parent_table_name = \Inflector::pluralize($parent_table_name)])) {
                                $column['_foreign_key'] = array('referenced_table_name' => $parent_table_name, 'referenced_column_name' => 'id');
                            }
                        }
                    }
                }
            }
            $html = \View::forge('mydoc/table', array('table_name' => $table_name, 'columns' => $columns, 'infos' => $infos))->render();
            \File::create($dir, 'table_' . $table_name . '.html', $html);
        }
        /**
         * generate indexes.html
         */
        $html = \View::forge('mydoc/indexes', array('tables' => $tables))->render();
        \File::create($dir, 'indexes.html', $html);
        /**
         * generate triggers.html
         */
        $html = \View::forge('mydoc/triggers', array('tables' => $tables))->render();
        \File::create($dir, 'triggers.html', $html);
        \Cli::write("Generated MySQL Documentation in \"{$dir}\"", 'green');
        exit;
    }
Example #6
0
 public function delete_dir($path, $recursive = true, $delete_top = true)
 {
     return \File::delete_dir($path, $recursive, $delete_top, $this);
 }
Example #7
0
 /**
  * Generate database documentation
  *
  * @param  $dir string Documentation directory
  * @return mixed (true or error message)
  */
 public function generate($dir, $force = false)
 {
     /**
      * delete and create dbdocs dir
      */
     if (file_exists($dir)) {
         if ($force === false) {
             return "{$dir} already exist, please use -f option to force delete and generate.";
         }
         $ret = \File::delete_dir($dir);
         if ($ret === false) {
             return "Could not delete directory \"{$dir}\"";
         }
     }
     $ret = mkdir($dir, 0777, true);
     if ($ret === false) {
         return "Could not create directory \"{$dir}\"";
     }
     /**
      * copy assets
      */
     \File::copy_dir(__DIR__ . DS . '..' . DS . 'assets', $dir . 'assets');
     /**
      * get tables and views
      */
     $tables = $this->get_tables();
     $views = $this->get_views();
     foreach ($tables as $table) {
         $this->set_fuel_relations($table);
     }
     /**
      * generate index.html
      */
     $html = \ViewModel::forge('dbdocs/index')->set('information', $this->get_information())->set('description', $this->config['description'])->set('__tables', $tables)->set('__views', $views)->render();
     \File::create($dir, 'index.html', $html);
     /**
      * generate indexes.html
      */
     $html = \ViewModel::forge('dbdocs/indexes')->set('__tables', $tables)->render();
     \File::create($dir, 'indexes.html', $html);
     /**
      * generate tables.html
      */
     $html = \ViewModel::forge('dbdocs/tables')->set('__tables', $tables)->render();
     \File::create($dir, 'tables.html', $html);
     /**
      * generate table_*.html
      */
     foreach ($tables as $table) {
         /* @var $table \Doctrine\DBAL\Schema\Table */
         $html = \ViewModel::forge('dbdocs/table')->set('__table', $table)->render();
         \File::create($dir, 'table_' . $table->getName() . '.html', $html);
     }
     /**
      * generate views.html
      */
     $html = \ViewModel::forge('dbdocs/views')->set('__views', $views)->render();
     \File::create($dir, 'views.html', $html);
     /**
      * generate view_*.html
      */
     foreach ($views as $view) {
         /* @var $view \Doctrine\DBAL\Schema\View */
         $html = \ViewModel::forge('dbdocs/view')->set('__view', $view)->render();
         \File::create($dir, 'view_' . $view->getName() . '.html', $html);
     }
     return true;
 }
Example #8
0
 /**
  * This will copy the template app into the app dir overwriting what is there.
  * Cannot be undone
  * @return bool
  */
 public static function copyAppTemplate()
 {
     // classes
     if (is_dir(APPPATH . 'classes')) {
         \File::delete_dir(APPPATH . 'classes');
     }
     \File::copy_dir(CMFPATH . 'templates/app/classes', APPPATH . 'classes');
     // config
     if (is_dir(APPPATH . 'config')) {
         \File::delete_dir(APPPATH . 'config');
     }
     \File::copy_dir(CMFPATH . 'templates/app/config', APPPATH . 'config');
     // views
     if (is_dir(APPPATH . 'views')) {
         \File::delete_dir(APPPATH . 'views');
     }
     \File::copy_dir(CMFPATH . 'templates/app/views', APPPATH . 'views');
     return true;
 }
Example #9
0
 protected static function process_views($path, $module, $module_path)
 {
     $from_path = APPPATH . $path;
     $to_path = $module_path . $path;
     // If the necessary directory doesn't exist on the
     // module, we create it.
     if (!file_exists($to_path)) {
         mkdir($to_path, static::$folder_permissions, true);
     }
     // Here it is a little different as we are
     // processing each file in the directory.
     $files = \File::read_dir($from_path);
     foreach ($files as $file) {
         $file_from = $from_path . DS . $file;
         $file_to = $to_path . DS . $file;
         if (!file_exists($file_to) || static::$force) {
             // We do the changes we did earlier for the
             // post scaffold views.
             $content = \File::read($file_from, true);
             $content = str_replace('Html::anchor(\'', 'Html::anchor(\'' . $module . '/', $content);
             // We write the modified content in the
             // destination file.
             \Cli::write("\tCreating view: " . $file_to, 'green');
             file_put_contents($file_to, $content);
         }
     }
     // We delete the original file.
     \Cli::write("\tDeleting views: " . $from_path, 'green');
     \File::delete_dir($from_path);
 }
Example #10
0
 /**
  * Edit background of category
  *
  * @param int $id cat. ID
  *
  * @author Nguyen Van Hiep
  * @access public
  *
  * @version 1.0
  * @since 1.0
  */
 public function action_img($id = null)
 {
     $cat = Model_Categories::get_cat_with_expected_size($id);
     if (!$cat or $id < 4) {
         Session::set_flash('error', __('message.cat_not_exists'));
         Response::redirect('admin/categories');
     }
     $this->add_js('img_preparation.js');
     $up_dir = DOCROOT . 'assets/img/cat/temp/';
     $img_dir = DOCROOT . 'assets/img/cat/';
     $view = View::forge('admin/categories/img');
     $view->cat = $cat;
     $view->error = array();
     $view->img = '';
     $view->width = 0;
     $view->height = 0;
     $view->pw = 0;
     $view->ph = 0;
     $view->rat = $cat['sizes'];
     if (Input::post('submit') == 'upload') {
         // Custom configuration for this upload
         $config = array('path' => $up_dir, 'randomize' => false, 'ext_whitelist' => array('jpg', 'jpeg', 'gif', 'png'), 'max_size' => MAX_IMG_SIZE, 'auto_rename' => true, 'overwrite' => false, 'prefix' => 'c' . $id . '_');
         Upload::process($config);
         if (Upload::is_valid()) {
             File::delete_dir($up_dir, true, false);
             Upload::save();
             $info = Upload::get_files(0);
             $filepath = $info['saved_to'] . $info['saved_as'];
             $view->img = $info['saved_as'];
             list($view->width, $view->height) = getimagesize($filepath);
             list($view->pw, $view->ph) = explode(':', $cat['sizes']);
             Session::set_flash('success', __('message.slider_uploaded'));
         } else {
             $err = Upload::get_errors()[0]['errors'][0];
             $view->error['img'] = $err['message'];
         }
     }
     if (Input::post('submit') == 'save') {
         $x1 = Input::post('x1');
         $y1 = Input::post('y1');
         $x2 = Input::post('x2');
         $y2 = Input::post('y2');
         $w = Input::post('w');
         $h = Input::post('h');
         $img = Input::post('img');
         $scale = 1;
         $this->resize_img($img_dir . $img, $up_dir . $img, $w, $h, $x1, $y1, $scale);
         Model_Categories::save_bg($id, $img, $cat['bg']);
         Session::set_flash('success', __('message.img_resized'));
         Response::redirect('admin/categories');
     }
     $this->template->title = __('cat.edit');
     $this->template->content = $view;
 }