Skip to content

Commit

Permalink
Подгружаются/выводятся все свойства от всех выбранных категорий, close
Browse files Browse the repository at this point in the history
…#338

+ мелочи по шаблону
  • Loading branch information
fuzegit committed Oct 25, 2020
1 parent 9045183 commit 8b7eca9
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 266 deletions.
16 changes: 0 additions & 16 deletions system/controllers/admin/actions/content_items_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ public function run($ctype_id){
'caption', 'html', 'string', 'text'
];

// Добавляем поле мультикатегорий
// @todo убрать, как это поле будет добавлено в форму
if($form->isFieldsetExists('multi_cats')){

$form->addField('multi_cats',
new fieldList('add_cats', [
'is_chosen_multiple' => true,
'generator' => function($item)use($ctype){
return $this->controller_content->getFormCategories($ctype['name']);
}
])
);

$form->setFieldsetAttribute('multi_cats', 'is_empty', null);
}

$structure = $form->getStructure();

$form_select->addFieldset(LANG_BASIC_OPTIONS, 'default_fields');
Expand Down
27 changes: 2 additions & 25 deletions system/controllers/content/actions/item_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,9 @@ public function run(){

if ($is_submitted){

// Добавляем поля свойств для валидации
if ($ctype['props']){
$props_cat_id = $this->request->get('category_id', 0);
if ($props_cat_id){
$item_props = $this->model->getContentProps($ctype['name'], $props_cat_id);
$item_props_fields = $this->getPropsFields($item_props);
foreach($item_props_fields as $field){
$form->addField('props', $field);
}
}
$form = $this->addFormPropsFields($form, $ctype, [], $is_submitted);
}

// Парсим форму и получаем поля записи
Expand Down Expand Up @@ -227,21 +221,6 @@ public function run(){
list($item, $errors) = cmsEventsManager::hook('content_validate', array($item, $errors), null, $this->request);
list($item, $errors, $ctype, $fields) = cmsEventsManager::hook("content_{$ctype['name']}_validate", array($item, $errors, $ctype, $fields), null, $this->request);

// несколько категорий
if (!empty($ctype['options']['is_cats_multi'])){
$add_cats = $this->request->get('add_cats', array());
if (is_array($add_cats)){
foreach($add_cats as $index=>$cat_id){
if (!is_numeric($cat_id) || !$cat_id){
unset($add_cats[$index]);
}
}
if ($add_cats){
$item['add_cats'] = $add_cats;
}
}
}

if (!$errors){

if($is_draf_submitted){
Expand Down Expand Up @@ -355,9 +334,7 @@ public function run(){
'button_save_text' => (($is_premoderation && !$is_moderator) ? LANG_MODERATION_SEND : LANG_SAVE),
'button_draft_text' => LANG_CONTENT_SAVE_DRAFT,
'hide_draft_btn' => !empty($ctype['options']['disable_drafts']),
'is_multi_cats' => !empty($ctype['options']['is_cats_multi']),
'is_load_props' => !isset($errors),
'add_cats' => isset($add_cats) ? $add_cats : array(),
'errors' => isset($errors) ? $errors : false
));

Expand Down
49 changes: 14 additions & 35 deletions system/controllers/content/actions/item_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public function run(){
list($ctype, $item) = cmsEventsManager::hook('content_edit', array($ctype, $item));
list($form, $item) = cmsEventsManager::hook("content_{$ctype['name']}_form", array($form, $item));

// Категории записи
$item['add_cats'] = $item_cats = $this->model->getContentItemCategories($ctype['name'], $id);
if ($item['add_cats']){
// Отдельно дополнительные категории
foreach($item['add_cats'] as $index => $cat_id){
if ($cat_id == $item['category_id']) { unset($item['add_cats'][$index]); break; }
}
}

// Форма отправлена?
$is_submitted = $this->request->has('submit') || $this->request->has('to_draft');

Expand All @@ -110,20 +119,14 @@ public function run(){

if ($ctype['props']){

$category_id = !$is_submitted ? $item['category_id'] :
(($this->request->has('category_id') && $ctype['options']['is_cats_change']) ?
$this->request->get('category_id', 0) :
$item['category_id']);
if(!$item_cats){
$item_cats = [$item['category_id']];
}

$item_props = $this->model->getContentProps($ctype['name'], $category_id);
$item_props_fields = $this->getPropsFields($item_props);
$form = $this->addFormPropsFields($form, $ctype, $item_cats, $is_submitted);

$item['props'] = $this->model->localizedOff()->getPropsValues($ctype['name'], $id);
$this->model->localizedRestore();

foreach($item_props_fields as $field){
$form->addField('props', $field);
}
}

$is_pub_control = cmsUser::isAllowed($ctype['name'], 'pub_on');
Expand All @@ -136,14 +139,6 @@ public function run(){
$item['pub_days'] = 0;
}

$add_cats = $this->model->getContentItemCategories($ctype['name'], $id);

if ($add_cats){
foreach($add_cats as $index => $cat_id){
if ($cat_id == $item['category_id']) { unset($add_cats[$index]); break; }
}
}

$show_save_button = ($is_owner || (!$is_premoderation && $item['is_approved']));

if ($is_submitted){
Expand All @@ -152,7 +147,7 @@ public function run(){
$item = array_merge($item, $form->parse($this->request, $is_submitted, $item));

// Проверям правильность заполнения
$errors = $form->validate($this, $item);
$errors = $form->validate($this, $item);

list($item, $errors) = cmsEventsManager::hook('content_validate', array($item, $errors), null, $this->request);
list($item, $errors, $ctype, $fields) = cmsEventsManager::hook("content_{$ctype['name']}_validate", array($item, $errors, $ctype, $fields), null, $this->request);
Expand Down Expand Up @@ -212,20 +207,6 @@ public function run(){
}
}

if (!empty($ctype['options']['is_cats_multi'])){
$add_cats = $this->request->get('add_cats', array());
if (is_array($add_cats)){
foreach($add_cats as $index=>$cat_id){
if (!is_numeric($cat_id) || !$cat_id){
unset($add_cats[$index]);
}
}
if ($add_cats){
$item['add_cats'] = $add_cats;
}
}
}

//
// Сохраняем запись и редиректим на ее просмотр
//
Expand Down Expand Up @@ -327,9 +308,7 @@ public function run(){
'button_save_text' => (($is_premoderation && !$is_moderator) ? LANG_MODERATION_SEND : ($item['is_approved'] ? LANG_SAVE : LANG_PUBLISH)),
'button_draft_text' => $button_draft_text,
'hide_draft_btn' => !empty($ctype['options']['disable_drafts']),
'is_multi_cats' => !empty($ctype['options']['is_cats_multi']),
'is_load_props' => false,
'add_cats' => $add_cats,
'errors' => isset($errors) ? $errors : false
));

Expand Down
60 changes: 37 additions & 23 deletions system/controllers/content/actions/item_props_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,60 @@

class actionContentItemPropsFields extends cmsAction {

public function run(){
public function run() {

if (!$this->request->isAjax()) { cmsCore::error404(); }
if (!$this->request->isAjax()) {
return cmsCore::error404();
}

$ctype_name = $this->request->get('ctype_name', '');
$category_id = $this->request->get('category_id', 0);
$item_id = $this->request->get('item_id', 0);
$add_cats = $this->request->get('add_cats', []);
if ($add_cats){
foreach($add_cats as $index=>$cat_id){
if (!is_numeric($cat_id) || !$cat_id){
unset($add_cats[$index]);
}
}
}

if (!$ctype_name || !$category_id) { cmsCore::error404(); }
if (!$ctype_name || (!$category_id && !$add_cats)) {
return cmsCore::error404();
}

$ctype = $this->model->getContentTypeByName($ctype_name);
if (!$ctype) { cmsCore::error404(); }
if (!$ctype) {
return cmsCore::error404();
}

$template = cmsTemplate::getInstance();
$add_cats[] = $category_id;

$props = $this->model->getContentProps($ctype['name'], $category_id);
$values = $item_id ? ['props' => $this->model->getPropsValues($ctype['name'], $item_id)] : [];

if (!$props){
$template->renderJSON(array(
'success' => false,
'html' => ''
));
}
$form = new cmsForm();

// Добавляется после набора props
$form->addFieldset('', 'props', array(
'is_empty' => true,
'is_hidden' => true
));

$form = $this->addFormPropsFields($form, $ctype, $add_cats);

$values = $item_id ? $this->model->getPropsValues($ctype['name'], $item_id) : array();
// Набор props уже есть в форме, удаляем его тут
$form->removeFieldset('props');

$fields = $this->getPropsFields($props);
ob_start();

$props_html = $template->render('item_props_fields', array(
'props' => $props,
'fields' => $fields,
'values' => $values,
), new cmsRequest(array(), cmsRequest::CTX_INTERNAL));
$this->cms_template->renderForm($form, $values, [
'form_tpl_file' => 'form_fields'
]);

$template->renderJSON(array(
return $this->cms_template->renderJSON(array(
'success' => true,
'html' => $props_html
'html' => ob_get_clean()
));

}

}
}
7 changes: 6 additions & 1 deletion system/controllers/content/actions/item_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ public function run(){
// Получаем поля-свойства
if ($ctype['is_cats'] && $item['category_id'] > 1){

$props = $this->model->getContentProps($ctype['name'], $item['category_id']);
$prop_cats = [$item['category_id']];
if(!empty($item['categories'])){
$prop_cats = array_keys($item['categories']);
}

$props = $this->model->getContentProps($ctype['name'], $prop_cats);

$props_values = array_filter((array)$this->model->getPropsValues($ctype['name'], $item['id']));

Expand Down
68 changes: 58 additions & 10 deletions system/controllers/content/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ public function renderItemsList($ctype, $page_url, $hide_filter = false, $catego

}


public function getItemInfoBar($ctype, $item, $fields, $subject = 'item') {

$bar = [];
Expand Down Expand Up @@ -703,15 +702,14 @@ public function getCategoryForm($ctype, $action){
//============================================================================//
//============================================================================//

public function getItemForm($ctype, $fields, $action, $data=array(), $item_id=false, $item=false){
public function getItemForm($ctype, $fields, $action, $data = [], $item_id = false, $item = false) {

// Контейнер для передачи дополнительных списков:
// $groups_list, $folders_list и т.д.
extract($data);

// Строим форму
$form = new cmsForm();
$fieldset_id = $form->addFieldset();

// Если включены категории, добавляем в форму поле выбора категории
if ($ctype['is_cats'] && ($action != 'edit' || $ctype['options']['is_cats_change'])){
Expand All @@ -737,11 +735,15 @@ public function getItemForm($ctype, $fields, $action, $data=array(), $item_id=fa
}

if (!empty($ctype['options']['is_cats_multi'])){

$fieldset_id = $form->addFieldset(LANG_ADDITIONAL_CATEGORIES, 'multi_cats', array(
'is_empty' => true
));

unset($cats['']);
$form->addField($fieldset_id,
new fieldList('add_cats', array(
'title' => LANG_ADDITIONAL_CATEGORIES,
'is_chosen_multiple' => true,
'items' => $cats
)
)
);
}

}
Expand All @@ -762,11 +764,12 @@ public function getItemForm($ctype, $fields, $action, $data=array(), $item_id=fa
)));
}

// Если есть поля-свойства, то добавляем область для них
// Если есть поля-свойства, то добавляем область
// После этого поля будут добавляться свойства
if (!empty($ctype['props'])){
$form->addFieldset('', 'props', array(
'is_empty' => true,
'class' => 'highlight'
'is_hidden' => true
));
}

Expand Down Expand Up @@ -1119,6 +1122,51 @@ public function getPropsFields($props){

}

public function addFormPropsFields($form, $ctype, $item_cats, $is_submitted = false){

if($is_submitted && $ctype['options']['is_cats_change']){
$item_cats = [];
if ($this->request->has('add_cats') && !empty($ctype['options']['is_cats_multi'])){
$item_cats = $this->request->get('add_cats', []);
foreach($item_cats as $index => $cat_id){
if (!is_numeric($cat_id) || !$cat_id){
unset($item_cats[$index]);
}
}
}
if($this->request->has('category_id')){
$item_cats[] = $this->request->get('category_id', 0);
}
}

if(!$item_cats){
return $form;
}

$item_props = $this->model->getContentProps($ctype['name'], $item_cats);
$item_props_fields = $this->getPropsFields($item_props);

$props = [];

foreach ($item_props as $p) {
$props[$p['cat_id']]['title'] = $p['cat_title'];
$props[$p['cat_id']]['props'][] = $p['id'];
}

$fid = 'props';

foreach ($props as $cat_id => $p) {

$fid = $form->addFieldsetAfter($fid, $p['title'], 'props'.$cat_id, ['class' => 'icms-content-props__fieldset highlight bg-light']);

foreach($p['props'] as $prop_id) {
$form->addField($fid, $item_props_fields[$prop_id]);
}
}

return $form;
}

//============================================================================//
//============================================================================//

Expand Down

0 comments on commit 8b7eca9

Please sign in to comment.