Property classes and mysql-schema

This commit is contained in:
RainLoop Team 2013-11-28 04:52:50 +04:00
parent 103c6efb85
commit 98cc5c0fe6
5 changed files with 353 additions and 14 deletions

View file

@ -0,0 +1,110 @@
<?php
namespace RainLoop\Providers\PersonalAddressBook\Classes;
class Contact
{
/**
* @var int
*/
public $IdContact;
/**
* @var int
*/
public $IdUser;
/**
* @var string
*/
public $DisplayName;
/**
* @var string
*/
public $DisplayEmail;
/**
* @var string
*/
public $DisplayInList;
/**
* @var bool
*/
public $IsAuto;
/**
* @var bool
*/
public $IsShare;
/**
* @var int
*/
public $Created;
/**
* @var int
*/
public $Updated;
/**
* @var array
*/
public $Tags;
/**
* @var array
*/
public $Properties;
public function __construct()
{
$this->Clear();
}
public function Clear()
{
$this->IdContact = 0;
$this->IdUser = 0;
$this->DisplayName = '';
$this->DisplayEmail = '';
$this->DisplayInList = '';
$this->IsAuto = false;
$this->IsShare = false;
$this->Changed = \time();
$this->Tags = array();
$this->Properties = array();
}
public function InitBeforeWrite()
{
$sDisplayName = '';
$sDisplayEmail = '';
foreach ($this->Properties as /* @var $oProperty \RainLoop\Providers\PersonalAddressBook\Classes\Property */ $oProperty)
{
if ($oProperty)
{
$oProperty->InitBeforeWrite();
if ('' === $sDisplayName && \RainLoop\Providers\PersonalAddressBook\Enumerations\PropertyType::FULLNAME === $oProperty->Type)
{
$sDisplayName = $oProperty->Value;
}
if ('' === $sDisplayEmail && $oProperty->IsEmail())
{
$sDisplayEmail = $oProperty->Value;
}
}
}
$this->DisplayName = $sDisplayName;
$this->DisplayEmail = $sDisplayEmail;
$this->DisplayInList = 0 < \strlen($this->DisplayName) ? $this->DisplayName : (!empty($this->DisplayEmail) ? $this->DisplayEmail : '');
$this->Changed = \time();
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace RainLoop\Providers\PersonalAddressBook\Classes;
use RainLoop\Providers\PersonalAddressBook\Enumerations\PropertyType;
class Property
{
/**
* @var int
*/
public $IdContact;
/**
* @var int
*/
public $IdUser;
/**
* @var int
*/
public $Type;
/**
* @var string
*/
public $TypeCustom;
/**
* @var string
*/
public $Value;
/**
* @var string
*/
public $ValueClear;
/**
* @var int
*/
public $Frec;
public function __construct()
{
$this->Clear();
}
public function Clear()
{
$this->IdContact = 0;
$this->IdUser = 0;
$this->Type = PropertyType::UNKNOWN;
$this->TypeCustom = '';
$this->Value = '';
$this->ValueClear = '';
$this->Frec = 0;
}
/**
* @return bool
*/
public function IsEmail()
{
return \in_array($this->Type, array(
PropertyType::EMAIl_PERSONAL, PropertyType::EMAIl_BUSSINES, PropertyType::EMAIl_OTHER
));
}
/**
* @return bool
*/
public function IsPhone()
{
return \in_array($this->Type, array(
PropertyType::PHONE_PERSONAL, PropertyType::PHONE_BUSSINES, PropertyType::PHONE_OTHER,
PropertyType::MOBILE_PERSONAL, PropertyType::MOBILE_BUSSINES, PropertyType::MOBILE_OTHER,
PropertyType::FAX_BUSSINES, PropertyType::FAX_OTHER
));
}
public function InitBeforeWrite()
{
// trimer
$this->Value = \trim($this->Value);
$this->ValueClear = \trim($this->ValueClear);
$this->TypeCustom = \trim($this->TypeCustom);
if (0 < \strlen($this->Value))
{
// lower
if ($this->IsEmail())
{
$this->Value = \strtolower($this->Value);
}
// phones clear value for searching
if ($this->IsPhone())
{
$sPhone = $this->Value;
$sPhone = \preg_replace('^[+]+', '', $sPhone);
$sPhone = \preg_replace('[^\d]', '', $sPhone);
$this->ValueClear = $sPhone;
}
}
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace RainLoop\Providers\PersonalAddressBook\Enumerations;
class PropertyType
{
const UNKNOWN = 0;
const FULLNAME = 10;
const NAME = 15;
const SURNAME = 16;
const MIDDLENAME = 17;
const NICK = 18;
const EMAIl_PERSONAL = 30;
const EMAIl_BUSSINES = 31;
const EMAIl_OTHER = 32;
const PHONE_PERSONAL = 50;
const PHONE_BUSSINES = 51;
const PHONE_OTHER = 52;
const MOBILE_PERSONAL = 60;
const MOBILE_BUSSINES = 61;
const MOBILE_OTHER = 62;
const FAX_BUSSINES = 70;
const FAX_OTHER = 71;
const FACEBOOK = 90;
const SKYPE = 91;
const GITHUB = 92;
const DESCRIPTION = 110;
const CUSTOM = 250;
}

View file

@ -46,7 +46,6 @@ class MySqlPersonalAddressBook implements \RainLoop\Providers\PersonalAddressBoo
throw new \Exception('class_exists=PDO');
}
$sVersionFile = '';
$sDsn = '';
$sDbLogin = '';
$sDbPassword = '';
@ -58,23 +57,11 @@ class MySqlPersonalAddressBook implements \RainLoop\Providers\PersonalAddressBoo
if ($oPdo)
{
$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (!@\file_exists($sVersionFile) ||
(string) @file_get_contents($sVersionFile) !== (string) \RainLoop\Providers\Contacts\Classes\Db::Version())
{
$this->syncTables($oPdo, $sVersionFile);
}
$oPdo->sqliteCreateFunction('SIMPLESEARCH', function ($sEmailValue, $sNameValue, $sMask) {
return \preg_match('/'.\preg_quote($sMask, '/').'/ui',
$sEmailValue.' '.$sNameValue) ? 1 : 0;
});
}
}
catch (\Exception $oException)
{
throw $oException;
// throw $oException;
$oPdo = false;
}

View file

@ -0,0 +1,94 @@
-- RainLoop Webmail initial contacts database structure
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;
-- Table structure for table `rainloop_system`
CREATE TABLE IF NOT EXISTS `rainloop_system` (
`name` varchar(50) NOT NULL,
`value_int` int(10) UNSIGNED NOT NULL DEFAULT '0',
`value_str` varchar(255) NOT NULL DEFAULT ''
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
-- Table structure for table `rainloop_pab_users`
CREATE TABLE IF NOT EXISTS `rainloop_pab_users` (
`id_user` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`email` varchar(255) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL,
UNIQUE `email_unique` (`email`),
PRIMARY KEY(`id_user`)
) /*!40000 ENGINE=INNODB */;
-- Table structure for table `rainloop_pab_contacts`
CREATE TABLE IF NOT EXISTS `rainloop_pab_contacts` (
`id_contact` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_user` int(10) UNSIGNED NOT NULL,
`display_name` varchar(255) NOT NULL DEFAULT '',
`display_email` varchar(255) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL DEFAULT '',
`display_in_list` varchar(255) NOT NULL DEFAULT '',
`is_auto` tinyint(1) NOT NULL DEFAULT '0',
`is_share` tinyint(1) NOT NULL DEFAULT '0',
`changed` int(10) UNSIGNED NOT NULL DEFAULT '0',
CONSTRAINT `id_user_fk_rainloop_pab_contacts` FOREIGN KEY (`id_user`)
REFERENCES `rainloop_pab_users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY(`id_contact`)
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
-- Table structure for table `rainloop_pab_prop`
CREATE TABLE IF NOT EXISTS `rainloop_pab_prop` (
`id_contact` int(10) UNSIGNED NOT NULL,
`id_user` int(10) UNSIGNED NOT NULL,
`type` int(10) UNSIGNED NOT NULL,
`type_custom` varchar(50) /*!40101 CHARACTER SET ascii COLLATE ascii_general_ci */ NOT NULL DEFAULT '',
`value` varchar(255) NOT NULL DEFAULT '',
`value_custom` varchar(255) NOT NULL DEFAULT '',
`frec` int(10) UNSIGNED NOT NULL DEFAULT '0',
CONSTRAINT `id_contact_fk_rainloop_pab_prop` FOREIGN KEY (`id_contact`)
REFERENCES `rainloop_pab_contacts` (`id_contact`) ON DELETE CASCADE ON UPDATE CASCADE
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
-- Table structure for table `rainloop_pab_tags`
CREATE TABLE IF NOT EXISTS `rainloop_pab_tags` (
`id_user` int(10) UNSIGNED NOT NULL,
`id_contact` int(10) UNSIGNED NOT NULL,
`name` varchar(255) NOT NULL,
UNIQUE `id_user_name_unique` (`id_user`, `name`),
CONSTRAINT `id_contact_fk_rainloop_pab_tags` FOREIGN KEY (`id_contact`)
REFERENCES `rainloop_pab_contacts` (`id_contact`) ON DELETE CASCADE ON UPDATE CASCADE
) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
-- RainLoop Webmail update contacts database structure
DELIMITER $$
DROP PROCEDURE IF EXISTS rainloop_pab_upgrade_database $$
CREATE PROCEDURE rainloop_pab_upgrade_database()
BEGIN
DECLARE new_version INT DEFAULT 1;
DECLARE current_version INT DEFAULT 0;
SELECT IFNULL(MAX(`value_int`), 0) INTO current_version FROM `rainloop_system` WHERE `name` = 'rainloop-pab-db-version';
IF current_version < 1 THEN
ALTER TABLE `rainloop_pab_prop` ADD INDEX `id_user_id_contact_index` (`id_user`, `id_contact`);
END IF;
-- TODO
--
-- IF current_version < 2 THEN
-- ALTER TABLE `rainloop_pab_prop` ADD INDEX `id_user_id_contact_index` (`id_user`, `id_contact`);
-- END IF;
DELETE FROM `rainloop_system` WHERE `name` = 'rainloop-pab-db-version' AND `value_int` <= new_version;
INSERT INTO `rainloop_system` (`name`, `value_int`) VALUES ('rainloop-pab-db-version', new_version);
END$$
CALL rainloop_pab_upgrade_database() $$
DROP PROCEDURE IF EXISTS rainloop_pab_upgrade_database $$
DELIMITER ;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;