mirror of
https://github.com/Yetangitu/ampache
synced 2025-10-06 03:49:56 +02:00
[FEATURE] #280 Add core libraries
This commit is contained in:
parent
b312a3f486
commit
08965bf7fc
8 changed files with 690 additions and 0 deletions
89
lib/class/DatabaseObject.php
Normal file
89
lib/class/DatabaseObject.php
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of Model
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
abstract class DatabaseObject
|
||||||
|
{
|
||||||
|
protected $id;
|
||||||
|
//private $originalData;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->remapCamelcase();
|
||||||
|
//$this->originalData = get_object_vars($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function isPropertyDirty($property)
|
||||||
|
{
|
||||||
|
return $this->originalData->$property !== $this->$property;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isDirty()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDirtyProperties()
|
||||||
|
{
|
||||||
|
$properties = get_object_vars($this);
|
||||||
|
unset($properties['id']);
|
||||||
|
return $this->fromCamelCase($properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the object properties to camelCase.
|
||||||
|
* This works in constructor because the properties are here from
|
||||||
|
* fetch_object before the constructor get called.
|
||||||
|
*/
|
||||||
|
public function remapCamelcase()
|
||||||
|
{
|
||||||
|
foreach(get_object_vars($this) as $key => $val) {
|
||||||
|
if(strpos($key, '_') !== false) {
|
||||||
|
$camelCaseKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
|
||||||
|
$this->$camelCaseKey = $val;
|
||||||
|
unset($this->$key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromCamelCase($properties) {
|
||||||
|
$data = array();
|
||||||
|
foreach($properties as $propertie => $value) {
|
||||||
|
$newPropertyKey = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $propertie));
|
||||||
|
$data[$newPropertyKey] = $value;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
lib/class/Interface/Model.php
Normal file
34
lib/class/Interface/Model.php
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of Model
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
interface Model
|
||||||
|
{
|
||||||
|
//put your code here
|
||||||
|
}
|
87
lib/class/Metadata/Metadata.php
Normal file
87
lib/class/Metadata/Metadata.php
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib\Metadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of metadata
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
class Metadata
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Some kind of item
|
||||||
|
* @var library_item
|
||||||
|
*/
|
||||||
|
protected $object;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag values
|
||||||
|
* @var stdClass
|
||||||
|
*/
|
||||||
|
protected $data;
|
||||||
|
|
||||||
|
public function __construct($object)
|
||||||
|
{
|
||||||
|
$this->object = $object;
|
||||||
|
$this->data = new stdClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function load($tag = null)
|
||||||
|
{
|
||||||
|
$sql = 'SELECT metadata.data, metadata_field.name FROM metadata '
|
||||||
|
. 'JOIN metadata_field ON metadata.field = metadata_field.id';
|
||||||
|
if($tag) {
|
||||||
|
$sql .= ' WHERE metadata_field.name = ?';
|
||||||
|
}
|
||||||
|
$statement = \Dba::read($sql, $tag);
|
||||||
|
while($object = \Dba::fetch_object($statement, __CLASS__)) {
|
||||||
|
$this->data[$object->getId()] = $object;
|
||||||
|
}
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($tag)
|
||||||
|
{
|
||||||
|
return $this->data->$tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAll() {
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($tag, $data)
|
||||||
|
{
|
||||||
|
$this->data->$tag = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAll($tags) {
|
||||||
|
$this->data = (object) $tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
135
lib/class/Metadata/Model/Metadata.php
Normal file
135
lib/class/Metadata/Model/Metadata.php
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib\Metadata\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of metadata
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
class Metadata extends \lib\DatabaseObject
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Database ID
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A library item like song or video
|
||||||
|
* @var \library_item
|
||||||
|
*/
|
||||||
|
protected $objectId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag Field
|
||||||
|
* @var Metadata_field
|
||||||
|
*/
|
||||||
|
protected $field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag Data
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return \library_item
|
||||||
|
*/
|
||||||
|
public function getObjectId()
|
||||||
|
{
|
||||||
|
return $this->objectId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return Metadata_field
|
||||||
|
*/
|
||||||
|
public function getField()
|
||||||
|
{
|
||||||
|
return $this->field;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getData()
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param \library_item $object
|
||||||
|
*/
|
||||||
|
public function setObjectId(\library_item $object)
|
||||||
|
{
|
||||||
|
$this->objectId = $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param Metadata_field $field
|
||||||
|
*/
|
||||||
|
public function setField(Metadata_field $field)
|
||||||
|
{
|
||||||
|
$this->field = $field;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $data
|
||||||
|
*/
|
||||||
|
public function setData($data)
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getType()
|
||||||
|
{
|
||||||
|
return $this->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $type
|
||||||
|
*/
|
||||||
|
public function setType($type)
|
||||||
|
{
|
||||||
|
$this->type = $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
93
lib/class/Metadata/Model/MetadataField.php
Normal file
93
lib/class/Metadata/Model/MetadataField.php
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib\Metadata\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of metadata_field
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
class MetadataField extends \lib\DatabaseObject
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Database ID
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag name
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the Tag public?
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
protected $public = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*/
|
||||||
|
public function setName($name)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isPublic() {
|
||||||
|
return $this->public;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set public to false
|
||||||
|
*/
|
||||||
|
public function hide() {
|
||||||
|
$this->public = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
lib/class/Metadata/Repository/Metadata.php
Normal file
36
lib/class/Metadata/Repository/Metadata.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib\Metadata\Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of Metadata
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
class Metadata extends \lib\Repository
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $modelClassName = '\lib\Metadata\Model\Metadata';
|
||||||
|
//put your code here
|
||||||
|
}
|
35
lib/class/Metadata/Repository/MetadataField.php
Normal file
35
lib/class/Metadata/Repository/MetadataField.php
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib\Metadata\Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of Metadata_field
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
class MetadataField extends \lib\Repository
|
||||||
|
{
|
||||||
|
protected $modelClassName = '\lib\Metadata\Model\MetadataField';
|
||||||
|
|
||||||
|
}
|
181
lib/class/Repository.php
Normal file
181
lib/class/Repository.php
Normal file
|
@ -0,0 +1,181 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LICENSE: GNU General Public License, version 2 (GPLv2)
|
||||||
|
* Copyright 2001 - 2014 Ampache.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License v2
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace lib;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of Repository
|
||||||
|
*
|
||||||
|
* @author raziel
|
||||||
|
*/
|
||||||
|
class Repository
|
||||||
|
{
|
||||||
|
protected $modelClassName;
|
||||||
|
|
||||||
|
protected function findBy($field, $value)
|
||||||
|
{
|
||||||
|
$table = $this->getTableName();
|
||||||
|
return $this->getRecords($table, $field, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findAll()
|
||||||
|
{
|
||||||
|
$table = $this->getTableName();
|
||||||
|
return $this->getRecords($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findById($id)
|
||||||
|
{
|
||||||
|
$rows = $this->findBy('id', $id);
|
||||||
|
return count($rows) ? reset($rows) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRecords($className, $field = null, $value = null)
|
||||||
|
{
|
||||||
|
$data = array();
|
||||||
|
$sql = 'SELECT * FROM ' . $className;
|
||||||
|
if ($value) {
|
||||||
|
$sql .= ' WHERE ' . $field . ' = ?';
|
||||||
|
}
|
||||||
|
|
||||||
|
$statement = \Dba::read($sql, array($value));
|
||||||
|
while ($object = \Dba::fetch_object($statement, $this->modelClassName)) {
|
||||||
|
$data[$object->getId()] = $object;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __call($name, $arguments)
|
||||||
|
{
|
||||||
|
if (preg_match('/^findBy(.*)$/', $name, $matches)) {
|
||||||
|
return $this->findBy($matches[1], count($arguments) ? $arguments[0] : null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTableName()
|
||||||
|
{
|
||||||
|
$className = get_called_class();
|
||||||
|
$nameParts = explode('\\', $className);
|
||||||
|
$tableName = preg_replace_callback(
|
||||||
|
'/(?<=.)([A-Z])/',
|
||||||
|
function($m) {
|
||||||
|
return '_' . strtolower($m[0]);
|
||||||
|
}, end($nameParts));
|
||||||
|
return lcfirst($tableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add(DatabaseObject $object)
|
||||||
|
{
|
||||||
|
$properties = $object->getDirtyProperties();
|
||||||
|
$this->setPrivateProperty(
|
||||||
|
$object,
|
||||||
|
'id',
|
||||||
|
$this->insertRecord($properties)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(DatabaseObject $object)
|
||||||
|
{
|
||||||
|
if ($object->isDirty()) {
|
||||||
|
$properties = $object->getDirtyProperties();
|
||||||
|
$this->updateRecord($properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove(DatabaseObject $object)
|
||||||
|
{
|
||||||
|
$id = $object->getId();
|
||||||
|
$this->deleteRecord($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function insertRecord($properties)
|
||||||
|
{
|
||||||
|
$sql = 'INSERT INTO ' . $this->getTableName() . ' (' . implode(',', array_keys($properties)) . ')'
|
||||||
|
. ' VALUES(' . implode(',', array_fill(0, count($properties), '?')) . ')';
|
||||||
|
//print_r($properties);
|
||||||
|
\Dba::write(
|
||||||
|
$sql,
|
||||||
|
array_values($this->resolveObjects($properties))
|
||||||
|
);
|
||||||
|
return \Dba::insert_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function updateRecord($properties)
|
||||||
|
{
|
||||||
|
$properties[] = $properties['id'];
|
||||||
|
$sql = 'UPDATE ' . $this->getTableName()
|
||||||
|
. ' SET ' . implode(',', $this->getKeyValuePairs($properties))
|
||||||
|
. ' WHERE id = ?';
|
||||||
|
\Dba::write(
|
||||||
|
$sql,
|
||||||
|
array_values($this->resolveObjects($properties))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function deleteRecord($id)
|
||||||
|
{
|
||||||
|
$sql = 'DELETE FROM ' . $this->getTableName()
|
||||||
|
. ' WHERE id = ?';
|
||||||
|
\Dba::write($sql, array($id));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getKeyValuePairs($properties)
|
||||||
|
{
|
||||||
|
$pairs = array();
|
||||||
|
foreach ($properties as $property => $value) {
|
||||||
|
$pairs[] = $property . '= ?';
|
||||||
|
}
|
||||||
|
return $pairs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a private or protected variable.
|
||||||
|
* Only used in case where a property should not publicly writable
|
||||||
|
* @param Object $object
|
||||||
|
* @param string $property
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
protected function setPrivateProperty(Object $object, $property, $value)
|
||||||
|
{
|
||||||
|
$reflectionClass = new \ReflectionClass(get_class($object));
|
||||||
|
$ReflectionProperty = $reflectionClass->getProperty($property);
|
||||||
|
$ReflectionProperty->setAccessible(true);
|
||||||
|
$ReflectionProperty->setValue($object, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve all objects into id's
|
||||||
|
* @param array $properties
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function resolveObjects(array $properties)
|
||||||
|
{
|
||||||
|
foreach($properties as $property => $value) {
|
||||||
|
if(is_object($value)) {
|
||||||
|
$properties[$property] = $value->getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue