Fix 4.7.0 update script (#813)
This commit is contained in:
parent
c7af383ef2
commit
b0b3ed7238
|
@ -12,7 +12,7 @@ print '[1/4] Updating user table...' . PHP_EOL;
|
|||
|
||||
if ($mysql->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'user' AND COLUMN_NAME = 'not_registered' AND TABLE_SCHEMA = '$mysql_db'")->num_rows == 0) {
|
||||
$mysql->query("ALTER TABLE user ADD not_registered tinyint(1)");
|
||||
$mysql->query("UPDATE setting SET not_registered = false ");
|
||||
$mysql->query("UPDATE user SET not_registered = null ");
|
||||
} else {
|
||||
print '-not_registered column already exists' . PHP_EOL;
|
||||
}
|
||||
|
@ -84,6 +84,7 @@ if ($mysql->query("SELECT * FROM setting WHERE name='mandatory-login' ")->num_ro
|
|||
$mysql->query("INSERT into setting VALUES(NULL, 'mandatory-login', '1')");
|
||||
}else{
|
||||
$mysql->query("INSERT into setting VALUES(NULL, 'mandatory-login', '0')");
|
||||
$mysql->query("UPDATE setting SET value=1 where name='registration'");
|
||||
}
|
||||
} else {
|
||||
print '-Mandatory-login already exists' . PHP_EOL;
|
||||
|
@ -91,7 +92,7 @@ if ($mysql->query("SELECT * FROM setting WHERE name='mandatory-login' ")->num_ro
|
|||
if ($mysql->query("SELECT * FROM setting WHERE name='default-department-id' ")->num_rows == 0) {
|
||||
$publicDepartment = $mysql->query("SELECT * FROM department WHERE private= 0 OR private IS NULL");
|
||||
if($publicDepartment->num_rows != 0){
|
||||
$query = "INSERT into setting VALUES(NULL, 'default-department-id', ". $publicDepartment->fetch_array(MYSQLI_BOTH)[0]['value'] . " )";
|
||||
$query = "INSERT into setting VALUES(NULL, 'default-department-id', ". $publicDepartment->fetch_array(MYSQLI_BOTH)['id'] . " )";
|
||||
|
||||
$mysql->query($query);
|
||||
}else{
|
||||
|
@ -110,7 +111,7 @@ if ($mysql->query("SELECT * FROM setting WHERE name='default-is-locked' ")->num_
|
|||
if ($mysql->query("SELECT * FROM setting WHERE name='user-system-enabled' ")->num_rows != 0) {
|
||||
$mysql->query("DELETE FROM setting WHERE name='user-system-enabled' ");
|
||||
} else {
|
||||
print '-User-system-enabled is realready deleted' . PHP_EOL;
|
||||
print '-User-system-enabled is already deleted' . PHP_EOL;
|
||||
}
|
||||
|
||||
print 'Update Completed!' . PHP_EOL;
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
use RedBeanPHP\Facade as RedBean;
|
||||
|
||||
abstract class DataStore {
|
||||
protected $_bean;
|
||||
protected $properties = [];
|
||||
|
||||
public static function isTableEmpty() {
|
||||
try {
|
||||
return (RedBean::count(static::TABLE) === 0);
|
||||
} catch(\Exception $e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getFetchAs() {
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function getDataStore($value, $property = 'id') {
|
||||
$bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array(
|
||||
':value' => $value
|
||||
));
|
||||
|
||||
return ($bean) ? new static($bean) : new NullDataStore();
|
||||
}
|
||||
|
||||
public static function count($addSQL = '', $bindings = array()) {
|
||||
return RedBean::count(static::TABLE, $addSQL, $bindings);
|
||||
}
|
||||
|
||||
public static function getAll() {
|
||||
$beanList = RedBean::findAll(static::TABLE);
|
||||
$dataStoreList = new DataStoreList();
|
||||
|
||||
foreach($beanList as $bean) {
|
||||
$dataStoreList->add(new static($bean));
|
||||
}
|
||||
|
||||
return $dataStoreList;
|
||||
}
|
||||
public static function find($query = '', $matches = []) {
|
||||
$beanList = RedBean::find(static::TABLE, $query, $matches);
|
||||
|
||||
return DataStoreList::getList(ucfirst(static::TABLE), $beanList);
|
||||
}
|
||||
|
||||
public static function findOne($query = '', $matches = []) {
|
||||
$bean = RedBean::findOne(static::TABLE, $query, $matches);
|
||||
|
||||
return ($bean) ? new static($bean) : new NullDataStore();
|
||||
}
|
||||
|
||||
private static function validateProp($propToValidate) {
|
||||
$validProp = false;
|
||||
|
||||
foreach (static::getProps() as $prop) {
|
||||
if ($propToValidate === $prop) {
|
||||
$validProp = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ($validProp) ? self::from_camel_case($propToValidate) : 'id';
|
||||
}
|
||||
|
||||
private static function from_camel_case($input) {
|
||||
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
|
||||
$ret = $matches[0];
|
||||
foreach ($ret as &$match) {
|
||||
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
|
||||
}
|
||||
return implode('_', $ret);
|
||||
}
|
||||
|
||||
public function __construct($beanInstance = null) {
|
||||
if ($beanInstance) {
|
||||
$this->setBean($beanInstance);
|
||||
} else {
|
||||
$this->setBean(RedBean::dispense(static::TABLE));
|
||||
$this->setProperties($this->getDefaultProps());
|
||||
}
|
||||
}
|
||||
|
||||
public function getDefaultProps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function setProperties($properties) {
|
||||
foreach (static::getProps() as $PROP) {
|
||||
if(array_key_exists($PROP, $properties)) {
|
||||
$this->properties[$PROP] = $properties[$PROP];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($prop, $value) {
|
||||
if (in_array($prop, static::getProps())) {
|
||||
$this->properties[$prop] = $value;
|
||||
} else if(property_exists($this, $prop)){
|
||||
$this->{$prop} = $value;
|
||||
} else {
|
||||
throw new Exception("Invalid prop: $prop");
|
||||
}
|
||||
}
|
||||
|
||||
public function &__get($name) {
|
||||
if (!array_key_exists($name, $this->properties) || !$this->properties[$name]) {
|
||||
$this->properties[$name] = $this->parseBeanProp($name);
|
||||
}
|
||||
|
||||
if ($name !== 'id') {
|
||||
$property =& $this->properties[$name];
|
||||
} else {
|
||||
$property = $this->_bean->id;
|
||||
}
|
||||
|
||||
return $property;
|
||||
}
|
||||
|
||||
private function setBean($beanInstance) {
|
||||
$this->_bean = $beanInstance;
|
||||
}
|
||||
|
||||
private function parseBeanProp($prop) {
|
||||
$fetchAs = static::getFetchAs();
|
||||
|
||||
if(array_key_exists($prop, $fetchAs)) {
|
||||
$parsedProp = $this->_bean->fetchAs($fetchAs[$prop])[$prop];
|
||||
} else {
|
||||
$parsedProp = $this->_bean[$prop];
|
||||
}
|
||||
|
||||
if (strpos($prop, 'List')) {
|
||||
$parsedProp = DataStoreList::getList($this->getListType($prop), $parsedProp);
|
||||
} else if ($parsedProp instanceof \RedBeanPHP\OODBBean) {
|
||||
$beanType = ucfirst($parsedProp->getPropertiesAndType()[1]);
|
||||
|
||||
$parsedProp = new $beanType($parsedProp);
|
||||
|
||||
}
|
||||
|
||||
return $parsedProp;
|
||||
}
|
||||
|
||||
public function store() {
|
||||
$this->updateBeanProperties();
|
||||
|
||||
return RedBean::store($this->getBeanInstance());
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
RedBean::trash($this->getBeanInstance());
|
||||
}
|
||||
|
||||
public function getBeanInstance() {
|
||||
return $this->_bean;
|
||||
}
|
||||
|
||||
public function isNull() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function updateBeanProperties() {
|
||||
foreach ($this->properties as $key => $prop) {
|
||||
$this->updateBeanProp($key, $prop);
|
||||
}
|
||||
}
|
||||
|
||||
public function withCondition($condition, $values) {
|
||||
return new static($this->_bean->withCondition($condition, $values));
|
||||
}
|
||||
|
||||
public function countShared($shared) {
|
||||
return $this->_bean->countShared($shared);
|
||||
}
|
||||
|
||||
private function updateBeanProp($key, $value) {
|
||||
if ($value instanceof DataStoreList) {
|
||||
$this->_bean[$key] = $value->toBeanList();
|
||||
} else if ($value instanceof DataStore) {
|
||||
$this->_bean[$key] = $value->getBeanInstance();
|
||||
} else {
|
||||
$this->_bean[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
private function getListType($listName) {
|
||||
$listType = $listName;
|
||||
|
||||
$listType = str_replace('List', '', $listType);
|
||||
$listType = str_replace('shared', '', $listType);
|
||||
$listType = str_replace('xown', '', $listType);
|
||||
$listType = str_replace('own', '', $listType);
|
||||
|
||||
return $listType;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue