/** * !!! NEED TO ADD RELATED_DICVALS SUPPORT !!! * * @param $dic_slug * @param $dicval_id * @param $array * * @return bool|\Illuminate\Support\Collection|null|static */ public static function refresh($dic_slug, $dicval_id, $array) { #Helper::d($dic_slug); #Helper::d($array); ## Find DIC $dic = Dic::where('slug', $dic_slug)->first(); if (!is_object($dic)) { return false; } ## Find dicval $dicval = DicVal::find($dicval_id); if (!is_object($dicval)) { return false; } if ($dicval->dic_id != $dic->id) { $dicval->dic_id = $dic->id; } if (isset($array['slug'])) { $dicval->slug = $array['slug']; } if (isset($array['name'])) { $dicval->name = $array['name']; } $dicval->save(); ## UPDATE FIELDS if (@isset($array['fields']) && is_array($array['fields']) && count($array['fields'])) { #$fields = new Collection(); foreach ($array['fields'] as $key => $value) { $dicval_field_search_array = array('dicval_id' => $dicval->id, 'key' => $key); if (is_array($value) && isset($value['language'])) { $dicval_field_search_array['language'] = @$value['language'] ?: NULL; } $dicval_field = DicFieldVal::firstOrNew($dicval_field_search_array); $dicval_field->value = is_array($value) ? @$value['value'] : $value; $dicval_field->save(); #$fields[] = $dicval_field; } #$dicval->relations['fields'] = $fields; } ## CREATE FIELDS_I18N if (@isset($array['fields_i18n']) && is_array($array['fields_i18n']) && count($array['fields_i18n'])) { #$fields_i18n = new Collection(); foreach ($array['fields_i18n'] as $locale_sign => $fields) { if (!@is_array($fields) || !@count($fields)) { continue; } #$temp = new Collection(); foreach ($fields as $key => $value) { $dicval_field_search_array = array('dicval_id' => $dicval->id, 'language' => $locale_sign, 'key' => $key); $dicval_field = DicFieldVal::firstOrNew($dicval_field_search_array); $dicval_field->value = is_array($value) ? @$value['value'] : $value; $dicval_field->save(); #$temp[] = $dicval_field_i18n; } #$fields_i18n[$locale_sign] = $temp; } #$dicval->relations['fields_i18n'] = $fields_i18n; } ## CREATE TEXT FIELDS if (@isset($array['textfields']) && is_array($array['textfields']) && count($array['textfields'])) { #$textfields = new Collection(); foreach ($array['textfields'] as $key => $value) { $dicval_field_search_array = array('dicval_id' => $dicval->id, 'key' => $key); if (is_array($value) && isset($value['language'])) { $dicval_field_search_array['language'] = @$value['language'] ?: NULL; } $dicval_field = DicTextFieldVal::firstOrNew($dicval_field_search_array); $dicval_field->value = is_array($value) ? @$value['value'] : $value; $dicval_field->save(); #$textfields[] = $dicval_textfield; } #$dicval->relations['textfields'] = $textfields; } ## CREATE TEXT FIELDS_I18N if (@isset($array['textfields_i18n']) && is_array($array['textfields_i18n']) && count($array['textfields_i18n'])) { #$textfields_i18n = new Collection(); foreach ($array['textfields_i18n'] as $locale_sign => $textfields) { if (!@is_array($textfields) || !@count($textfields)) { continue; } #$temp = new Collection(); foreach ($textfields as $key => $value) { $dicval_field_search_array = array('dicval_id' => $dicval->id, 'language' => $locale_sign, 'key' => $key); $dicval_field = DicTextFieldVal::firstOrNew($dicval_field_search_array); $dicval_field->value = is_array($value) ? @$value['value'] : $value; $dicval_field->save(); #$temp[] = $dicval_textfield_i18n; } #$textfields_i18n[$locale_sign] = $temp; } #$dicval->relations['textfields_i18n'] = $textfields_i18n; } ## CREATE META if (@isset($array['meta']) && is_array($array['meta']) && count($array['meta'])) { #$metas = new Collection(); foreach ($array['meta'] as $locale_sign => $fields) { if (!@is_array($fields) || !@count($fields)) { continue; } $temp = new Collection(); foreach ($fields as $key => $value) { $dicval_field_search_array = array('dicval_id' => $dicval->id, 'language' => $locale_sign); $dicval_field = DicValMeta::firstOrNew($dicval_field_search_array); $dicval_field->name = is_array($value) ? @$value['name'] : $value; $dicval_field->save(); #$temp[] = $dicval_meta; } #$metas[$locale_sign] = $temp; } #$dicval->relations['metas'] = $metas; } $dicval->load('allfields', 'alltextfields', 'metas'); ## RETURN DICVAL return $dicval; }
/** * Функция удаляет резервные копии, превысившие лимит * * @param int $dicval_id * @return bool */ private function delete_backups($dicval_id = 0) { /** * Находим запись словаря для удаления ее бэкапов * Запись должна быть оригиналом, т.е. иметь version_of = NULL */ $element = DicVal::where('id', $dicval_id)->with('dic', 'metas', 'allfields', 'seos', 'versions')->first(); if (!isset($element) || !is_object($element) || $element->version_of != NULL) { return false; } #Helper::tad($element); $dic = $element->dic; $versions = Config::get('dic/' . $dic->slug . '.versions'); $element_versions = $element->versions; $result = true; if (count($element_versions) > 0 && count($element_versions) >= $versions) { /** * Вычисляем ID записей, подлежащих удалению */ $for_delete = $element_versions->lists('id'); krsort($for_delete); $for_delete = array_slice($for_delete, 0, count($element_versions) - $versions); #Helper::dd($for_delete); if (count($for_delete)) { $result = false; /** * Открываем транзакцию */ DB::transaction(function () use($element, $for_delete, $result) { /** * Удаляем старые резервные копии и их доп. поля, META и SEO-данные */ DicFieldVal::whereIn('dicval_id', $for_delete)->delete(); DicValMeta::whereIn('dicval_id', $for_delete)->delete(); if (Allow::module('seo')) { Seo::where('module', 'DicVal')->whereIn('unit_id', $for_delete)->delete(); } $deleted = DicVal::where('version_of', $element->id)->whereIn('id', $for_delete)->delete(); #Helper::d($deleted); #Helper::dd($for_delete); if ($deleted) { $result = true; } }); } } return $result; }