Esempio n. 1
2
 protected static function checkFieldExists($entityId, $fieldName)
 {
     $fld = UserFieldTable::getList(array('filter' => array('=ENTITY_ID' => $entityId, '=FIELD_NAME' => $fieldName), 'limit' => 1))->fetch();
     return is_array($fld) && (string) $fld['FIELD_NAME'] != '';
 }
Esempio n. 2
0
	function Update($entity_id, $ID, $arFields, $user_id = false)
	{
		global $DB;

		$result = false;

		$entity_id = preg_replace("/[^0-9A-Z_]+/", "", $entity_id);

		$arUpdate = array();
		$arBinds = array();
		$arInsert = array();
		$arInsertType = array();
		$arDelete = array();
		$arUserFields = $this->GetUserFields($entity_id, $ID, false, $user_id);
		foreach($arUserFields as $FIELD_NAME=>$arUserField)
		{
			if(array_key_exists($FIELD_NAME, $arFields))
			{
				$arUserField['VALUE_ID'] = $ID;
				if($arUserField["MULTIPLE"] == "N")
				{
					if(is_callable(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesave")))
						$arFields[$FIELD_NAME] = call_user_func_array(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesave"), array($arUserField, $arFields[$FIELD_NAME], $user_id));

					if(strlen($arFields[$FIELD_NAME])>0)
						$arUpdate[$FIELD_NAME] = $arFields[$FIELD_NAME];
					else
						$arUpdate[$FIELD_NAME] = false;
				}
				elseif(is_array($arFields[$FIELD_NAME]))
				{
					$arInsert[$arUserField["ID"]] = array();
					$arInsertType[$arUserField["ID"]] = $arUserField["USER_TYPE"];

					if(is_callable(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesaveall")))
						$arInsert[$arUserField["ID"]] = call_user_func_array(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesaveall"), array($arUserField, $arFields[$FIELD_NAME], $user_id));
					else
					{
						foreach($arFields[$FIELD_NAME] as $value)
						{
							if(is_callable(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesave")))
								$value = call_user_func_array(array($arUserField["USER_TYPE"]["CLASS_NAME"], "onbeforesave"), array($arUserField, $value, $user_id));

							if(strlen($value)>0)
							{
								switch($arInsertType[$arUserField["ID"]]["BASE_TYPE"])
								{
									case "int":
									case "file":
									case "enum":
										$value = intval($value);
										break;
									case "double":
										$value = doubleval($value);
										break;
									case "datetime":
										//TODO: convert to valid site date/time
										//$value = $DB->CharToDateFunction($value);
										break;
									default:
										// For SQL will follow
										$value = substr($value, 0, 2000);
								}
								$arInsert[$arUserField["ID"]][] = $value;
							}
						}
					}

					if ($arUserField['USER_TYPE_ID'] == 'datetime')
					{
						$serialized = \Bitrix\Main\UserFieldTable::serializeMultipleDatetime($arInsert[$arUserField["ID"]]);
					}
					elseif ($arUserField['USER_TYPE_ID'] == 'date')
					{
						$serialized = \Bitrix\Main\UserFieldTable::serializeMultipleDate($arInsert[$arUserField["ID"]]);
					}
					else
					{
						$serialized = serialize($arInsert[$arUserField["ID"]]);
					}

					$arBinds[$FIELD_NAME] = $arUpdate[$FIELD_NAME] = $serialized;

					$arDelete[$arUserField["ID"]] = true;
				}
			}
		}

		$lower_entity_id = strtolower($entity_id);

		if(!empty($arUpdate))
			$strUpdate = $DB->PrepareUpdate("b_uts_".$lower_entity_id, $arUpdate);
		else
			return $result;

		if(strlen($strUpdate) > 0)
		{
			$result = true;
			$rs = $DB->QueryBind("UPDATE b_uts_".$lower_entity_id." SET ".$strUpdate." WHERE VALUE_ID = ".intval($ID), $arBinds);
			$rows = $rs->AffectedRowsCount();
		}
		else
		{
			$rows = 0;
		}

		if(intval($rows)<=0)
		{
			$rs = $DB->Query("SELECT 'x' FROM b_uts_".$lower_entity_id." WHERE VALUE_ID = ".intval($ID), false, "FILE: ".__FILE__."<br>LINE: ".__LINE__);
			if($rs->Fetch())
				$rows = 1;
		}

		if($rows <= 0)
		{
			$arUpdate["ID"] = $arUpdate["VALUE_ID"] = $ID;
			$DB->Add("b_uts_".$lower_entity_id, $arUpdate, array_keys($arBinds));
		}
		else
		{
			foreach($arDelete as $key=>$value)
			{
				$DB->Query("DELETE from b_utm_".$lower_entity_id." WHERE FIELD_ID = ".intval($key)." AND VALUE_ID = ".intval($ID), false, "FILE: ".__FILE__."<br>LINE: ".__LINE__);
			}
		}

		foreach($arInsert as $FieldId=>$arField)
		{
			switch($arInsertType[$FieldId]["BASE_TYPE"])
			{
				case "int":
				case "file":
				case "enum":
					$COLUMN = "VALUE_INT";
					break;
				case "double":
					$COLUMN = "VALUE_DOUBLE";
					break;
				case "datetime":
					$COLUMN = "VALUE_DATE";
					break;
				default:
					$COLUMN = "VALUE";
			}
			foreach($arField as $value)
			{
				if ($value instanceof \Bitrix\Main\Type\Date)
				{
					// little hack to avoid timezone vs 00:00:00 ambiguity. for utm only
					$value = new \Bitrix\Main\Type\DateTime($value->format('Y-m-d H:i:s'), 'Y-m-d H:i:s');
				}

				switch($arInsertType[$FieldId]["BASE_TYPE"])
				{
					case "int":
					case "file":
					case "enum":
						break;
					case "double":
						break;
					case "datetime":
						$value = $DB->CharToDateFunction($value);
						break;
					default:
						$value = "'".$DB->ForSql($value)."'";
				}
				$DB->Query("INSERT INTO b_utm_".$lower_entity_id." (VALUE_ID, FIELD_ID, ".$COLUMN.")
					VALUES (".intval($ID).", '".$FieldId."', ".$value.")", false, "FILE: ".__FILE__."<br>LINE: ".__LINE__);
			}
		}

		return $result;
	}
Esempio n. 3
0
 public function postInitialize()
 {
     // basic properties
     $classPath = explode('\\', ltrim($this->className, '\\'));
     $this->name = substr(end($classPath), 0, -5);
     // default db table name
     if (is_null($this->dbTableName)) {
         $_classPath = array_slice($classPath, 0, -1);
         $this->dbTableName = 'b_';
         foreach ($_classPath as $i => $_pathElem) {
             if ($i == 0 && $_pathElem == 'Bitrix') {
                 // skip bitrix namespace
                 continue;
             }
             if ($i == 1 && $_pathElem == 'Main') {
                 // also skip Main module
                 continue;
             }
             $this->dbTableName .= strtolower($_pathElem) . '_';
         }
         // add class
         if ($this->name !== end($_classPath)) {
             $this->dbTableName .= Base::camel2snake($this->name);
         } else {
             $this->dbTableName = substr($this->dbTableName, 0, -1);
         }
     }
     $this->primary = array();
     $this->references = array();
     // attributes
     foreach ($this->fieldsMap as $fieldName => &$fieldInfo) {
         $this->addField($fieldInfo, $fieldName);
     }
     if (!empty($this->fieldsMap) && empty($this->primary)) {
         throw new Main\SystemException(sprintf('Primary not found for %s Entity', $this->name));
     }
     // attach userfields
     if (!empty($this->uf_id)) {
         Main\UserFieldTable::attachFields($this, $this->uf_id);
     }
 }
Esempio n. 4
0
 protected static function compileUtmEntity(Entity\Base $hlentity, $userfield)
 {
     global $USER_FIELD_MANAGER;
     // build utm entity
     /** @var DataManager $hlDataClass */
     $hlDataClass = $hlentity->getDataClass();
     $hlblock = $hlDataClass::getHighloadBlock();
     $utmClassName = static::getUtmEntityClassName($hlentity, $userfield);
     $utmTableName = static::getMultipleValueTableName($hlblock, $userfield);
     // main fields
     $utmValueField = $USER_FIELD_MANAGER->getEntityField($userfield, 'VALUE');
     $utmEntityFields = array(new Entity\IntegerField('ID'), $utmValueField);
     // references
     $references = $USER_FIELD_MANAGER->getEntityReferences($userfield, $utmValueField);
     foreach ($references as $reference) {
         $utmEntityFields[] = $reference;
     }
     // create entity
     $utmEntity = Entity\Base::compileEntity($utmClassName, $utmEntityFields, array('table_name' => $utmTableName, 'namespace' => $hlentity->getNamespace()));
     // add original entity reference
     $referenceField = new Entity\ReferenceField('OBJECT', $hlentity, array('=this.ID' => 'ref.ID'));
     $utmEntity->addField($referenceField);
     // add short alias for back-reference
     $aliasField = new Entity\ExpressionField($userfield['FIELD_NAME'] . '_SINGLE', '%s', $utmEntity->getFullName() . ':' . 'OBJECT.VALUE', array('data_type' => get_class($utmEntity->getField('VALUE'))));
     $hlentity->addField($aliasField);
     // add aliases to references
     /*foreach ($references as $reference)
     		{
     			// todo after #44924 is resolved
     			// actually no. to make it work expression should support linking to references
     		}*/
     // add seriazed cache-field
     $cacheField = new Entity\TextField($userfield['FIELD_NAME']);
     Main\UserFieldTable::setMultipleFieldSerialization($cacheField, $userfield);
     $hlentity->addField($cacheField);
     return $utmEntity;
 }
Esempio n. 5
-1
 /**
  * Returns list of user fields in ORM way
  * 
  * @param mixed[] A standard ORM getList() first agrument
  * 
  * @return DBResult
  */
 public static function getFieldList($parameters = array())
 {
     if (!is_array($parameters)) {
         $parameters = array();
     }
     $parameters['filter']['=ENTITY_ID'] = static::getTargetEntityId();
     return UserFieldTable::getList($parameters);
 }