пользовательское свойство от антона
<?
/* init.php */
if(file_exists($_SERVER["DOCUMENT_ROOT"] . '/bitrix/php_interface/include/user_prop_serialize.php'))
{
require_once $_SERVER["DOCUMENT_ROOT"] . '/bitrix/php_interface/include/user_prop_serialize.php';
}
if(file_exists($_SERVER["DOCUMENT_ROOT"] . '/bitrix/php_interface/include/user_prop_serializeNews.php'))
{
require_once $_SERVER["DOCUMENT_ROOT"] . '/bitrix/php_interface/include/user_prop_serializeNews.php';
}
/* /include/user_prop_serialize */
class SerializePortfolioUserProperty
{
function GetUserTypeDescription()
{
return array(
'PROPERTY_TYPE' => 'S',
'USER_TYPE' => __CLASS__,
'DESCRIPTION' => __CLASS__,
'GetPropertyFieldHtml' => array( new SerializePortfolioUserProperty(),'GetPropertyFieldHtml'),
'ConvertToDB' => array( new SerializePortfolioUserProperty(),'ConvertToDB'),
'ConvertFromDB' => array( new SerializePortfolioUserProperty(),'ConvertFromDB'),
);
}
function GetPropertyFieldHtml($arProperty, $arValues, $strHTMLControlName)
{
global $APPLICATION;
CJSCore::Init('jquery');
$APPLICATION->AddHeadScript('/source/js/user_props_SerializePortfolioUserProperty.js');
$APPLICATION->SetAdditionalCSS('/source/css/user_props_SerializePortfolioUserProperty.css');
$html = '';
$html .= $this->__HTMLFieldsType($strHTMLControlName["VALUE"],$arValues["VALUE"]);
$html .= $this->propertyUserText($strHTMLControlName["VALUE"],$arValues["VALUE"]);
$html .= $this->propertyUserTextImage($strHTMLControlName["VALUE"],$arValues["VALUE"]);
$html .= $this->propertyUserTextGallery($strHTMLControlName["VALUE"],$arValues["VALUE"]);
return $html;
}
function propertyUserText($name, $arValues = '')
{
$textArea = '<div class="propertyUserText hidden">';
$textArea .= $this->__RenderTextArea('Текст справа: ',$arValues,$name,'propertyUserText','left:S');
$textArea .= $this->__RenderTextArea('Текст слева: ',$arValues,$name,'propertyUserText','right:S');
$textArea .='</div>';
return $textArea;
}
function propertyUserTextImage($name,$arValues = '')
{
$html = '<div class="propertyUserTextImage hidden">';
$html .= $this->__RenderTextArea('Заголовок: ',$arValues,$name,'propertyUserTextImage','header:S');
$html .= $this->__RenderTextArea('Текст: ',$arValues,$name,'propertyUserTextImage','text:S');
$html .= $this->__RenderFile($arValues,$name,'propertyUserTextImage','image:F');
$refference = array(
"VALUE" => array(
'left',
'right',
'in_image'
),
"NAME" => array(
'Слева: ',
'Права: ',
'На картинки: '
),
);
$html .= $this->__RenderRadio('Как позиционировать текст: ',$arValues,$name,$refference,'propertyUserTextImage','type:R');
$html .= '</div>';
return $html;
}
function propertyUserTextGallery($name,$arValues = '')
{
$html = '<div class="propertyUserTextGallery hidden">';
$html .= $this->__RenderFiles($arValues,$name,'propertyUserTextGallery','images:F');
$html .= $this->__RenderTextArea('Текст: ',$arValues,$name,'propertyUserTextGallery','text:S');
$html .= '</div>';
return $html;
}
function __HTMLFieldsType($key,$arValues = '')
{
$select = '';
$refference= array(
"VALUE" => array(
'propertyUserText',
'propertyUserTextImage',
'propertyUserTextGallery'
),
"NAME" => array(
'Текст',
'Текст + Картинка',
'Галлерея',
),
);
$select .= $this->__RenderSelect('(Выбрать тип)',$arValues,$key,$refference,'type');
$select .= $this->__RenderInput('Сортировка: ','sorting hidden',$arValues,$key,'sort');
return $select;
}
function __RenderFiles($value,$name,$propertyType,$property)
{
if(is_array($value))
{
$curValue = $value['render'][$propertyType][$property];
}
else
$curValue = '';
$curName = $name . '[render]['.$propertyType.']'.'[' .$property .']';
$html = '';
if(is_array($curValue))
{
$count = 0;
foreach($curValue as $k => $arIMG)
{
if(is_numeric($arIMG))
{
$arFile = CFile::GetFileArray($arIMG);
$html .= '
<div class="adm-input-file-new adm-input-cont-bordered" xmlns="http://www.w3.org/1999/html">
<span class="adm-input-file">
<span class="filename">'.$arFile["FILE_NAME"].'</span>
</span>
<input type="hidden" style="" class="adm-input" size="50" name="'.$curName . '['.$count.']" value="'.$arFile["ID"].'">
</span>
</span>
<span class="delete"><label class="">Удалить</label><input type="checkbox" value="del" name="'.$curName . '['.$count.']" /></span>
</div>
';
$count ++;
}
}
}
$html .= '<div class="wrapAdd">
<input class="fileAdd" type="file" data-code="'.$curName.'" name="'.$curName . '[1000]" value=""/><br/><br/><br/>
</div>' ;
return $html;
}
function __RenderFile($value,$name,$propertyType,$property)
{
if(is_array($value))
{
$curValue = $value['render'][$propertyType][$property];
}
else
$curValue = '';
$curName = $name . '[render]['.$propertyType.']'.'[' .$property .']';
$file = '';
if(is_numeric($curValue))
{
$arFile = CFile::GetFileArray($curValue);
$file .= '<div class="adm-input-file-new adm-input-cont-bordered" xmlns="http://www.w3.org/1999/html">
<span class="adm-input-file">
<span>'.$arFile["FILE_NAME"].'</span>
</span>
<input type="hidden" style="" class="adm-input" size="50" name="'.$curName .'" value="'.$arFile["ID"].'">
</span>
</span>
<span class="delete"> Удалить: <input type="checkbox" value="del" name="'.$curName . '" /></span><s<br/>
</div>';
}
else
{
$file .= '<input type="file" name="'.$curName . '" value="'.$curValue.'"/><br/>' ;
}
return $file;
}
function __RenderRadio($label,$value,$name,$refference,$propertyType,$property)
{
if(is_array($value))
$curValue = $value["render"][$propertyType][$property];
else
$curValue = false;
$curName = $name .'[render]['.$propertyType.']['.$property.']';
$radio = ' <p><b>'.$label.'</b></p>';
foreach($refference["VALUE"] as $key => $value)
{
if($curValue == $value)
$radio .= '<p><input checked name="'.$curName.'" type="radio" value="'.$value.'">'.$refference["NAME"][$key].'</p>';
else
$radio .= '<p><input name="'.$curName.'" type="radio" value="'.$value.'">'.$refference["NAME"][$key].'</p>';
}
return $radio;
}
function __RenderTextArea($label,$value,$name,$propertyType,$property,$row=10,$cols=45)
{
if(is_array($value))
{
$curValue = $value['render'][$propertyType][$property];
}
else
$curValue = '';
$curName = '[render]['.$propertyType.']'.'[' .$property .']';
$textArea = '<p><b>'.$label.'</b></p>
<p><textarea rows="'.$row.'" cols="'.$cols.'" name="'.$name . $curName.'">'.$curValue.'</textarea></p>';
return $textArea;
}
function __RenderSelect($label,$value,$name,$refference,$propertyType)
{
if(is_array($value))
$curValue = $value[$propertyType];
else
$curValue = false;
$curName = $name .'['.$propertyType.']';
$select ='
<p>
<select class="type" size="1" name="'.$curName.'">';
$select .= '<option value="">'.$label.'</option>';
foreach($refference["VALUE"] as $key => $value)
{
if($curValue == $value)
$select .= '<option selected value="'.$value.'">'.$refference["NAME"][$key].'</option>';
else
$select .= '<option value="'.$value.'">'.$refference["NAME"][$key].'</option>';
}
$select .= '</select></p>';
return $select;
}
function __RenderInput($label,$class,$value,$name,$propertyType)
{
if(is_array($value))
$curValue = $value[$propertyType];
else
$curValue = '';
$curName = $name .'['.$propertyType.']';
$input = '';
$input .= '<p class="'.$class.'"><b>'.$label.'</b><Br>
<input type="text" name="'.$curName.'" value="'.$curValue.'"><Br>
</p>';
return $input;
}
function ConvertToDB($arProperty, $value)
{
$mainType = $value["VALUE"]["type"];
foreach($value["VALUE"]["render"] as $type => &$arProperty)
{
if($type !== $mainType)
{
unset($value["VALUE"]["render"][$type]);
continue;
}
foreach($arProperty as $k => &$v)
{
list($key,$typeAction) = explode(":",$k);
if($typeAction == "F" )
{
if(is_array($v) && !isset($v['name']) )
{
foreach($v as $key => &$arImage)
{
$arImage = $this->ConvertImage($arImage);
}
}
else
{
$v = $this->ConvertImage($v);
}
}
}
}
if(!empty($mainType))
{
$value["VALUE"] = serialize($value["VALUE"]);;
return $value;
}
}
function ConvertImage($v)
{
if(!is_numeric($v) && is_array($v))
{
$v = CFile::SaveFile($v, "property");
}elseif(is_numeric($v))
{
$v = intval($v);
}else
{
$v = false;
}
return $v;
}
public function ConvertFromDB($arProperty, $value)
{
$value["VALUE"] = unserialize($value["VALUE"]);
return $value;
}
}
AddEventHandler("iblock", "OnIBlockPropertyBuildList", Array(new SerializePortfolioUserProperty(), "GetUserTypeDescription"));
?>
<?
/* /include/user_prop_serializeNews.php */
class SerializeNewsUserProperty
{
function GetUserTypeDescription()
{
return array(
'PROPERTY_TYPE' => 'S',
'USER_TYPE' => __CLASS__,
'DESCRIPTION' => __CLASS__,
'GetPropertyFieldHtml' => array( new SerializeNewsUserProperty(),'GetPropertyFieldHtml'),
'ConvertToDB' => array( new SerializeNewsUserProperty(),'ConvertToDB'),
'ConvertFromDB' => array( new SerializeNewsUserProperty(),'ConvertFromDB'),
);
}
function GetPropertyFieldHtml($arProperty, $arValues, $strHTMLControlName)
{
global $APPLICATION;
CJSCore::Init('jquery');
$APPLICATION->AddHeadScript('/source/tinymce/tinymce.min.js');
$APPLICATION->AddHeadScript('/source/js/user_props_SerializeNewsUserProperty.js');
$APPLICATION->SetAdditionalCSS('/source/css/user_props_SerializeNewsUserProperty.css');
$html = '';
$html .= $this->propertyUserType($strHTMLControlName["VALUE"],$arValues["VALUE"]);
$html .= $this->propertyUserTextSlider($strHTMLControlName["VALUE"],$arValues["VALUE"]);
$html .= $this->propertyUserText($strHTMLControlName["VALUE"],$arValues["VALUE"]);
$html .= $this->propertyUserTextImage($strHTMLControlName["VALUE"],$arValues["VALUE"]);
return $html;
}
function propertyUserTextImage($name,$arValues = '')
{
$html = '<div class="propertyUserTextImage hidden">';
if(is_array($arValues["VALUE"]))
$curValueText = $arValues["VALUE"]["propertyUserTextImage"]["Text:S"];
else
$curValueText = '';
$html .= $this->__RenderTextArea('Текст : ',$curValueText, $name.'[VALUE][propertyUserTextImage][Text:S]','');
$html .= $this->__RenderFile($arValues,$name,'propertyUserTextImage','Text:F');
$html .= '</div>';
return $html;
}
function __RenderFile($value,$name,$propertyType,$property)
{
if(is_array($value))
{
$curValue = $value['VALUE'][$propertyType][$property];
}
else
$curValue = '';
$curName = $name . '[VALUE]['.$propertyType.']'.'[' .$property .']';
$file = '';
if(is_numeric($curValue))
{
$arFile = CFile::GetFileArray($curValue);
$file .= '<div class="adm-input-file-new adm-input-cont-bordered" xmlns="http://www.w3.org/1999/html">
<span class="adm-input-file">
<span>'.$arFile["FILE_NAME"].'</span>
</span>
<input type="hidden" style="" class="adm-input" size="50" name="'.$curName .'" value="'.$arFile["ID"].'">
</span>
</span>
<span class="delete"> Удалить: <input type="checkbox" value="del" name="'.$curName . '" /></span><s<br/>
</div>';
}
else
{
$file .= '<input type="file" name="'.$curName . '" value="'.$curValue.'"/><br/>' ;
}
return $file;
}
function propertyUserText($name,$arValues = '')
{
$html = '<div class="propertyUserText hidden">';
if(is_array($arValues["VALUE"]))
$curValue = $arValues["VALUE"]["propertyUserText"]["Text:S"];
else
$curValue = '';
$html .= $this->__RenderTextArea('Текст : ',$curValue, $name.'[VALUE][propertyUserText][Text:S]','tinymce');
$html .= '</div>';
return $html;
}
function propertyUserTextSlider($name,$arValues = '')
{
$html ='<div class="propertyUserTextSlider hidden">';
$html .= $this->__RenderFiles($arValues,$name,'propertyUserTextSlider','Images:F');
$html .= '</div>';
return $html;
}
function propertyUserType($name,$arValues = '')
{
$refference= array(
"VALUE" => array(
'propertyUserText',
'propertyUserTextImage',
'propertyUserTextSlider'
),
"NAME" => array(
'Текст',
'Текст + Картинка',
'Слайдер',
),
);
$select = '';
$select .= $this->__RenderSelect('(Выбрать тип)',$arValues,$name,$refference,'type');
$select .= $this->__RenderInput('Сортировка: ','sorting hidden',$arValues,$name,'sort');
return $select;
}
function __RenderInput($label,$class,$value,$name,$propertyType)
{
if(is_array($value))
$curValue = $value[$propertyType];
else
$curValue = '';
$curName = $name .'['.$propertyType.']';
$input = '';
$input .= '<p class="'.$class.'"><b>'.$label.'</b><Br>
<input type="text" name="'.$curName.'" value="'.$curValue.'"><Br>
</p>';
return $input;
}
function __RenderFiles($value,$name,$propertyType,$property)
{
if(is_array($value))
{
$curValue = $value['VALUE'][$propertyType][$property];
}
else
$curValue = '';
$curName = $name . '[VALUE]['.$propertyType.']'.'[' .$property .']';
$html = '';
if(count($curValue) == 0 || is_string($curValue))
{
$html .= '';
}
$count = 0;
foreach($curValue as $key => $imgArray)
{
$arFile = CFile::GetFileArray($imgArray);
if(!is_numeric($arFile["ID"]))
continue;
$html .= '<div class="adm-input-file-new adm-input-cont-bordered" xmlns="http://www.w3.org/1999/html">
<span class="adm-input-file">';
$html .= '<span class="filename">'.$arFile["FILE_NAME"].'</span>
</span>
<input type="hidden" style="" class="adm-input" size="50" name="'.$curName . '['.$count.']" value="'.$arFile["ID"].'">
</span>';
$html .= $this->__RenderHidden($curName . '['.$count.']',$arFile["ID"]);
$html .= $this->__RenderCheckbox($curName . '['.$count.']','del','Удалить:');
$html .= $this->__RenderTextArea('Текст для слайда: ',$value["VALUE"]["propertyUserTextSlider"]["Text:S"][$key],$name.'[VALUE][propertyUserTextSlider][Text:S]'.'['.$count.']');
$html .= '</span></div>';
$count++;
}
$f = true;
for($i = 1000,$z=1010;$i <=$z; $i++)
{
if(!$f)
{
$class = 'blockAdd';
}
$html .= '<div class="'.$class.'">';
$html .= ' <div class="wrapAdd">
<input class="fileAdd" type="file" data-code="'.$curName.'" name="'.$curName . '['.$i.']" value=""/>
<br/><br/><br/>
</div>';
$html .= $this->__RenderTextArea('Текст для слайда: ','',$name.'[VALUE][propertyUserTextSlider][Text:S]'.'['.$i.']');
$html .= '</div>';
unset($f);
}
return $html;
}
function __RenderTextArea($label,$value,$name,$class='',$row=10,$cols=45)
{
$textArea = '<p><b>'.$label.'</b></p>
<p><textarea class="'.$class.'" rows="'.$row.'" cols="'.$cols.'" name="'.$name .'">'.$value.'</textarea></p>';
return $textArea;
}
function __RenderCheckbox($name,$arValues,$label)
{
$checkbox = '';
$checkbox .= '<span class="delete"><label class="">'.$label.'</label><input type="checkbox" value="'.$arValues.'" name="'.$name.'" /></span>';
return $checkbox;
}
function __RenderHidden($name,$arValues)
{
$input = '';
$input .= '<input type="hidden" name="'.$name . '" value="'.$arValues.'"/>';
return $input;
}
function __RenderSelect($label,$value,$name,$refference,$propertyType)
{
if(is_array($value))
$curValue = $value[$propertyType];
else
$curValue = false;
$curName = $name .'['.$propertyType.']';
$select ='
<p>
<select class="type" size="1" name="'.$curName.'">';
$select .= '<option value="">'.$label.'</option>';
foreach($refference["VALUE"] as $key => $value)
{
if($curValue == $value)
$select .= '<option selected value="'.$value.'">'.$refference["NAME"][$key].'</option>';
else
$select .= '<option value="'.$value.'">'.$refference["NAME"][$key].'</option>';
}
$select .= '</select></p>';
return $select;
}
function ConvertToDB($arProperty, $value)
{
foreach($value["VALUE"]["VALUE"] as $type => $v)
{
if($type != $value["VALUE"]["type"])
unset($value["VALUE"]["VALUE"][$type]);
}
// die();
if(!empty($value["VALUE"]['type']))
{
$mainType = $value["VALUE"]["type"];
foreach($value["VALUE"]["VALUE"][$mainType] as $typeProperty => &$propertyValue)
{
list($subCode,$type) = explode(":",$typeProperty);
if($type == "F" )
{
if(is_array($propertyValue) && !isset($propertyValue['name']) )
{
foreach($propertyValue as $key => &$arImage)
{
$ResultImage = $this->ConvertImage($arImage);
if($ResultImage)
{
$arImage = $ResultImage;
}
else{
unset($propertyValue[$key]);
}
}
}
else
{
$propertyValue = $this->ConvertImage($propertyValue);
}
}
elseif($type == "S" )
{
if(is_array($propertyValue))
{
foreach($propertyValue as $key => &$vs)
{
if(empty($vs))
unset($propertyValue[$key]);
}
}
}
}
$value["VALUE"] = serialize($value["VALUE"]);;
return $value;
}
}
function ConvertImage($v)
{
if(!is_numeric($v) && is_array($v) && !($v["error"] > 0))
{
$v = CFile::SaveFile($v, "property");
}elseif(is_numeric($v))
{
$v = intval($v);
}else
{
$v = false;
}
return $v;
}
public function ConvertFromDB($arProperty, $value)
{
$value["VALUE"] = unserialize($value["VALUE"]);
return $value;
}
}
AddEventHandler("iblock", "OnIBlockPropertyBuildList", Array(new SerializeNewsUserProperty(), "GetUserTypeDescription"));
?>
<script>
/* user_props_SerializeNewsUserProperty.js */
$(document).ready(function(){
$(".type").live('change',function(){
className = $(this).val();
rootRow = $(this).closest('tr');
rootRow.find('.hidden').hide();
console.log('.' + className);
if(className.length > 0)
{
rootRow.find('.' + className).show();
rootRow.find('.sorting').show();
}
});
$('.fileAdd').live('change',function(){
$('.blockAdd').eq(1).removeClass('blockAdd');
});
$(".type").each(function(){
$(this).trigger('change');
});
tinyMCE.init({
selector: "textarea.tinymce",
mode:"textareas",
});
});
/* user_props_SerializeUserProperty.js */
$(document).ready(function(){
var config = {
template : '<input type="file" data-code="#CODE#" value="" name="#NAME#" class="fileAdd adm-designed-file"><br/>',
}
$(".type").live('change',function(){
className = $(this).val();
rootRow = $(this).closest('tr');
rootRow.find('.hidden').hide();
rootRow.find('.' + className).show();
rootRow.find('.sorting').show();
});
$('.fileAdd').live('change',function(){
count = $('.propertyUserTextGallery input').length;
name = $(this).data('code') + "["+count+"]";
$(this).closest('.wrapAdd').append(config.template.replace("#NAME#",name).replace("#CODE#",$(this).data('code')));
});
$(".type").each(function(){
$(this).trigger('change');
});
});
</script>
<style>
/* user_props_SerializeUserProperty.css */
.propertyUserText, .propertyUserTextImage, .propertyUserTextGallery, .sorting, .hidden{
display: none;
}
.propertyUserTextImage{
margin-top:40px;
}
.property{
display: block;
float: inherit;
padding-top: 25px;
width: 100px;
}
.delete {
line-height: 31px;
padding-left:6px;
}
.wrapAdd input[type="file"] {
/* font-size: 40px;
opacity: 0;
position: absolute;
right: 0;
top: 0;
outline: medium none;*/
}
/* user_props_SerializeNewsUserProperty.css */
.blockAdd,.propertyUserSlider,.propertyUserTextSlider, .propertyUserText{
display: none;
}
.propertyUserTextSlider{
display: none;
}
/*
.propertyUserTextImage{
margin-top:40px;
}
.property{
display: block;
float: inherit;
padding-top: 25px;
width: 100px;
}
.delete {
line-height: 31px;
padding-left:6px;
}
.wrapAdd input[type="file"] {
font-size: 40px;
opacity: 0;
position: absolute;
right: 0;
top: 0;
outline: medium none;
}*/
</style>