1
0
Fork 0
mirror of synced 2025-04-20 01:21:01 +00:00

add any repository and new-style models

This commit is contained in:
Sergey Chazov 2020-08-21 10:41:11 +03:00 committed by Павел
parent 5122471abe
commit b851a17c49
7 changed files with 544 additions and 37 deletions

View file

@ -26,7 +26,9 @@ use Bitrix\Sale\Internals\OrderTable;
use \RetailCrm\ApiClient;
use RetailCrm\Exception\CurlException;
use Intaro\RetailCrm\Component\Loyalty\EventsHandlers;
use Intaro\RetailCrm\Model\Bitrix\ORM\ToModuleTable;
use Intaro\RetailCrm\Repository\OrderPropsRepository;
use Intaro\RetailCrm\Repository\PersonTypeRepository;
use Intaro\RetailCrm\Repository\ToModuleRepository;
IncludeModuleLangFile(__FILE__);
if (class_exists('intaro_retailcrm')) {
@ -223,6 +225,12 @@ class intaro_retailcrm extends CModule
}
}
include($this->INSTALL_PATH . '/../lib/model/bitrix/orderprops.php');
include($this->INSTALL_PATH . '/../lib/model/bitrix/tomodule.php');
include($this->INSTALL_PATH . '/../lib/repository/orderpropsrepository.php');
include($this->INSTALL_PATH . '/../lib/repository/persontyperepository.php');
include($this->INSTALL_PATH . '/../lib/repository/tomodulerepository.php');
$this->CopyFiles();
$this->addBonusPaySystem();
$this->addLPUserFields();
@ -1492,9 +1500,7 @@ class intaro_retailcrm extends CModule
*/
public function addLPOrderProps(): void
{
$persons = PersonTypeTable::query()
->setSelect(['ID'])
->fetchCollection();
$persons = PersonTypeRepository::getCollectionByWhere(['ID']);
foreach ($persons as $person) {
$personId = $person->getID();
@ -1564,19 +1570,16 @@ class intaro_retailcrm extends CModule
/**
* @param $personID
* @param $groupID
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
private function addBonusField($personID, $groupID): void
{
$bonusProp = OrderPropsTable::query()
->setSelect(['ID'])
->where([
['PERSON_TYPE_ID', '=', $personID],
['PROPS_GROUP_ID', '=', $groupID],
])
->fetch();
$where = [
['PERSON_TYPE_ID', '=', $personID],
['PROPS_GROUP_ID', '=', $groupID],
];
$bonusProp = OrderPropsRepository::getFirstByWhere(['ID'], $where);
if ($bonusProp === false) {
$fields = [
"REQUIRED" => "N",
@ -1604,11 +1607,12 @@ class intaro_retailcrm extends CModule
*/
private function addBonusPaySystem(): void
{
$arrPaySystemAction = PaySystemActionTable::query()->setSelect(['ID'])->where(
[
$arrPaySystemAction = PaySystemActionTable::query()
->setSelect(['ID'])
->where([
['ACTION_FILE', '=', self::BONUS_PAY_SYSTEM_CODE],
]
)->fetchCollection();
])
->fetchCollection();
if (count($arrPaySystemAction) === 0) {
@ -1650,26 +1654,29 @@ class intaro_retailcrm extends CModule
{
$eventManager = EventManager::getInstance();
include($this->INSTALL_PATH . '/../lib/model/bitrix/orm/tomodule.php');
include($this->INSTALL_PATH . '/../lib/repository/tomodulerepository.php');
foreach (self::SUBSCRIBE_LP_EVENTS as $event){
$events = ToModuleTable::query()->setSelect(['ID'])->where(
[
['from_module_id', '=', $event['FROM_MODULE']],
['to_module_id', '=', $this->MODULE_ID],
['to_method', '=', $event['EVENT_NAME'] . 'Handler'],
['to_class', '=', EventsHandlers::class],
]
)->fetchCollection();
if (count($events) === 0) {
$eventManager->registerEventHandler(
$event['FROM_MODULE'],
$event['EVENT_NAME'],
$this->MODULE_ID,
EventsHandlers::class,
$event['EVENT_NAME'] . 'Handler'
);
$select = ['ID'];
$where = [
['from_module_id', '=', $event['FROM_MODULE']],
['to_module_id', '=', $this->MODULE_ID],
['to_method', '=', $event['EVENT_NAME'] . 'Handler'],
['to_class', '=', EventsHandlers::class],
];
try {
$events = ToModuleRepository::getCollectionByWhere($select, $where);
if ($events !== null && count($events) === 0) {
$eventManager->registerEventHandler(
$event['FROM_MODULE'],
$event['EVENT_NAME'],
$this->MODULE_ID,
EventsHandlers::class,
$event['EVENT_NAME'] . 'Handler'
);
}
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
AddMessage2Log($exception->getMessage(), $this->MODULE_ID);
}
}
}

View file

@ -0,0 +1,95 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Model\Bitrix
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Model\Bitrix;
use Bitrix\Main\ORM\Objectify\EntityObject;
use Bitrix\Main\Type\DateTime;
use Bitrix\Sale\FuserTable;
use Bitrix\Sale\Internals\OrderPropsTable;
use Intaro\RetailCrm\Model\Bitrix\ORM\ToModuleTable;
/**
* Class ToModule
*
* @package Intaro\RetailCrm\Model\Bitrix
*
* @method int getId()
* @method void setId(int $id)
* @method int getPersonTypeId()
* @method void setPersonTypeId(int $personTypeId)
* @method string getName()
* @method void setName(string $name)
* @method string getType()
* @method void setType(string $type)
* @method bool getRequired()
* @method void setRequired(bool $required)
* @method string getDefaultValue()
* @method void setDefaultValue(string $defaultValue)
* @method int getSort()
* @method void setSort(int $sort)
* @method bool getUserProps()
* @method void setUserProps(bool $userProps)
* @method bool getIsLocation()
* @method void setIsLocation(bool $isLocation)
* @method int getPropsGroupId()
* @method void setPropsGroupId(int $propsGroupId)
* @method string getDescription()
* @method void setDescription(string $description)
* @method bool getIsEmail()
* @method void setIsEmail(bool $isEmail)
* @method bool getIsProfileName()
* @method void setIsProfileName(bool $isProfileName)
* @method bool getIsPayer()
* @method void setIsPayer(bool $isPayer)
* @method bool getIsLocation4tax()
* @method void setIsLocation4tax(bool $isLocation4tax)
* @method bool getIsFiltered()
* @method void setIsFiltered(bool $isFiltered)
* @method string getCode()
* @method void setCode(string $code)
* @method bool getIsZip()
* @method void setIsZip(bool $isZip)
* @method bool getIsPhone()
* @method void setIsPhone(bool $isPhone)
* @method bool getIsAddress()
* @method void setIsAddress(bool $isAddress)
* @method bool getActive()
* @method void setActive(bool $active)
* @method bool getUtil()
* @method void setUtil(bool $util)
* @method int getInputFieldLocation()
* @method void setInputFieldLocation(int $inputFieldLocation)
* @method bool getMultiple()
* @method void setMultiple(bool $multiple)
* @method string getSettings()
* @method void setSettings(string $settings)
* @method mixed getGroup()
* @method void setGroup($group)
* @method mixed getPersonType()
* @method void setPersonType($personType)
* @method string getEntityRegistryType()
* @method void setEntityRegistryType(string $entityRegistryType)
* @method string getXmlId()
* @method void setXmlId(string $xmlId)
*/
class OrderProps extends AbstractModelProxy
{
/**
* @return \Bitrix\Main\ORM\Objectify\EntityObject|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\SystemException
*/
protected static function newObject(): ?EntityObject
{
return OrderPropsTable::createObject();
}
}

View file

@ -0,0 +1,60 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Model\Bitrix
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Model\Bitrix;
use Bitrix\Main\ORM\Objectify\EntityObject;
use Bitrix\Main\Type\DateTime;
use Bitrix\Sale\FuserTable;
use Intaro\RetailCrm\Model\Bitrix\ORM\ToModuleTable;
/**
* Class ToModule
*
* @package Intaro\RetailCrm\Model\Bitrix
*
* @method int getId()
* @method void setId(int $id)
* @method DateTime getTimestampX()
* @method void setTimestampX(DateTime $timestampX)
* @method int getSort()
* @method void setSort(int $sort)
* @method string getFromModuleId()
* @method void setFromModuleId(string $fromModuleId)
* @method string getMessageId()
* @method void setMessageId(string $messageId)
* @method string getToModuleId()
* @method void setToModuleId(string $toModuleId)
* @method string getToPath()
* @method void setToPath(string $toPath)
* @method string getToClass()
* @method void setToClass(string $toClass)
* @method string getToMethod()
* @method void setToMethod(string $toMethod)
* @method string getToMethodArg()
* @method void setToMethodArg(string $toMethodArg)
* @method int getVersion()
* @method void setVersion(int $version)
* @method string getUniqueId()
* @method void setUniqueId(string $uniqueId)
*/
class ToModule extends AbstractModelProxy
{
/**
* @return \Bitrix\Main\ORM\Objectify\EntityObject|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\SystemException
*/
protected static function newObject(): ?EntityObject
{
return ToModuleTable::createObject();
}
}

View file

@ -0,0 +1,64 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Model\Api
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Repository;
use Bitrix\Sale\FuserTable;
use Bitrix\Sale\Internals\OrderPropsTable;
use Intaro\RetailCrm\Model\Bitrix\Fuser;
use Bitrix\Main\ORM\Objectify\EntityObject;
use Intaro\RetailCrm\Model\Bitrix\OrderProps;
use Intaro\RetailCrm\Model\Bitrix\ORM\ToModuleTable;
use Intaro\RetailCrm\Model\Bitrix\ToModule;
/**
* Class RepositoryRepository
*
* @package Intaro\RetailCrm\Repository
*/
class OrderPropsRepository extends AbstractRepository
{
public static function getFirstByWhere(array $select, array $where): ?ToModule
{
return static::getWrapped(ToModuleTable::query()
->setSelect($select)
->where($where)
->fetchObject());
}
/**
* @param int $id
*
* @return OrderProps|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function getById(int $id): ?OrderProps
{
return static::getWrapped(OrderPropsTable::getByPrimary($id)->fetchObject());
}
/**
* @param \Bitrix\Main\ORM\Objectify\EntityObject|null $entityObject
*
* @return \Intaro\RetailCrm\Model\Bitrix\Fuser|null
*/
private static function getWrapped(?EntityObject $entityObject): ?OrderProps
{
if (null === $entityObject) {
return null;
}
return new OrderProps($entityObject);
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Model\Api
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Repository;
use Bitrix\Main\Type\Collection;
use Bitrix\Sale\Internals\PersonTypeTable;
/**
* Class ToModuleRepository
*
* @package Intaro\RetailCrm\Repository
*/
class PersonTypeRepository extends AbstractRepository
{
/**
* @param array $select
* @param array $where
* @return \Bitrix\Main\Type\Collection|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function getCollectionByWhere(array $select = ['*'], array $where = []): ?Collection
{
return PersonTypeTable::query()
->setSelect($select)
->where($where)
->fetchCollection();
}
}

View file

@ -0,0 +1,56 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Model\Api
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Repository;
use Bitrix\Main\Type\Collection;
use Intaro\RetailCrm\Model\Bitrix\ORM\ToModuleTable;
use Intaro\RetailCrm\Model\Bitrix\ToModule;
/**
* Class ToModuleRepository
*
* @package Intaro\RetailCrm\Repository
*/
class ToModuleRepository extends AbstractRepository
{
/**
* @param array $select
* @param array $where
* @return \Intaro\RetailCrm\Model\Bitrix\ToModule|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function getFirstByWhere(array $select, array $where): ?ToModule
{
return ToModuleTable::query()
->setSelect($select)
->where($where)
->fetchObject();
}
/**
* @param array $select
* @param array $where
* @return \Bitrix\Main\Type\Collection|null
* @throws \Bitrix\Main\ArgumentException
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
*/
public static function getCollectionByWhere(array $select, array $where): ?Collection
{
return ToModuleTable::query()
->setSelect($select)
->where($where)
->fetchCollection();
}
}

View file

@ -0,0 +1,186 @@
<?php
function bitrixNameToCamelCase($string, $capitalizeFirstCharacter = false)
{
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($string))));
if (!$capitalizeFirstCharacter) {
$str[0] = strtolower($str[0]);
}
return $str;
}
$typeMapping = [
'integer' => 'int',
'string' => 'string',
'text' => 'string',
'boolean' => 'bool',
'datetime' => 'DateTime',
'date' => 'DateTime'
];
$fieldMap = array(
'ID' => array(
'primary' => true,
'autocomplete' => true,
'data_type' => 'integer',
'format' => '/^[0-9]{1,11}$/',
),
'PERSON_TYPE_ID' => array(
'required' => true,
'data_type' => 'integer',
'format' => '/^[0-9]{1,11}$/',
),
'NAME' => array(
'required' => true,
'data_type' => 'string',
),
'TYPE' => array(
'required' => true,
'data_type' => 'string',
),
'REQUIRED' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'DEFAULT_VALUE' => array(
'data_type' => 'string',
),
'SORT' => array(
'data_type' => 'integer',
'format' => '/^[0-9]{1,11}$/',
),
'USER_PROPS' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_LOCATION' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'PROPS_GROUP_ID' => array(
'required' => true,
'data_type' => 'integer',
'format' => '/^[0-9]{1,11}$/',
),
'DESCRIPTION' => array(
'data_type' => 'string',
),
'IS_EMAIL' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_PROFILE_NAME' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_PAYER' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_LOCATION4TAX' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_FILTERED' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'CODE' => array(
'data_type' => 'string',
),
'IS_ZIP' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_PHONE' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'IS_ADDRESS' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'ACTIVE' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'UTIL' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'INPUT_FIELD_LOCATION' => array(
'data_type' => 'integer',
'format' => '/^[0-9]{1,11}$/',
),
'MULTIPLE' => array(
'data_type' => 'boolean',
'values' => array('N', 'Y'),
),
'SETTINGS' => array(
'data_type' => 'string',
),
'GROUP' => array(
'data_type' => 'Bitrix\Sale\Internals\OrderPropsGroupTable',
'reference' => array('=this.PROPS_GROUP_ID' => 'ref.ID'),
'join_type' => 'LEFT',
),
'PERSON_TYPE' => array(
'data_type' => 'Bitrix\Sale\Internals\PersonTypeTable',
'reference' => array('=this.PERSON_TYPE_ID' => 'ref.ID'),
'join_type' => 'LEFT',
),
'ENTITY_REGISTRY_TYPE' => array(
'data_type' => 'string',
),
'XML_ID' => array(
'data_type' => 'string',
),
); //replace this array AnyTable::getMap();
foreach ($fieldMap as $fieldName => $fieldType) {
if (!is_string($fieldName)) {
continue;
}
$setArgumentName = bitrixNameToCamelCase($fieldName);
$getter = bitrixNameToCamelCase('GET_' . $fieldName);
$setter = bitrixNameToCamelCase('SET_' . $fieldName);
$dataType = '';
if (isset($typeMapping[$fieldType['data_type']])) {
$dataType = $typeMapping[$fieldType['data_type']];
} else {
if (strpos($fieldType['data_type'], '\\') !== false) {
if (class_exists($fieldType['data_type'])) {
$dataType = $fieldType['data_type'];
} else {
$dataType = 'mixed';
}
} else {
$dataType = $fieldType['data_type'];
}
}
printf(' * @method %s %s()' . PHP_EOL, empty($dataType) ? 'mixed' : $dataType, $getter);
printf(' * @method void %s(%s%s$%s)' . PHP_EOL, $setter, $dataType == 'mixed' ? '' : $dataType, $dataType == 'mixed' ? '' : ' ', $setArgumentName);
}