Your IP : 3.133.131.225
<?php
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
{
die();
}
\CBitrixComponent::includeComponentClass('bitrix:landing.base');
class LandingBaseFormComponent extends LandingBaseComponent
{
/**
* Current element id.
* @var int
*/
protected $id = 0;
/**
* Class of current element.
* @var string
*/
protected $class = null;
/**
* Page after success save.
* @var string
*/
protected $successSavePage = '';
/**
* POST key.
* @var string
*/
protected $postCode = 'fields';
/**
* Local version of table map with available fields for change.
* @return array
*/
protected function getMap()
{
return array();
}
/**
* Get some var from request.
* @param string $var Code of var.
* @param bool $strict Strict check of var.
* @return mixed
*/
protected function request($var, $strict = false)
{
static $request = null;
if ($request === null)
{
$context = \Bitrix\Main\Application::getInstance()->getContext();
$request = $context->getRequest();
if (isset($request[$this->postCode]))
{
$request = $request[$this->postCode];
}
}
if ($strict)
{
if (array_key_exists($var, $request))
{
return $request[$var];
}
return false;
}
return isset($request[$var]) ? $request[$var] : '';
}
/**
* Current form is saving now.
* @return boolean
*/
protected function isSaving()
{
static $result = null;
if ($result === null)
{
$result = false;
$context = \Bitrix\Main\Application::getInstance()->getContext();
$server = $context->getServer();
if (
$server->getRequestMethod() == 'POST' &&
$this->request('SAVE_FORM') == 'Y'
)
{
$result = true;
}
}
return $result;
}
/**
* Fill or not row data from Request.
* @return boolean
*/
protected function fillFromRequest()
{
return $this->isSaving();
}
/**
* Allowed or not additional fields for this form.
* @return boolean
*/
protected function additionalFieldsAllowed()
{
return false;
}
/**
* Get additional fields, which must be un-wafed.
* @return array
*/
protected function getAdditionalFieldsRaw()
{
return array('HEADBLOCK_CODE', 'HEADBLOCK_CSS_CODE');
}
/**
* Get additional fields values.
* @return array
*/
protected function getAdditionalFieldsValue()
{
$additionalFieldsParent = $this->request('ADDITIONAL_FIELDS_PARENT');
$additionalFields = $this->request('ADDITIONAL_FIELDS');
// bugfix for security waf
$context = \Bitrix\Main\Application::getInstance()->getContext();
$request = $context->getRequest();
$postList = $request->getPostList()->getRaw($this->postCode);
if (isset($postList['ADDITIONAL_FIELDS']))
{
$postList = $postList['ADDITIONAL_FIELDS'];
foreach ($this->getAdditionalFieldsRaw() as $code)
{
if (isset($postList[$code]))
{
$additionalFields[$code] = $postList[$code];
}
}
}
// detect groups wich different
$diffGroups = array();
if (is_array($additionalFields))
{
foreach ($additionalFields as $key => $value)
{
$group = substr($key, 0, strpos($key, '_'));
if (
!in_array($group, $diffGroups) &&
isset($additionalFieldsParent[$key]) &&
trim($additionalFieldsParent[$key]) != trim($value)
)
{
$diffGroups[] = $group;
}
}
}
// delete from child form duplicate values
if (is_array($additionalFieldsParent))
{
foreach ($additionalFieldsParent as $key => $value)
{
$group = substr($key, 0, strpos($key, '_'));
if (
!in_array($group, $diffGroups) &&
isset($additionalFields[$key]) &&
trim($additionalFields[$key]) == trim($value)
)
{
$additionalFields[$key] = '';
}
}
}
return $additionalFields;
}
/**
* Fill default fields from exist another row.
* @return int
*/
protected function getCopyId()
{
return (int)$this->request('copy');
}
/**
* Get hooks for current entity.
* @param string $class Get hooks from this class.
* @param int $id Get hooks from this entity id.
* @return array
*/
protected function getHooks($class = false, $id = false)
{
$hooks = array();
if ($id === false)
{
$id = $this->id;
}
if ($class === false)
{
$class = $this->class;
}
$classFull = $this->getValidClass($class);
if (
$classFull &&
method_exists($classFull, 'getHooks')
)
{
\Bitrix\Landing\Hook::setEditMode();
$hooks = $classFull::getHooks($id);
$requestAdditional = $this->getAdditionalFieldsValue();
foreach ($hooks as $hook)
{
foreach ($hook->getPageFields() as $field)
{
if (isset($requestAdditional[$field->getCode()]))
{
$field->setValue(
$requestAdditional[$field->getCode()]
);
}
}
}
}
return $hooks;
}
/**
* Get current item from table (or request) with map description.
* @return array
*/
protected function getRow()
{
$item = array();
$classFull = $this->getValidClass($this->class);
if ($classFull)
{
if ($this->id > 0 || $this->getCopyId())
{
$row = $this->getItems($this->class, array(
'filter' => array(
'ID' => $this->id > 0
? $this->id
: $this->getCopyId()
)
)
);
if ($row)
{
$row = array_shift($row);
}
}
// get map of class
$localMap = $this->getMap();
$fillFromRequest = $this->fillFromRequest();
foreach ($classFull::getMap() as $code => $field)
{
$defaultValue = method_exists($field, 'getDefaultValue')
? $field->getDefaultValue()
: '';
$item[$code] = array(
'TITLE' => $field->getTitle(),
'READONLY' => !in_array($code, $localMap),
'~CURRENT' => $fillFromRequest
? $this->request($code)
: (isset($row[$code]) ? $row[$code] : $defaultValue)
);
$item[$code]['CURRENT'] = \htmlspecialcharsbx($item[$code]['~CURRENT']);
}
}
return $item;
}
/**
* Save current item.
* @return boolean
*/
protected function updateRow()
{
$fields = array();
$classFull = $this->getValidClass($this->class);
// check common errors
if (!$classFull)
{
return false;
}
if (!check_bitrix_sessid())
{
$this->addError('LANDING_ERROR_SESS_EXPIRED');
return false;
}
// collect fields
foreach ($this->getRow() as $code => $field)
{
if (!$field['READONLY'])
{
$fields[$code] = $field['~CURRENT'];
}
}
// add/update
if (!empty($fields))
{
if ($this->additionalFieldsAllowed())
{
$fields['ADDITIONAL_FIELDS'] = $this->getAdditionalFieldsValue();
}
if ($this->id > 0)
{
$res = $classFull::update($this->id, $fields);
}
else
{
$res = $classFull::add($fields);
}
if ($res->isSuccess())
{
$this->id = $res->getId();
return true;
}
else
{
$this->addErrorFromResult($res);
}
}
return false;
}
/**
* Delete current item.
* @return boolean
*/
protected function deleteRow()
{
$classFull = $this->getValidClass($this->class);
// check common errors
if (!$classFull)
{
return false;
}
if (!check_bitrix_sessid())
{
$this->addError('LANDING_ERROR_SESS_EXPIRED');
return false;
}
// delete
$res = $classFull::delete($this->id);
if ($res->isSuccess())
{
return true;
}
else
{
$this->addErrorFromResult($res);
return false;
}
}
/**
* Base executable method.
* @return void
*/
public function executeComponent()
{
if ($this->init())
{
$this->arParams['SUCCESS_SAVE'] = false;
// add / update
if (
$this->isSaving() &&
$this->updateRow()
)
{
$this->arParams['SUCCESS_SAVE'] = true;
}
// delete
// tmp disabled, #107130
/*if (
$this->request('delete') == 'Y' &&
$this->deleteRow()
)
{
\localRedirect($this->successSavePage);
}*/
}
parent::executeComponent();
}
}