update wizzard added
This commit is contained in:
parent
5e1e62f65b
commit
f7238b7690
|
@ -46,5 +46,6 @@ return array(
|
|||
'site_keywords' => '',
|
||||
'site_description' => '',
|
||||
'site_window_title' => true,
|
||||
'config_version' => 1
|
||||
'config_version' => 1,
|
||||
'db_version' => 1
|
||||
);
|
|
@ -99,6 +99,7 @@ return array(
|
|||
'Delete users' => 'Удалять пользователей',
|
||||
'Upload files' => 'Загружать файлы',
|
||||
'Delete files' => 'Удалять файлы',
|
||||
'Download files' => 'Скачивать файлы',
|
||||
'View files list' => 'Просматривать список файлов',
|
||||
'Upload images' => 'Загружать изображения',
|
||||
'Delete images' => 'Удалять изображения',
|
||||
|
|
19
languages/ru/update.php
Normal file
19
languages/ru/update.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
return array(
|
||||
'Update' => 'Обновить',
|
||||
'Zira CMS update wizzard' => 'Мастер обновления Zira CMS',
|
||||
'Zira CMS is ready to update.' => 'Zira CMS готова к обновлению.',
|
||||
'Database version: %s' => 'Версия базы данных: %s',
|
||||
'Database needs to be updated to version: %s' => 'База данных должна быть обновлена до версии: %s',
|
||||
'Error' => 'Ошибка',
|
||||
'Message' => 'Сообщение',
|
||||
'Close' => 'Закрыть',
|
||||
'An error occurred' => 'Возникла ошибка',
|
||||
'Please wait...' => 'Пожалуйста, подождите...',
|
||||
'Please wait' => 'Пожалуйста, подождите',
|
||||
'Update in progress' => 'Идёт обновление',
|
||||
'Updated successfully!' => 'Успешно обновлено!',
|
||||
'An error occurred.' => 'Возникла ошибка.',
|
||||
'Database is up to date.' => 'База данных обновлена.'
|
||||
);
|
219
update/index.php
Normal file
219
update/index.php
Normal file
|
@ -0,0 +1,219 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* index.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
require('..' . DIRECTORY_SEPARATOR . 'const.php');
|
||||
if (file_exists('../config.php')) $config = @include '../config.php';
|
||||
if (empty($config) || !is_array($config)) {
|
||||
http_response_code(403);
|
||||
die('Zira is not installed');
|
||||
}
|
||||
|
||||
chdir('..');
|
||||
define('ZIRA_UPDATE', true);
|
||||
define('REAL_PATH', realpath(ROOT_DIR));
|
||||
|
||||
error_reporting(E_ALL);
|
||||
function error_handler($severity, $message, $file, $line) {
|
||||
if (LOG_ERRORS) {
|
||||
Zira\Log::write(Zira\Log::getErrorType($severity).': '.$message.' in '.$file.':'.$line);
|
||||
}
|
||||
throw new ErrorException(Zira\Log::getErrorType($severity).': '.$message, 0, $severity, $file, $line);
|
||||
}
|
||||
set_error_handler('error_handler', E_ALL);
|
||||
|
||||
function shutdown_handler() {
|
||||
$error = error_get_last();
|
||||
if (!$error) {
|
||||
Zira::getInstance()->shutdown();
|
||||
} else if (isset($error['type']) && $error['type']==E_ERROR) {
|
||||
// trying to log fatal errors
|
||||
$message = isset($error['message']) ? $error['message'] : 'unknown error';
|
||||
$file = isset($error['file']) ? $error['file'] : 'unknown file';
|
||||
$line = isset($error['line']) ? $error['line'] : 'unknown line';
|
||||
Zira\Log::write('Fatal error: '.$message.' in '.$file.':'.$line);
|
||||
}
|
||||
}
|
||||
register_shutdown_function('shutdown_handler');
|
||||
|
||||
spl_autoload_extensions('.php');
|
||||
spl_autoload_register();
|
||||
|
||||
try {
|
||||
\Zira\Config::setSystemDefaults($config);
|
||||
unset($config);
|
||||
} catch (Exception $e) {
|
||||
Zira\Response::exception($e);
|
||||
}
|
||||
|
||||
$languages = array();
|
||||
$d=opendir(ROOT_DIR . DIRECTORY_SEPARATOR . LANGUAGES_DIR);
|
||||
while(($f=readdir($d))!==false) {
|
||||
if ($f=='.' || $f=='..') continue;
|
||||
if (!is_dir(ROOT_DIR . DIRECTORY_SEPARATOR .LANGUAGES_DIR . DIRECTORY_SEPARATOR . $f)) continue;
|
||||
$languages []= $f;
|
||||
}
|
||||
closedir($d);
|
||||
|
||||
$language = Zira\Request::get('lang', 'ru');
|
||||
if (!in_array($language, $languages)) $language = 'en';
|
||||
|
||||
Zira\Log::init();
|
||||
Zira\Session::start();
|
||||
Zira\Db\Loader::initialize();
|
||||
Zira\Db\Db::open();
|
||||
Zira\Config::load();
|
||||
Zira\Datetime::init();
|
||||
|
||||
Zira\Locale::load($language, 'update');
|
||||
if (Zira\Locale::getLanguage() && Zira\Config::get('db_translates')) {
|
||||
Zira\Locale::loadDBStrings();
|
||||
}
|
||||
|
||||
$version = Zira::VERSION;
|
||||
$db_version = Zira\Config::get('db_version', 0);
|
||||
|
||||
$v_arr = array();
|
||||
$d = opendir(ROOT_DIR . DIRECTORY_SEPARATOR . 'update');
|
||||
while(($f = readdir($d))!==false) {
|
||||
if (!is_dir(ROOT_DIR . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . $f)) continue;
|
||||
if (strpos($f, 'v')!==0 || strlen($f)<=1) continue;
|
||||
$v = intval(substr($f, 1));
|
||||
if ($v>0 && !in_array($v, $v_arr)) {
|
||||
$v_arr []= $v;
|
||||
}
|
||||
}
|
||||
sort($v_arr);
|
||||
$db_update_version = $v_arr[count($v_arr)-1];
|
||||
|
||||
Zira\View::setTheme(DEFAULT_THEME);
|
||||
Zira\View::setRenderJsStrings(false);
|
||||
Zira\View::setRenderBreadcrumbs(false);
|
||||
Zira\View::addDefaultAssets();
|
||||
//Zira\View::addThemeAssets();
|
||||
Zira\View::addWidget('\Zira\Widgets\Logo');
|
||||
|
||||
Zira\Helper::setAddingLanguageToUrl(false);
|
||||
$html = Zira\Helper::tag_open('ul', array('id'=>'language-switcher'));
|
||||
foreach($languages as $_language) {
|
||||
if ($_language == $language) $class='active';
|
||||
else $class = '';
|
||||
$html .= Zira\Helper::tag_open('li');
|
||||
$html .= Zira\Helper::tag('a', Zira\Helper::html(ucfirst($_language)), array('href'=>'?lang='.Zira\Helper::html($_language), 'class'=>$class));
|
||||
$html .= Zira\Helper::tag_close('li');
|
||||
}
|
||||
$html .= Zira\Helper::tag_close('ul');
|
||||
Zira\View::addHTML($html, Zira\View::VAR_HEADER);
|
||||
|
||||
$step = (int)Zira\Request::post('step', 0);
|
||||
if ($step>0) {
|
||||
if($step <= $db_version) {
|
||||
$response = array('success' => true);
|
||||
} else if ($step <= $db_update_version) {
|
||||
try {
|
||||
Zira\Db\Db::begin();
|
||||
$path = ROOT_DIR . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'v' . $step . DIRECTORY_SEPARATOR . 'index.php';
|
||||
if (file_exists($path)) include($path);
|
||||
Zira\Models\Option::write('db_version', $step);
|
||||
Zira\Db\Db::commit();
|
||||
$response = array('success' => true);
|
||||
} catch(\Exception $e) {
|
||||
Zira\Db\Db::rollback();
|
||||
$response = array('error' => $e->getMessage());
|
||||
Zira\Log::write($e->getMessage());
|
||||
}
|
||||
} else {
|
||||
$response = array('error' => Zira\Locale::t('An error occurred.'));
|
||||
}
|
||||
echo json_encode($response);
|
||||
Zira\Session::close();
|
||||
exit;
|
||||
}
|
||||
|
||||
$init_content = Zira\Helper::tag_open('div', array('id'=>'zira-update-container'));
|
||||
|
||||
if ($db_version < $db_update_version) {
|
||||
$init_content .= Zira\Helper::tag('p', Zira\Locale::t('Database version: %s', $db_version)).
|
||||
Zira\Helper::tag('p', Zira\Locale::t('Database needs to be updated to version: %s', $db_update_version)).
|
||||
Zira\Helper::tag('p', Zira\Locale::t('Zira CMS is ready to update.')).
|
||||
Zira\Helper::tag_open('div', array('style'=>'margin:40px 0px 100px')).
|
||||
Zira\Helper::tag('button', Zira\Locale::t('Update'), array('class'=>'btn btn-primary', 'id'=>'zira-update-start-btn')).
|
||||
Zira\Helper::tag_close('div');
|
||||
|
||||
$init_js = Zira\Helper::tag_open('script',array('type'=>'text/javascript')).
|
||||
'zira_strings = {\'Error\':\''.Zira\Locale::t('Error').'\', \'Message\':\''.Zira\Locale::t('Message').'\', \'Close\':\''.Zira\Locale::t('Close').'\', \'Please wait\': \''.Zira\Locale::t('Please wait').'\', \'Updated successfully!\': \''.Zira\Locale::t('Updated successfully!').'\', \'Database is up to date.\': \''.Zira\Locale::t('Database is up to date.').'\'};'.
|
||||
'(function($){'.
|
||||
'zira_update_step = '.$db_version.';'.
|
||||
'zira_update_target = '.$db_update_version.';'.
|
||||
'zira_update_start_version = zira_update_step;'.
|
||||
'zira_update = function() {'.
|
||||
'$(\'#zira-update-start-btn\').attr(\'disabled\', \'disabled\');'.
|
||||
'zira_update_step++;'.
|
||||
'zira_modal_progress(\''.Zira\Locale::t('Update in progress').'\');'.
|
||||
'zira_update_request(zira_update_step);'.
|
||||
'};'.
|
||||
'zira_update_request = function(step) {'.
|
||||
'if (step > zira_update_target) return;'.
|
||||
'$(\'body\').css(\'cursor\',\'wait\');'.
|
||||
'var data = {\'step\': step};'.
|
||||
'zira_update_xhr=$.post(\'?lang='.Zira\Helper::html($language).'\', data, function(response){'.
|
||||
'if (!response) { zira_error(\''.Zira\Locale::t('An error occurred').'\'); return; }'.
|
||||
'if (response.error) {'.
|
||||
'zira_modal_progress_hide();'.
|
||||
'zira_error(response.error);'.
|
||||
'}'.
|
||||
'if (response.success) {'.
|
||||
'var progress_total = zira_update_target - zira_update_start_version;'.
|
||||
'var progress_current = zira_update_step - zira_update_start_version;'.
|
||||
'var progress_percent = Math.floor(progress_current * 100 / progress_total);'.
|
||||
'zira_modal_progress_update(progress_percent);'.
|
||||
'if (zira_update_step < zira_update_target) {'.
|
||||
'zira_update_step++;'.
|
||||
'window.setTimeout(\'zira_update_request(zira_update_step);\',1000);'.
|
||||
'} else {'.
|
||||
'zira_modal_progress_hide();'.
|
||||
'zira_message(t(\'Updated successfully!\'));'.
|
||||
'$(\'#zira-update-container\').text(t(\'Database is up to date.\'));'.
|
||||
'}'.
|
||||
'}'.
|
||||
'$(\'body\').css(\'cursor\',\'default\');'.
|
||||
'}, \'json\').always(function(){'.
|
||||
'if (zira_update_xhr.status != 200) zira_error(\''.Zira\Locale::t('An error occurred').'\'+\'.\');'.
|
||||
'});'.
|
||||
'};'.
|
||||
'$(document).ready(function(){'.
|
||||
'$(\'#zira-update-start-btn\').click(zira_update);'.
|
||||
'});'.
|
||||
'})(jQuery);'.
|
||||
Zira\Helper::tag_close('script');
|
||||
} else {
|
||||
$init_content .= Zira\Helper::tag('p', Zira\Locale::t('Database is up to date.'));
|
||||
|
||||
$init_js = '';
|
||||
}
|
||||
$init_content .= Zira\Helper::tag_close('div');
|
||||
|
||||
$view_file = ROOT_DIR . DIRECTORY_SEPARATOR .
|
||||
THEMES_DIR . DIRECTORY_SEPARATOR .
|
||||
DEFAULT_THEME . DIRECTORY_SEPARATOR .
|
||||
'page.php';
|
||||
|
||||
$layout_file = ROOT_DIR . DIRECTORY_SEPARATOR .
|
||||
'update' . DIRECTORY_SEPARATOR .
|
||||
'layout.php';
|
||||
|
||||
Zira\View::addBodyBottomScript($init_js);
|
||||
|
||||
Zira\View::$data = array(
|
||||
Zira\View::VAR_TITLE => Zira\Locale::t('Zira CMS update wizzard'),
|
||||
Zira\View::VAR_CONTENT => $init_content
|
||||
);
|
||||
|
||||
Zira\View::$view = $view_file;
|
||||
Zira\View::$layout = $layout_file;
|
||||
|
||||
Zira\View::renderLayout();
|
||||
Zira\Session::close();
|
55
update/layout.php
Normal file
55
update/layout.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="shortcut icon" href="<?php echo Zira\Helper::baseUrl('favicon.ico') ?>" type="image/x-icon"/>
|
||||
<?php layout_head() ?>
|
||||
<style>
|
||||
header {
|
||||
min-height: 160px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<?php layout_body_top() ?>
|
||||
<div id="main-container-wrapper"><div id="main-container">
|
||||
<header>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<?php layout_header() ?>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-sm-2 sidebar">
|
||||
<aside>
|
||||
<?php layout_sidebar_left() ?>
|
||||
</aside>
|
||||
</div>
|
||||
<div id="content" class="col-sm-8">
|
||||
<?php breadcrumbs(); ?>
|
||||
<?php layout_content_top() ?>
|
||||
<?php layout_content() ?>
|
||||
<?php layout_content_bottom() ?>
|
||||
</div>
|
||||
<div class="col-sm-2 sidebar">
|
||||
<aside>
|
||||
<?php layout_sidebar_right() ?>
|
||||
</aside>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!--
|
||||
<footer>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<?php //layout_footer() ?>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
-->
|
||||
</div></div><!--/main-container-wrapper-->
|
||||
<?php layout_body_bottom() ?>
|
||||
</body>
|
||||
</html>
|
98
update/table.php
Normal file
98
update/table.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* table.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
namespace Update;
|
||||
|
||||
abstract class Table {
|
||||
protected $_table;
|
||||
|
||||
public function __construct($table_name) {
|
||||
$this->_table = DB_PREFIX . $table_name;
|
||||
}
|
||||
|
||||
public function getFields() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getKeys() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getUnique() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getValues() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
$primary = null;
|
||||
$fields = array();
|
||||
foreach((array)$this->getFields() as $name=>$type) {
|
||||
if ($type == Field::primary()) $primary = $name;
|
||||
$fields[]= Db::escapeIdentifier($name).' '.$type;
|
||||
}
|
||||
if ($primary!==null) {
|
||||
$fields[]='PRIMARY KEY ('.Db::escapeIdentifier($primary).')';
|
||||
}
|
||||
foreach((array)$this->getKeys() as $name=>$keys) {
|
||||
$index='KEY '.Db::escapeIdentifier($name).' ( ';
|
||||
if (is_string($keys)) $index .= Db::escapeIdentifier($keys);
|
||||
else if (is_array($keys)) {
|
||||
for($i=0; $i<count($keys); $i++) {
|
||||
$keys[$i]= Db::escapeIdentifier($keys[$i]);
|
||||
}
|
||||
$index.=implode(', ',$keys);
|
||||
}
|
||||
$index .= ' )';
|
||||
$fields[]=$index;
|
||||
}
|
||||
foreach((array)$this->getUnique() as $name=>$keys) {
|
||||
$index='UNIQUE KEY '.Db::escapeIdentifier($name).' ( ';
|
||||
if (is_string($keys)) $index .= Db::escapeIdentifier($keys);
|
||||
else if (is_array($keys)) {
|
||||
for($i=0; $i<count($keys); $i++) {
|
||||
$keys[$i]= Db::escapeIdentifier($keys[$i]);
|
||||
}
|
||||
$index.=implode(', ',$keys);
|
||||
}
|
||||
$index .= ' )';
|
||||
$fields[]=$index;
|
||||
}
|
||||
|
||||
$sql = 'CREATE TABLE '.DB::escapeIdentifier($this->_table).' ( ';
|
||||
$sql .= implode(', ', $fields);
|
||||
$sql .= ' ) ENGINE='.$this->getEngine().' DEFAULT CHARSET='.$this->getCharset();
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function install() {
|
||||
// creating table
|
||||
$query = (string)$this;
|
||||
Db::query($query);
|
||||
// inserting default values
|
||||
foreach((array)$this->getDefaults() as $insert) {
|
||||
$fields = array();
|
||||
$values = array();
|
||||
$data = array();
|
||||
foreach($insert as $field=>$value) {
|
||||
$fields[]=Db::escapeIdentifier($field);
|
||||
$values[]='?';
|
||||
$data[]=$value;
|
||||
}
|
||||
$query = 'INSERT INTO '.Db::escapeIdentifier($this->_table).' ('.implode(', ',$fields).') VALUES ('.implode(', ',$values).')';
|
||||
Db::query($query, $data);
|
||||
}
|
||||
}
|
||||
|
||||
public function uninstall() {
|
||||
$query = 'DROP TABLE IF EXISTS '.DB::escapeIdentifier($this->_table);
|
||||
Db::query($query);
|
||||
}
|
||||
}
|
5
update/v1/index.php
Normal file
5
update/v1/index.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
if (!defined('ZIRA_UPDATE') || !ZIRA_UPDATE) exit;
|
||||
|
||||
$alterPermission = new \Update\V1\Permission();
|
||||
$alterPermission->execute();
|
48
update/v1/permission.php
Normal file
48
update/v1/permission.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Update\V1;
|
||||
|
||||
use Zira\Db\Alter;
|
||||
use Zira\Db\Field;
|
||||
|
||||
class Permission extends Alter {
|
||||
protected $_table = 'permissions';
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct($this->_table);
|
||||
}
|
||||
|
||||
public function getValues() {
|
||||
$superAdminPermissions = \Zira\Install\Permission::getDefaultSuperAdminPermissions();
|
||||
$adminPermissions = \Zira\Install\Permission::getDefaultAdminPermissions();
|
||||
$userPermissions = \Zira\Install\Permission::getDefaultUserPermissions();
|
||||
if (!isset($superAdminPermissions[\Zira\Permission::TO_DOWNLOAD_FILES]) ||
|
||||
!isset($adminPermissions[\Zira\Permission::TO_DOWNLOAD_FILES]) ||
|
||||
!isset($userPermissions[\Zira\Permission::TO_DOWNLOAD_FILES])
|
||||
) return array();
|
||||
|
||||
return array(
|
||||
array(
|
||||
'id' => null,
|
||||
'group_id' => \Zira\User::GROUP_SUPERADMIN,
|
||||
'module' => 'zira',
|
||||
'name' => \Zira\Permission::TO_DOWNLOAD_FILES,
|
||||
'allow' => $superAdminPermissions[\Zira\Permission::TO_DOWNLOAD_FILES]
|
||||
),
|
||||
array(
|
||||
'id' => null,
|
||||
'group_id' => \Zira\User::GROUP_ADMIN,
|
||||
'module' => 'zira',
|
||||
'name' => \Zira\Permission::TO_DOWNLOAD_FILES,
|
||||
'allow' => $adminPermissions[\Zira\Permission::TO_DOWNLOAD_FILES]
|
||||
),
|
||||
array(
|
||||
'id' => null,
|
||||
'group_id' => \Zira\User::GROUP_USER,
|
||||
'module' => 'zira',
|
||||
'name' => \Zira\Permission::TO_DOWNLOAD_FILES,
|
||||
'allow' => $userPermissions[\Zira\Permission::TO_DOWNLOAD_FILES]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
57
zira/db/implement/alter.php
Normal file
57
zira/db/implement/alter.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* alter.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
namespace Zira\Db\Implement;
|
||||
|
||||
interface Alter {
|
||||
/**
|
||||
* Returns table name
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Returns table fields to be added
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldsToAdd();
|
||||
|
||||
/**
|
||||
* Returns table indexes to be added
|
||||
* @return array
|
||||
*/
|
||||
public function getKeysToAdd();
|
||||
|
||||
/**
|
||||
* Returns table indexes to be dropped
|
||||
* @return array
|
||||
*/
|
||||
public function getKeysToDrop();
|
||||
|
||||
/**
|
||||
* Returns table unique indexes to be added
|
||||
* @return array
|
||||
*/
|
||||
public function getUniqueToAdd();
|
||||
|
||||
/**
|
||||
* Returns table rows to be added
|
||||
* @return array
|
||||
*/
|
||||
public function getValues();
|
||||
|
||||
/**
|
||||
* Returns alter SQL
|
||||
* @return string
|
||||
*/
|
||||
public function __toString();
|
||||
|
||||
/**
|
||||
* Apply modifications
|
||||
*/
|
||||
public function execute();
|
||||
}
|
|
@ -13,7 +13,8 @@ class Loader {
|
|||
'field',
|
||||
'table',
|
||||
'orm',
|
||||
'collection'
|
||||
'collection',
|
||||
'alter'
|
||||
);
|
||||
|
||||
public static function initialize() {
|
||||
|
|
12
zira/db/mysql.alter.php
Normal file
12
zira/db/mysql.alter.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* alter.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
namespace Zira\Db;
|
||||
|
||||
class Alter extends Mysql\Alter implements Implement\Alter {
|
||||
|
||||
}
|
102
zira/db/mysql/alter.php
Normal file
102
zira/db/mysql/alter.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* alter.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
namespace Zira\Db\Mysql;
|
||||
|
||||
abstract class Alter implements \Zira\Db\Implement\Alter {
|
||||
protected $_table;
|
||||
|
||||
public function __construct($table_name) {
|
||||
$this->_table = DB_PREFIX . $table_name;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->_table;
|
||||
}
|
||||
|
||||
public function getFieldsToAdd() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getKeysToAdd() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getKeysToDrop() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getUniqueToAdd() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getValues() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
$fields = array();
|
||||
|
||||
foreach((array)$this->getKeysToDrop() as $name) {
|
||||
$fields[]='DROP KEY '.Db::escapeIdentifier($name);
|
||||
}
|
||||
|
||||
foreach((array)$this->getFieldsToAdd() as $name=>$type) {
|
||||
$fields[]= 'ADD '.Db::escapeIdentifier($name).' '.$type;
|
||||
}
|
||||
|
||||
foreach((array)$this->getKeysToAdd() as $name=>$keys) {
|
||||
$index='KEY '.Db::escapeIdentifier($name).' ( ';
|
||||
if (is_string($keys)) $index .= Db::escapeIdentifier($keys);
|
||||
else if (is_array($keys)) {
|
||||
for($i=0; $i<count($keys); $i++) {
|
||||
$keys[$i]= Db::escapeIdentifier($keys[$i]);
|
||||
}
|
||||
$index.=implode(', ',$keys);
|
||||
}
|
||||
$index .= ' )';
|
||||
$fields[]='ADD '.$index;
|
||||
}
|
||||
|
||||
foreach((array)$this->getUniqueToAdd() as $name=>$keys) {
|
||||
$index='UNIQUE KEY '.Db::escapeIdentifier($name).' ( ';
|
||||
if (is_string($keys)) $index .= Db::escapeIdentifier($keys);
|
||||
else if (is_array($keys)) {
|
||||
for($i=0; $i<count($keys); $i++) {
|
||||
$keys[$i]= Db::escapeIdentifier($keys[$i]);
|
||||
}
|
||||
$index.=implode(', ',$keys);
|
||||
}
|
||||
$index .= ' )';
|
||||
$fields[]='ADD '.$index;
|
||||
}
|
||||
|
||||
$sql = 'ALTER TABLE '.DB::escapeIdentifier($this->_table);
|
||||
$sql .= implode(', ', $fields);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
// creating query
|
||||
$query = (string)$this;
|
||||
Db::query($query);
|
||||
// inserting new values
|
||||
foreach((array)$this->getValues() as $insert) {
|
||||
$fields = array();
|
||||
$values = array();
|
||||
$data = array();
|
||||
foreach($insert as $field=>$value) {
|
||||
$fields[]=Db::escapeIdentifier($field);
|
||||
$values[]='?';
|
||||
$data[]=$value;
|
||||
}
|
||||
$query = 'INSERT INTO '.Db::escapeIdentifier($this->_table).' ('.implode(', ',$fields).') VALUES ('.implode(', ',$values).')';
|
||||
Db::query($query, $data);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace Zira\Db\Mysql;
|
||||
|
||||
class Collection {
|
||||
class Collection implements \Zira\Db\Implement\Collection {
|
||||
protected $_class;
|
||||
protected $_table;
|
||||
protected $_pk;
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace Zira\Db\Mysql;
|
|||
|
||||
use PDO;
|
||||
|
||||
class Db {
|
||||
class Db implements \Zira\Db\Implement\Db {
|
||||
protected static $_db;
|
||||
protected static $_total = 0;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace Zira\Db\Mysql;
|
||||
|
||||
abstract class Field {
|
||||
abstract class Field implements \Zira\Db\Implement\Field {
|
||||
const FIELD_TYPE_TINYINT = 'TINYINT(4)';
|
||||
const FIELD_TYPE_TINYINT_NOT_NULL = 'TINYINT(4) NOT NULL';
|
||||
const FIELD_TYPE_TINYINT_UNSIGNED = 'TINYINT(3) UNSIGNED';
|
||||
|
|
12
zira/db/sqlite.alter.php
Normal file
12
zira/db/sqlite.alter.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* alter.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
namespace Zira\Db;
|
||||
|
||||
class Alter extends Sqlite\Alter implements Implement\Alter {
|
||||
|
||||
}
|
132
zira/db/sqlite/alter.php
Normal file
132
zira/db/sqlite/alter.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
/**
|
||||
* Zira project.
|
||||
* alter.php
|
||||
* (c)2017 http://dro1d.ru
|
||||
*/
|
||||
|
||||
namespace Zira\Db\Sqlite;
|
||||
|
||||
abstract class Alter implements \Zira\Db\Implement\Alter {
|
||||
protected $_table;
|
||||
|
||||
public function __construct($table_name) {
|
||||
$this->_table = DB_PREFIX . $table_name;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->_table;
|
||||
}
|
||||
|
||||
public function getFieldsToAdd() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getKeysToAdd() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getKeysToDrop() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getUniqueToAdd() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getValues() {
|
||||
return array();
|
||||
}
|
||||
|
||||
protected function _getAddFieldSQL($name, $type) {
|
||||
$sql = 'ALTER TABLE '.DB::escapeIdentifier($this->_table).' ADD '.Db::escapeIdentifier($name).' '.$type;
|
||||
|
||||
if ($type == Field::FIELD_TYPE_INT_NOT_NULL) $sql .= ' DEFAULT 0';
|
||||
else if ($type == Field::FIELD_TYPE_TEXT_NOT_NULL) $sql .= ' DEFAULT \'\'';
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
protected function _getCreateIndexSQL($name, $keys, $unique = false) {
|
||||
$index=' ( ';
|
||||
if (is_string($keys)) $index .= Db::escapeIdentifier($keys);
|
||||
else if (is_array($keys)) {
|
||||
for($i=0; $i<count($keys); $i++) {
|
||||
$keys[$i]= Db::escapeIdentifier($keys[$i]);
|
||||
}
|
||||
$index.=implode(', ',$keys);
|
||||
}
|
||||
$index .= ' )';
|
||||
$sql = 'CREATE ';
|
||||
if ($unique) $sql .= 'UNIQUE ';
|
||||
$sql .='INDEX '.Db::escapeIdentifier($this->_table.'_'.$name).' ON '.DB::escapeIdentifier($this->_table).$index;
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
protected function _getDropIndexSQL($name) {
|
||||
$sql ='DROP INDEX '.Db::escapeIdentifier($this->_table.'_'.$name);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function createIndexes() {
|
||||
foreach((array)$this->getKeysToAdd() as $name=>$keys) {
|
||||
$sql = $this->_getCreateIndexSQL($name, $keys);
|
||||
Db::query($sql);
|
||||
}
|
||||
foreach((array)$this->getUniqueToAdd() as $name=>$keys) {
|
||||
$sql = $this->_getCreateIndexSQL($name, $keys, true);
|
||||
Db::query($sql);
|
||||
}
|
||||
}
|
||||
|
||||
public function dropIndexes() {
|
||||
foreach((array)$this->getKeysToDrop() as $name) {
|
||||
$sql = $this->_getDropIndexSQL($name);
|
||||
Db::query($sql);
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
$sql = '';
|
||||
foreach((array)$this->getKeysToDrop() as $name) {
|
||||
$sql .= $this->_getDropIndexSQL($name).';'."\r\n";
|
||||
}
|
||||
foreach((array)$this->getFieldsToAdd() as $name=>$type) {
|
||||
$sql .= $this->_getAddFieldSQL($name, $type).';'."\r\n";
|
||||
}
|
||||
foreach((array)$this->getKeysToAdd() as $name=>$keys) {
|
||||
$sql .= $this->_getCreateIndexSQL($name, $keys).';'."\r\n";
|
||||
}
|
||||
foreach((array)$this->getUniqueToAdd() as $name=>$keys) {
|
||||
$sql .= $this->_getCreateIndexSQL($name, $keys, true).';'."\r\n";
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
// dropping indexes
|
||||
$this->dropIndexes();
|
||||
// adding fields
|
||||
foreach((array)$this->getFieldsToAdd() as $name=>$type) {
|
||||
$query = $this->_getAddFieldSQL($name, $type);
|
||||
Db::query($query);
|
||||
}
|
||||
// creating indexes
|
||||
$this->createIndexes();
|
||||
// inserting default values
|
||||
foreach((array)$this->getValues() as $insert) {
|
||||
$fields = array();
|
||||
$values = array();
|
||||
$data = array();
|
||||
foreach($insert as $field=>$value) {
|
||||
$fields[]=Db::escapeIdentifier($field);
|
||||
$values[]='?';
|
||||
$data[]=$value;
|
||||
}
|
||||
$query = 'INSERT INTO '.Db::escapeIdentifier($this->_table).' ('.implode(', ',$fields).') VALUES ('.implode(', ',$values).')';
|
||||
Db::query($query, $data);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace Zira\Db\Sqlite;
|
||||
|
||||
class Collection {
|
||||
class Collection implements \Zira\Db\Implement\Collection {
|
||||
protected $_class;
|
||||
protected $_table;
|
||||
protected $_pk;
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace Zira\Db\Sqlite;
|
|||
|
||||
use PDO;
|
||||
|
||||
class Db {
|
||||
class Db implements \Zira\Db\Implement\Db {
|
||||
protected static $_db;
|
||||
protected static $_total = 0;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace Zira\Db\Sqlite;
|
||||
|
||||
abstract class Field {
|
||||
abstract class Field implements \Zira\Db\Implement\Field {
|
||||
const FIELD_TYPE_TINYINT = 'INTEGER';
|
||||
const FIELD_TYPE_TINYINT_NOT_NULL = 'INTEGER NOT NULL';
|
||||
const FIELD_TYPE_INT = 'INTEGER';
|
||||
|
|
|
@ -83,10 +83,10 @@ abstract class Table implements \Zira\Db\Implement\Table {
|
|||
public function __toString() {
|
||||
$sql = $this->_getCreateTableSQL().';';
|
||||
foreach((array)$this->getKeys() as $name=>$keys) {
|
||||
$sql .= "\r\n". $this->_getCreateIndexSQL($name, $keys);
|
||||
$sql .= "\r\n". $this->_getCreateIndexSQL($name, $keys).';';
|
||||
}
|
||||
foreach((array)$this->getUnique() as $name=>$keys) {
|
||||
$sql .= "\r\n". $this->_getCreateIndexSQL($name, $keys, true);
|
||||
$sql .= "\r\n". $this->_getCreateIndexSQL($name, $keys, true).';';
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ class Permission extends Table {
|
|||
);
|
||||
}
|
||||
|
||||
protected function getDefaultSuperAdminPermissions() {
|
||||
public static function getDefaultSuperAdminPermissions() {
|
||||
return array(
|
||||
\Zira\Permission::TO_ACCESS_DASHBOARD => 1,
|
||||
\Zira\Permission::TO_EXECUTE_TASKS => 1,
|
||||
|
@ -50,6 +50,7 @@ class Permission extends Table {
|
|||
\Zira\Permission::TO_DELETE_USERS => 1,
|
||||
\Zira\Permission::TO_UPLOAD_FILES => 1,
|
||||
\Zira\Permission::TO_DELETE_FILES => 1,
|
||||
\Zira\Permission::TO_DOWNLOAD_FILES => 1,
|
||||
\Zira\Permission::TO_VIEW_FILES => 1,
|
||||
\Zira\Permission::TO_UPLOAD_IMAGES => 1,
|
||||
\Zira\Permission::TO_DELETE_IMAGES => 1,
|
||||
|
@ -63,7 +64,7 @@ class Permission extends Table {
|
|||
);
|
||||
}
|
||||
|
||||
protected function getDefaultAdminPermissions() {
|
||||
public static function getDefaultAdminPermissions() {
|
||||
return array(
|
||||
\Zira\Permission::TO_ACCESS_DASHBOARD => 1,
|
||||
\Zira\Permission::TO_EXECUTE_TASKS => 0,
|
||||
|
@ -74,6 +75,7 @@ class Permission extends Table {
|
|||
\Zira\Permission::TO_DELETE_USERS => 0,
|
||||
\Zira\Permission::TO_UPLOAD_FILES => 0,
|
||||
\Zira\Permission::TO_DELETE_FILES => 0,
|
||||
\Zira\Permission::TO_DOWNLOAD_FILES => 1,
|
||||
\Zira\Permission::TO_VIEW_FILES => 1,
|
||||
\Zira\Permission::TO_UPLOAD_IMAGES => 1,
|
||||
\Zira\Permission::TO_DELETE_IMAGES => 1,
|
||||
|
@ -87,7 +89,7 @@ class Permission extends Table {
|
|||
);
|
||||
}
|
||||
|
||||
protected function getDefaultUserPermissions() {
|
||||
public static function getDefaultUserPermissions() {
|
||||
return array(
|
||||
\Zira\Permission::TO_ACCESS_DASHBOARD => 0,
|
||||
\Zira\Permission::TO_EXECUTE_TASKS => 0,
|
||||
|
@ -98,6 +100,7 @@ class Permission extends Table {
|
|||
\Zira\Permission::TO_DELETE_USERS => 0,
|
||||
\Zira\Permission::TO_UPLOAD_FILES => 0,
|
||||
\Zira\Permission::TO_DELETE_FILES => 0,
|
||||
\Zira\Permission::TO_DOWNLOAD_FILES => 1,
|
||||
\Zira\Permission::TO_VIEW_FILES => 0,
|
||||
\Zira\Permission::TO_UPLOAD_IMAGES => 0,
|
||||
\Zira\Permission::TO_DELETE_IMAGES => 0,
|
||||
|
|
|
@ -17,6 +17,7 @@ class Permission {
|
|||
const TO_DELETE_USERS = 'Delete users';
|
||||
const TO_UPLOAD_FILES = 'Upload files';
|
||||
const TO_DELETE_FILES = 'Delete files';
|
||||
const TO_DOWNLOAD_FILES = 'Download files';
|
||||
const TO_VIEW_FILES = 'View files list';
|
||||
const TO_UPLOAD_IMAGES = 'Upload images';
|
||||
const TO_DELETE_IMAGES = 'Delete images';
|
||||
|
@ -41,6 +42,7 @@ class Permission {
|
|||
self::TO_DELETE_USERS,
|
||||
self::TO_UPLOAD_FILES,
|
||||
self::TO_DELETE_FILES,
|
||||
self::TO_DOWNLOAD_FILES,
|
||||
self::TO_VIEW_FILES,
|
||||
self::TO_UPLOAD_IMAGES,
|
||||
self::TO_DELETE_IMAGES,
|
||||
|
|
Loading…
Reference in a new issue