/** * @param $id * @param string $item * @param bool $reset * @return null|string|array * @throws nc_Exception_Class_Doesnt_Exist */ public function get_by_id($id, $item = '', $reset = false) { $nc_core = nc_Core::get_object(); $id = intval($id); if (!$id) { return; } //в этом случае был бы возвращен null, но в кэш загружены все компоненты без своих шаблонов $res = array(); if (isset($this->data[$id]) && !$reset) { $res = $this->data[$id]; } if (empty($res)) { $res = $nc_core->db->get_results("SELECT * FROM `Class` WHERE `Class_ID` = '" . $id . "' OR `ClassTemplate` = '" . $id . "' ", ARRAY_A); if (empty($res)) { throw new nc_Exception_Class_Doesnt_Exist($id); } for ($i = 0; $i < $nc_core->db->num_rows; $i++) { if (false && $res[$i]['File_Mode']) { //for debug $class_editor = new nc_class_editor($nc_core->CLASS_TEMPLATE_FOLDER, $nc_core->db); $class_editor->load($res[$i]['Class_ID'], $res[$i]['File_Path'], $res[$i]['File_Hash']); $class_editor->fill_fields(); $res[$i] = array_merge($res[$i], $class_editor->get_fields()); } $this->data[$res[$i]['Class_ID']] = $res[$i]; $this->data[$res[$i]['Class_ID']]['_nc_final'] = 0; $this->data[$res[$i]['Class_ID']]['Real_Class_ID'] = $res[$i]['Class_ID']; } } if (!$this->data[$id]['_nc_final'] && $this->data[$id]['ClassTemplate']) { // визуальные настройки берутся от компонента if ($this->data[$id]['Type'] != 'useful' && $this->data[$id]['Type'] != 'title') { $this->data[$id]['CustomSettingsTemplate'] = $this->get_by_id($this->data[$id]['ClassTemplate'], 'CustomSettingsTemplate'); } if (!@$res[$i]['File_Mode']) { $macrovars = array('%Prefix%' => 'FormPrefix', '%Record%' => 'RecordTemplate', '%Suffix%' => 'FormSuffix', '%Full%' => 'RecordTemplateFull', '%Settings%' => 'Settings', '%TitleTemplate%' => 'TitleTemplate', '%Order%' => 'SortBy', '%AddForm%' => 'AddTemplate', '%AddCond%' => 'AddCond', '%AddAction%' => 'AddActionTemplate', '%EditForm%' => 'EditTemplate', '%EditCond%' => 'EditCond', '%EditAction%' => 'EditActionTemplate', '%DeleteForm%' => 'DeleteTemplate', '%DeleteCond%' => 'DeleteCond', '%DeleteAction%' => 'DeleteActionTemplate', '%SearchForm%' => 'FullSearchTemplate', '%Search%' => 'SearchTemplate', '%CheckAction%' => 'CheckActionTemplate'); foreach ($macrovars as $var => $field) { if (strstr($this->data[$id][$field], $var)) { $this->data[$id][$field] = str_replace($var, $this->get_by_id($this->data[$id]['ClassTemplate'], $field), $this->data[$id][$field]); } } } } $this->data[$id]['_nc_final'] = 1; if ($item && is_array($this->data[$id])) { return array_key_exists($item, $this->data[$id]) ? $this->data[$id][$item] : ""; } return $this->data[$id]; }
/** * Создание шаблона компонента * * @param int $class_id номер исходного класса * @param string $type тип шаблона: useful, rss, admin_mode, inside_admin, xml * @param string $base создать на основе компонента - class_XX, auto, empty * * @return int номер созданого шаблона */ function nc_classtempalte_make($class_id, $type = 'useful', $base = 'auto') { $nc_core = nc_Core::get_object(); $class_id = intval($class_id); $class_name = $nc_core->component->get_by_id($class_id, 'Class_Name'); $File_Mode = false; // создание шаблона на основе другого компонента if (preg_match('/class_(\\d+)/i', $base == 'auto' ? 'class_' . $class_id : $base, $match)) { $File_Mode = nc_get_file_mode('Class', $match[1]); } if (!is_writable($nc_core->CLASS_TEMPLATE_FOLDER)) { return false; } if ($type != 'useful' && $type != 'mobile') { $class_name = constant("CONTROL_CLASS_COMPONENT_TEMPLATE_TYPE_" . strtoupper($type)); } if ($type == 'mobile') { $class_name = $class_name . " (" . constant("CONTROL_CLASS_COMPONENT_TEMPLATE_TYPE_" . strtoupper($type)) . ")"; } if ($base == 'empty') { return $nc_core->component->add($class_name, CONTROL_CLASS_CLASS_TEMPLATE_GROUP, array(), $class_id, $type); } if ($base == 'auto' && in_array($type, array('rss', 'xml', 'trash', 'inside_admin'))) { $template = call_user_func('nc_classtemplate_make_' . $type, $class_id); } if ($base == 'auto' && in_array($type, array('useful', 'admin_mode', 'title', 'mobile'))) { $base = 'class_' . $class_id; } // создание шаблона на основе другого компонента if (preg_match('/class_(\\d+)/i', $base, $match)) { if ($File_Mode) { $class_editor = new nc_class_editor($nc_core->CLASS_TEMPLATE_FOLDER, $nc_core->db); $class_editor->load($match[1]); $class_editor->fill_fields(); $template = $class_editor->get_fields(); } else { $template = $nc_core->component->get_by_id($match[1]); } } return $nc_core->component->add($class_name, CONTROL_CLASS_CLASS_TEMPLATE_GROUP, $template, $class_id, $type); }