diff --git a/Admin/Installer.php b/Admin/Installer.php index b5b8a77..bc1984e 100644 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -46,14 +46,14 @@ class Installer extends InstallerAbstract case DatabaseType::MYSQL: $dbPool->get('core')->con->beginTransaction(); - $dbPool->get('core')->con->prepare( + $dbPool->get('core')->con->prepare(/* todo: maybe add client logo? */ 'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'clientmgmt_client` ( `clientmgmt_client_id` int(11) NOT NULL AUTO_INCREMENT, `clientmgmt_client_no` int(11) NOT NULL, `clientmgmt_client_no_reverse` int(11) NOT NULL, `clientmgmt_client_status` tinyint(2) NOT NULL, `clientmgmt_client_type` tinyint(2) NOT NULL, - `clientmgmt_client_TaxId` varchar(50) NOT NULL, + `clientmgmt_client_taxid` varchar(50) NOT NULL, `clientmgmt_client_info` text NOT NULL, `clientmgmt_client_created_at` datetime NOT NULL, `clientmgmt_client_account` int(11) NOT NULL, @@ -67,6 +67,60 @@ class Installer extends InstallerAbstract ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_ibfk_1` FOREIGN KEY (`clientmgmt_client_account`) REFERENCES `' . $dbPool->get('core')->prefix . 'profile_account` (`profile_account_id`);' )->execute(); + $dbPool->get('core')->con->prepare( + 'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'clientmgmt_client_address` ( + `clientmgmt_client_address_id` int(11) NOT NULL AUTO_INCREMENT, + `clientmgmt_client_address_client` int(11) NOT NULL, + `clientmgmt_client_address_address` int(11) NOT NULL, + `clientmgmt_client_address_type` tinyint(2) NOT NULL, + PRIMARY KEY (`clientmgmt_client_address_id`), + KEY `clientmgmt_client_address_client` (`clientmgmt_client_address_client`), + KEY `clientmgmt_client_address_address` (`clientmgmt_client_address_address`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;' + )->execute(); + + $dbPool->get('core')->con->prepare( + 'ALTER TABLE `' . $dbPool->get('core')->prefix . 'clientmgmt_client_address` + ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_address_ibfk_1` FOREIGN KEY (`clientmgmt_client_address_client`) REFERENCES `' . $dbPool->get('core')->prefix . 'clientmgmt_client` (`clientmgmt_client_id`), + ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_address_ibfk_2` FOREIGN KEY (`clientmgmt_client_address_address`) REFERENCES `' . $dbPool->get('core')->prefix . 'profile_address` (`profile_address_id`);' + )->execute(); + + $dbPool->get('core')->con->prepare( + 'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'clientmgmt_client_contactelement` ( + `clientmgmt_client_contactelement_id` int(11) NOT NULL AUTO_INCREMENT, + `clientmgmt_client_contactelement_dst` int(11) NOT NULL, + `clientmgmt_client_contactelement_src` int(11) NOT NULL, + `clientmgmt_client_contactelement_type` tinyint(2) NOT NULL, + PRIMARY KEY (`clientmgmt_client_contactelement_id`), + KEY `clientmgmt_client_contactelement_dst` (`clientmgmt_client_contactelement_dst`), + KEY `clientmgmt_client_contactelement_src` (`clientmgmt_client_contactelement_src`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;' + )->execute(); + + $dbPool->get('core')->con->prepare( + 'ALTER TABLE `' . $dbPool->get('core')->prefix . 'clientmgmt_client_contactelement` + ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_contactelement_ibfk_1` FOREIGN KEY (`clientmgmt_client_contactelement_src`) REFERENCES `' . $dbPool->get('core')->prefix . 'clientmgmt_client` (`clientmgmt_client_id`), + ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_contactelement_ibfk_2` FOREIGN KEY (`clientmgmt_client_contactelement_dst`) REFERENCES `' . $dbPool->get('core')->prefix . 'profile_contactelement` (`profile_contactelement_id`);' + )->execute(); + + $dbPool->get('core')->con->prepare( + 'CREATE TABLE if NOT EXISTS `' . $dbPool->get('core')->prefix . 'clientmgmt_client_media` ( + `clientmgmt_client_media_id` int(11) NOT NULL AUTO_INCREMENT, + `clientmgmt_client_media_dst` int(11) NOT NULL, + `clientmgmt_client_media_src` int(11) NOT NULL, + `clientmgmt_client_media_type` tinyint(2) NOT NULL, + PRIMARY KEY (`clientmgmt_client_media_id`), + KEY `clientmgmt_client_media_dst` (`clientmgmt_client_media_dst`), + KEY `clientmgmt_client_media_src` (`clientmgmt_client_media_src`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;' + )->execute(); + + $dbPool->get('core')->con->prepare( + 'ALTER TABLE `' . $dbPool->get('core')->prefix . 'clientmgmt_client_media` + ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_media_ibfk_1` FOREIGN KEY (`clientmgmt_client_media_src`) REFERENCES `' . $dbPool->get('core')->prefix . 'clientmgmt_client` (`clientmgmt_client_id`), + ADD CONSTRAINT `' . $dbPool->get('core')->prefix . 'clientmgmt_client_media_ibfk_2` FOREIGN KEY (`clientmgmt_client_media_dst`) REFERENCES `' . $dbPool->get('core')->prefix . 'media` (`media_id`);' + )->execute(); + $dbPool->get('core')->con->commit(); break; } diff --git a/Models/Client.php b/Models/Client.php index 763ae60..8846819 100644 --- a/Models/Client.php +++ b/Models/Client.php @@ -13,9 +13,10 @@ * @version 1.0.0 * @link http://orange-management.com */ -declare(strict_types=1); -namespace Modules\Profile\Models; +declare(strict_types = 1); +namespace Modules\ClientManagement\Models; +use Modules\Media\Models\Media; use Modules\Profile\Models\Account; /** @@ -29,95 +30,144 @@ use Modules\Profile\Models\Account; * @link http://orange-management.com * @since 1.0.0 */ -class Client extends Account +class Client { - private $number = 0; + private $id = 0; - private $numberReverse = 0; + private $number = 0; - private $status = 0; + private $numberReverse = 0; - private $type = 0; + private $status = 0; - private $taxId = ''; + private $type = 0; - private $info = ''; + private $taxId = ''; - private $createdAt = null; + private $info = ''; - public function __construct(int $id = 0) - { - $this->createdAt = new \DateTime('now'); + private $createdAt = null; - parent::__construct($id); - } + private $profile = null; - public function getNumber() : int - { - return $this->number; - } + private $files = []; - public function setNumber(int $number) /* : void */ - { - $this->number = $number; - } + private $contactElements = []; - public function getReverseNumber() - { - return $this->numberReverse; - } + private $address = []; - public function setReverseNumber($rev_no) /* : void */ - { - if(!is_scalar($rev_no)) { - throw new \Exception(); - } + public function __construct(int $id = 0) + { + $this->createdAt = new \DateTime('now'); + $this->profile = new Account(); + } - $this->numberReverse = $rev_no; - } + public function getId() : int + { + return $this->id; + } - public function getStatus() : int - { - return $this->status; - } + public function getNumber() : int + { + return $this->number; + } - public function setStatus(int $status) /* : void */ - { - $this->status = $status; - } + public function setNumber(int $number) /* : void */ + { + $this->number = $number; + } - public function getType() : int - { - return $this->type; - } + public function getReverseNumber() + { + return $this->numberReverse; + } - public function setType(int $type) /* : void */ - { - $this->type = $type; - } + public function setReverseNumber($rev_no) /* : void */ + { + if (!is_scalar($rev_no)) { + throw new \Exception(); + } - public function getTaxId() : string - { - return $this->taxId; - } + $this->numberReverse = $rev_no; + } - public function setTaxId(string $taxId) /* : void */ - { - $this->taxId = $taxId; - } + public function getStatus() : int + { + return $this->status; + } - public function getInfo() : string - { - return $this->info; - } + public function setStatus(int $status) /* : void */ + { + $this->status = $status; + } - public function setInfo(string $info) /* : void */ - { - return $this->info; - } + public function getType() : int + { + return $this->type; + } - public function getCreatedAt() : \DateTime - { - return $this->createdAt; - } + public function setType(int $type) /* : void */ + { + $this->type = $type; + } + + public function getTaxId() : string + { + return $this->taxId; + } + + public function setTaxId(string $taxId) /* : void */ + { + $this->taxId = $taxId; + } + + public function getInfo() : string + { + return $this->info; + } + + public function setInfo(string $info) /* : void */ + { + return $this->info; + } + + public function getCreatedAt() : \DateTime + { + return $this->createdAt; + } + + public function getAccount() : Account + { + return $this->profile; + } + + public function setAccount(Account $account) /* : void */ + { + $this->profile = $account; + } + + public function getFiles() : array + { + return $this->files; + } + + public function addFile(Media $file) /* : void */ + { + $this->files[] = $file; + } + + public function getAddresses() : array + { + return $this->address; + } + + public function getContactElements() : array + { + return $this->contactElements; + } + + public function getProfile() : Account + { + return $this->profile; + } } diff --git a/Models/ClientMapper.php b/Models/ClientMapper.php index e69de29..b06549d 100644 --- a/Models/ClientMapper.php +++ b/Models/ClientMapper.php @@ -0,0 +1,160 @@ + + * @author Dennis Eichhorn + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +declare(strict_types=1); +namespace Modules\ClientManagement\Models; + +use Modules\Media\Models\MediaMapper; +use Modules\Profile\Models\AccountMapper; +use Modules\Profile\Models\ContactElement; +use Modules\Profile\Models\ContactElementMapper; +use Modules\Profile\Models\ContactMapper; +use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Query\Builder; +use phpOMS\DataStorage\Database\Query\Column; +use phpOMS\DataStorage\Database\RelationType; + +class ClientMapper extends DataMapperAbstract +{ + /** + * Columns. + * + * @var array + * @since 1.0.0 + */ + protected static $columns = [ + 'clientmgmt_client_id' => ['name' => 'clientmgmt_client_id', 'type' => 'int', 'internal' => 'id'], + 'clientmgmt_client_no' => ['name' => 'clientmgmt_client_no', 'type' => 'int', 'internal' => 'number'], + 'clientmgmt_client_no_reverse' => ['name' => 'clientmgmt_client_no_reverse', 'type' => 'string', 'internal' => 'numberReverse'], + 'clientmgmt_client_status' => ['name' => 'clientmgmt_client_status', 'type' => 'int', 'internal' => 'status'], + 'clientmgmt_client_type' => ['name' => 'clientmgmt_client_type', 'type' => 'int', 'internal' => 'type'], + 'clientmgmt_client_taxid' => ['name' => 'clientmgmt_client_taxid', 'type' => 'string', 'internal' => 'taxId'], + 'clientmgmt_client_info' => ['name' => 'clientmgmt_client_info', 'type' => 'string', 'internal' => 'info'], + 'clientmgmt_client_at' => ['name' => 'clientmgmt_client_at', 'type' => 'DateTime', 'internal' => 'createdAt'], + 'clientmgmt_client_account' => ['name' => 'clientmgmt_client_account', 'type' => 'int', 'internal' => 'profile'], + ]; + + /** + * Primary table. + * + * @var string + * @since 1.0.0 + */ + protected static $table = 'clientmgmt_client'; + + /** + * Primary field name. + * + * @var string + * @since 1.0.0 + */ + protected static $primaryField = 'clientmgmt_client_id'; + + /** + * Created at column + * + * @var string + * @since 1.0.0 + */ + protected static $createdAt = 'clientmgmt_client_at'; + + /** + * Has one relation. + * + * @var array + * @since 1.0.0 + */ + protected static $ownsOne = [ + 'profile' => [ + 'mapper' => AccountMapper::class, + 'src' => 'clientmgmt_client_account', + ], + ]; + + protected static $hasMany = [ + 'files' => [ + 'mapper' => MediaMapper::class, /* mapper of the related object */ + 'table' => 'clientmgmt_client_media', /* table of the related object, null if no relation table is used (many->1) */ + 'dst' => 'clientmgmt_client_media_dst', + 'src' => 'clientmgmt_client_media_src', + ], + 'contact' => [ + 'mapper' => ContactElementMapper::class, + 'table' => 'clientmgmt_client_contactelement', + 'dst' => 'clientmgmt_client_contactelement_dst', + 'src' => 'clientmgmt_client_contactelement_src', + ], + ]; + + /** + * Create object. + * + * @param mixed $obj Object + * @param int $relations Behavior for relations creation + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function create($obj, int $relations = RelationType::ALL) + { + try { + $objId = parent::create($obj, $relations); + $query = new Builder(self::$db); + + $query->prefix(self::$db->getPrefix()) + ->insert( + 'account_permission_account', + 'account_permission_from', + 'account_permission_for', + 'account_permission_id1', + 'account_permission_id2', + 'account_permission_r', + 'account_permission_w', + 'account_permission_m', + 'account_permission_d', + 'account_permission_p' + ) + ->into('account_permission') + ->values(1, 'clientmgmt_client', 'clientmgmt_client', 1, $objId, 1, 1, 1, 1, 1); + + self::$db->con->prepare($query->toSql())->execute(); + } catch (\Exception $e) { + var_dump($e->getMessage()); + + return false; + } + + return $objId; + } + + /** + * Get object. + * + * @param mixed $primaryKey Key + * @param int $relations Load relations + * @param mixed $fill Object to fill + * + * @return Client + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null) + { + return parent::get((int) $primaryKey, $relations, $fill); + } +}