Beispiel #1
0
 /**
  * Available keys in $additional
  * REBALANCE - if set to true, method will rebalance tree after insertion
  */
 public static function addExtended(array $data, array $additional = array())
 {
     $rebalance = isset($additional['REBALANCE']) && $additional['REBALANCE'] == false;
     // determine LEFT_MARGIN, RIGHT_MARGIN and DEPTH_LEVEL
     if ($data['PARENT_ID'] = intval($data['PARENT_ID'])) {
         // if we have PARENT_ID set, just use it`s info
         $node = self::getNodeInfo($data['PARENT_ID']);
         $needResort = true;
         $data['LEFT_MARGIN'] = $node['RIGHT_MARGIN'];
         $data['RIGHT_MARGIN'] = $node['RIGHT_MARGIN'] + 1;
         $data['DEPTH_LEVEL'] = $node['DEPTH_LEVEL'] + 1;
         $data['PARENT_ID'] = $node['ID'];
     } else {
         // otherwise, we assume we have "virtual root node", that has LEFT_MARGIN == 0 and RIGHT_MARGIN == +INFINITY
         // it allows us to have actually a forest, not a tree
         $rm = self::getMaxMargin();
         $needResort = false;
         $data['LEFT_MARGIN'] = $rm > 1 ? $rm + 1 : 1;
         $data['RIGHT_MARGIN'] = $rm > 1 ? $rm + 2 : 2;
         $data['DEPTH_LEVEL'] = 1;
         $data['PARENT_ID'] = 0;
     }
     // process insert options: INSERT_AFTER and INSERT_BEFORE
     //self::processInsertInstruction($data);
     $addResult = parent::add($data);
     if ($addResult->isSuccess() && $needResort && $rebalance) {
         self::rebalance($node, $addResult->getId());
     }
     return $addResult;
 }
Beispiel #2
0
 public static function add(array $data)
 {
     if (isset($data['ICON']) && is_array($data['ICON'])) {
         $iconError = $data['ICON']['name'] ? \CFile::checkImageFile($data['ICON']) : null;
         if (is_null($iconError)) {
             $data['ICON']['MODULE_ID'] = 'mail';
             \CFile::saveForDB($data, 'ICON', 'mail/mailservices/icon');
         }
     }
     return parent::add($data);
 }
Beispiel #3
0
 public static function add($data)
 {
     if (!isset($data['SORT'])) {
         $data['TEMPLATE_ID'] = intval($data['TEMPLATE_ID']);
         if ($data['TEMPLATE_ID']) {
             $item = static::getList(array('runtime' => array('MAX_SORT' => array('dat_type' => 'integer', 'expression' => array('MAX(SORT)'))), 'filter' => array('=TEMPLATE_ID' => $data['TEMPLATE_ID']), 'select' => array('MAX_SORT')))->fetch();
             if (intval($item['MAX_SORT'])) {
                 $data['SORT'] = intval($item['MAX_SORT']) + 1;
             } else {
                 $data['SORT'] = 1;
             }
         }
     }
     return parent::add($data);
 }
Beispiel #4
0
 public static function add($data = array())
 {
     if (isset($data['NAME'])) {
         $name = $data['NAME'];
         unset($data['NAME']);
     }
     $addResult = parent::add($data);
     // add connected data
     if ($addResult->isSuccess()) {
         $primary = $addResult->getId();
         // names
         if (isset($name)) {
             Name\TypeTable::addMultipleForOwner($primary, $name);
         }
     }
     return $addResult;
 }
Beispiel #5
0
 public static function add(array $data)
 {
     if (isset($data['NAME'])) {
         $name = $data['NAME'];
         unset($data['NAME']);
     }
     $addResult = parent::add($data);
     // add connected data
     if ($addResult->isSuccess()) {
         $primary = $addResult->getId();
         // names
         if (isset($name)) {
             Name\GroupTable::addMultipleForOwner($primary, $name);
         }
         // set flag that indicates whether project still uses groups or not
         self::setGroupUsage();
     }
     return $addResult;
 }
Beispiel #6
0
    /**
     * @param array $data
     *
     * @return Entity\AddResult
     * @throws \Bitrix\Main\SystemException
     */
    public static function add($data)
    {
        $result = parent::add($data);
        if (!$result->isSuccess(true)) {
            return $result;
        }
        // create table in db
        $dbtype = strtolower($GLOBALS['DB']->type);
        $connection = Application::getConnection();
        $sqlHelper = $connection->getSqlHelper();
        if ($dbtype == 'mysql') {
            $connection->query('
				CREATE TABLE ' . $sqlHelper->quote($data['TABLE_NAME']) . ' (ID int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (ID))
			');
        } elseif ($dbtype == 'mssql') {
            $connection->query('
				CREATE TABLE ' . $sqlHelper->quote($data['TABLE_NAME']) . ' (ID int NOT NULL IDENTITY (1, 1),
				CONSTRAINT ' . $data['TABLE_NAME'] . '_ibpk_1 PRIMARY KEY (ID))
			');
        } elseif ($dbtype == 'oracle') {
            $connection->query('
				CREATE TABLE ' . $sqlHelper->quote($data['TABLE_NAME']) . ' (ID number(11) NOT NULL, PRIMARY KEY (ID))
			');
            $connection->query('
				CREATE SEQUENCE sq_' . $data['TABLE_NAME'] . '
			');
            $connection->query('
				CREATE OR REPLACE TRIGGER ' . $data['TABLE_NAME'] . '_insert
					BEFORE INSERT
					ON ' . $sqlHelper->quote($data['TABLE_NAME']) . '
					FOR EACH ROW
						BEGIN
						IF :NEW.ID IS NULL THEN
							SELECT sq_' . $data['TABLE_NAME'] . '.NEXTVAL INTO :NEW.ID FROM dual;
						END IF;
					END;
			');
        } else {
            throw new Main\SystemException('Unknown DB type');
        }
        return $result;
    }
Beispiel #7
0
 public static function add(array $data)
 {
     if (isset($data['NAME'])) {
         $name = $data['NAME'];
         unset($data['NAME']);
     }
     if ((string) $data['DISPLAY_SORT'] == '' && (string) $data['SORT'] != '') {
         $data['DISPLAY_SORT'] = $data['SORT'];
     }
     $addResult = parent::add($data);
     // add connected data
     if ($addResult->isSuccess()) {
         $primary = $addResult->getId();
         // names
         if (isset($name)) {
             Name\TypeTable::addMultipleForOwner($primary, $name);
         }
     }
     return $addResult;
 }
Beispiel #8
0
 /**
  * Available keys in $behaviour
  * REBALANCE - if set to true, method will rebalance tree after insertion
  */
 public static function add($data = array(), $behaviour = array('REBALANCE' => true))
 {
     $needResort = false;
     if (!is_array($behaviour)) {
         $behaviour = array();
     }
     if (!isset($behaviour['REBALANCE'])) {
         $behaviour['REBALANCE'] = true;
     }
     // determine LEFT_MARGIN, RIGHT_MARGIN and DEPTH_LEVEL
     if ($data['PARENT_ID'] = intval($data['PARENT_ID'])) {
         // if we have PARENT_ID set, just use it`s info
         $node = self::getNodeInfo($data['PARENT_ID']);
         if ($behaviour['REBALANCE']) {
             $needResort = true;
         }
         $data['LEFT_MARGIN'] = $node['RIGHT_MARGIN'];
         $data['RIGHT_MARGIN'] = $node['RIGHT_MARGIN'] + 1;
         $data['DEPTH_LEVEL'] = $node['DEPTH_LEVEL'] + 1;
         $data['PARENT_ID'] = $node['ID'];
     } else {
         // otherwise, we assume we have "virtual root node", that has LEFT_MARGIN == 0 and RIGHT_MARGIN == +INFINITY
         // it allows us to have actually a forest, not a tree
         $rm = self::getMaxMargin();
         $data['LEFT_MARGIN'] = $rm > 1 ? $rm + 1 : 1;
         $data['RIGHT_MARGIN'] = $rm > 1 ? $rm + 2 : 2;
         $data['DEPTH_LEVEL'] = 1;
         $data['PARENT_ID'] = 0;
     }
     // process insert options: INSERT_AFTER and INSERT_BEFORE
     //self::processInsertInstruction($data);
     $addResult = parent::add($data);
     if ($addResult->isSuccess() && $needResort) {
         self::manageFreeSpace($node['RIGHT_MARGIN'], 2, self::SPACE_ADD, $addResult->getId());
     }
     return $addResult;
 }
Beispiel #9
0
 public static function add($sitemapId, $entityId)
 {
     parent::add(array('ENTITY_TYPE' => static::ENTITY_TYPE, 'ENTITY_ID' => $entityId, 'SITEMAP_ID' => $sitemapId));
 }
Beispiel #10
0
 /**
  * Adds a task to favorites for a particular (or current) user. This function DOES NOT check permissions.
  * 
  * @param mixed[] Primary key for \Bitrix\Tasks\Task\FavoriteTable entity
  * @param mixed[] Behaviour flags
  * 
  * 	<li> CHECK_EXISTENCE boolean if true, function will check if the task is already in favorites (+1 db query) (default true). Setting this to false may cause SQL error.
  *  <li> AFFECT_CHILDREN boolean if true, all child tasks also will be added to favorite. (default false)
  * 
  * @return \Bitrix\Main\Entity\AddResult
  */
 public static function add($data, $behaviour = array('CHECK_EXISTENCE' => true, 'AFFECT_CHILDREN' => false))
 {
     if (!is_array($behaviour)) {
         $behaviour = array();
     }
     if (!isset($behaviour['CHECK_EXISTENCE'])) {
         $behaviour['CHECK_EXISTENCE'] = true;
     }
     if (!isset($behaviour['AFFECT_CHILDREN'])) {
         $behaviour['AFFECT_CHILDREN'] = false;
     }
     $data = static::processPrimary($data);
     // already there, we dont want \Bitrix\Main\DB\SqlQueryException "Duplicate entry" crumble everywhere
     if ($behaviour['CHECK_EXISTENCE'] && static::check($data)) {
         $result = new \Bitrix\Main\Entity\AddResult();
     } else {
         $result = parent::add($data);
     }
     if ($result->isSuccess() && $behaviour['AFFECT_CHILDREN']) {
         // add also all children...
         $res = TaskTable::getChildrenTasksData($data['TASK_ID'], array('runtime' => TaskTable::getRuntimeFieldMixins(array('IN_FAVORITE'), array('USER_ID' => $data['USER_ID'])), 'select' => array('IN_FAVORITE')));
         while ($item = $res->fetch()) {
             if (!$item['IN_FAVORITE']) {
                 // our client
                 static::add(array('TASK_ID' => $item['ID']), array('AFFECT_CHILDREN' => false, 'CHECK_EXISTENCE' => false));
             }
         }
     }
     return $result;
 }
Beispiel #11
0
 /**
  * Adds a new connection between location and entity
  * 
  * @param mixed[] $data data to add (See getMap() of a certain implementation for $data key details)
  * 
  * @return Bitrix\Main\Entity\AddResult
  */
 public static function add($data = array())
 {
     $res = parent::add($data);
     if ($res->isSuccess()) {
         static::setLinkUsage($data[static::getLinkField()], $data[static::getTypeField()], true);
     }
     $GLOBALS['CACHE_MANAGER']->ClearByTag('sale-location-data');
     return $res;
 }
Beispiel #12
0
 /**
  * Links one item with another. Low-level method.
  */
 public static function createLink($id, $parentId, $behaviour = array('LINK_DATA' => array()))
 {
     static::applyCreateRestrictions($id, $parentId);
     if (!is_array($behaviour)) {
         $behaviour = array();
     }
     // check here if there is a link already
     //_dump_r('Create: '.$parentId.' => '.$id.' ('.$behaviour['LINK_DATA']['TYPE'].')');
     $parentColName = static::getPARENTIDColumnName();
     $idColName = static::getIDColumnName();
     // delete broken previous links, if any (but not the link to itself)
     $dbConnection = Main\HttpApplication::getConnection();
     $dbConnection->query("delete from " . static::getTableName() . " where " . $idColName . " = '" . intval($id) . "' and " . $parentColName . " != '" . intval($id) . "'");
     $success = true;
     $lastAddResult = null;
     if ($parentId) {
         // check if parent exists. if not - add it
         $item = static::getList(array('filter' => array('=' . $parentColName => $parentId, '=' . $idColName => $parentId), 'select' => array($idColName)))->fetch();
         if (!is_array($item)) {
             //_dump_r('link: '.$parentId.' => '.$parentId);
             $lastAddResult = parent::add(array($parentColName => $parentId, $idColName => $parentId));
             if (!$lastAddResult->isSuccess()) {
                 $success = false;
             }
         }
     }
     // check if link to itself exists. if not - add it
     $item = static::getList(array('filter' => array('=' . $parentColName => $id, '=' . $idColName => $id), 'select' => array($idColName)))->fetch();
     if (!is_array($item)) {
         //_dump_r('link: '.$id.' => '.$id);
         $lastAddResult = parent::add(array($parentColName => $id, $idColName => $id));
         if (!$lastAddResult->isSuccess()) {
             $success = false;
         }
     }
     $linkedWithParent = false;
     // TODO: the following part could be rewritten using just db-side insert-select
     if ($success && $parentId) {
         $subtree = array();
         $res = static::getSubTree($id);
         while ($item = $res->fetch()) {
             $subtree[] = $item[$idColName];
         }
         // link each child (including self) to each parent in the path(es)
         $res = static::getPathToNode($parentId, array('select' => array($parentColName)));
         while ($item = $res->fetch()) {
             foreach ($subtree as $itemId) {
                 if ($item[$parentColName] == $parentId && $itemId == $id) {
                     if (!is_array($behaviour['LINK_DATA'])) {
                         $behaviour['LINK_DATA'] = array();
                     }
                     $lastAddResult = parent::add(array_merge(array($idColName => $id, $parentColName => $parentId, static::getDIRECTColumnName() => true), $behaviour['LINK_DATA']));
                     if (!$lastAddResult->isSuccess()) {
                         $success = false;
                         break;
                     }
                 } else {
                     $lastAddResult = parent::add(array($idColName => $itemId, $parentColName => $item[$parentColName]));
                     if (!$lastAddResult->isSuccess()) {
                         $success = false;
                         break;
                     }
                 }
             }
         }
     }
     return array('RESULT' => $success, 'LAST_DB_RESULT' => $lastAddResult);
 }
Beispiel #13
0
 public static function add(array $data)
 {
     $entityName = static::getEntity()->getName();
     $hlblock = HighloadBlockTable::getList(array('select' => array('ID'), 'filter' => array('=NAME' => $entityName)))->fetch();
     // add other fields
     $fields = $GLOBALS['USER_FIELD_MANAGER']->getUserFields('HLBLOCK_' . $hlblock['ID']);
     foreach ($data as $k => $v) {
         $arUserField = $fields[$k];
         if (is_callable(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesave"))) {
             $data[$k] = call_user_func_array(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesave"), array($arUserField, $data[$k]));
         }
         if (strlen($data[$k]) <= 0) {
             $data[$k] = false;
         } else {
             // convert string datetime to DateTime object
             if ($arUserField['USER_TYPE_ID'] == 'datetime') {
                 try {
                     $data[$k] = Type\DateTime::createFromUserTime($v);
                 } catch (Main\ObjectException $e) {
                     $data[$k] = '';
                 }
             }
         }
     }
     return parent::add($data);
 }
Beispiel #14
0
 /**
  * Ad subscription row
  *
  * @param array $parameters
  * @return \Bitrix\Main\DB\Result
  */
 public static function addUnSubscription(array $parameters = array())
 {
     $primary = array('MAILING_ID' => $parameters['MAILING_ID'], 'CONTACT_ID' => $parameters['CONTACT_ID']);
     $fields = array('IS_UNSUB' => 'Y');
     $row = static::getRowById($primary);
     if ($row) {
         $result = parent::update($primary, $fields);
         return $result->isSuccess();
     } else {
         $result = parent::add($fields + $parameters);
         return $result->isSuccess();
     }
 }
Beispiel #15
0
 /**
  * Links one item with another. Low-level method.
  */
 public static function createLink($id, $parentId, $behaviour = array())
 {
     $id = Assert::expectIntegerPositive($id, '$id');
     $parentId = Assert::expectIntegerNonNegative($parentId, '$parentId');
     // parent id might be equal to 0
     if (!is_array($behaviour)) {
         $behaviour = array();
     }
     $parentColName = static::getPARENTIDColumnName();
     $idColName = static::getIDColumnName();
     // delete broken previous links, if any
     $dbConnection = Main\HttpApplication::getConnection();
     $dbConnection->query("delete from " . static::getTableName() . " where " . $idColName . " = '" . intval($id) . "'");
     $success = true;
     $lastAddResult = null;
     // check if parent exists. if not - add it
     $item = static::getList(array('filter' => array('=' . $parentColName => $parentId, '=' . $idColName => $parentId), 'select' => array($idColName)))->fetch();
     if (!is_array($item)) {
         $lastAddResult = parent::add(array($parentColName => $parentId, $idColName => $parentId));
         if (!$lastAddResult->isSuccess()) {
             $success = false;
         }
     }
     $linkedWithParent = false;
     // the following part could be rewritten using just db-side insert-select
     if ($success) {
         if ($parentId > 0) {
             // link to all parents in the path
             $res = static::getPathToNode($parentId, array('select' => array($parentColName), 'filter' => array('!=' . $parentColName => $parentId)));
             while ($item = $res->fetch()) {
                 if ($item[$parentColName] == $parentId) {
                     $linkedWithParent = true;
                 }
                 $lastAddResult = parent::add(array($idColName => $id, $parentColName => $item[$parentColName]));
                 if (!$lastAddResult->isSuccess()) {
                     $success = false;
                     break;
                 }
             }
         }
     }
     if ($success) {
         // link to itself
         $lastAddResult = parent::add(array($idColName => $id, $parentColName => $id));
         if (!$lastAddResult->isSuccess()) {
             $success = false;
         }
     }
     if ($success && !$linkedWithParent) {
         // link to parent
         $lastAddResult = parent::add(array($idColName => $id, $parentColName => $parentId, 'DIRECT' => true));
         if (!$lastAddResult->isSuccess()) {
             $success = false;
         }
     }
     return array('RESULT' => $success, 'LAST_DB_RESULT' => $lastAddResult);
 }