public function convert($ClassID, $action = 'convert') { $nc_core = nc_Core::get_object(); $db = $nc_core->db; $fs = $action == 'convert' ? 1 : 0; if ($action == 'convert') { $this->db->query("UPDATE `Class` SET `File_Mode` = 1, `File_Path` = IF(`ClassTemplate` > 0, CONCAT('/', `ClassTemplate`, '/', `Class_ID`,'/'), CONCAT('/', `Class_ID`,'/')) WHERE `Class_ID` = " . $ClassID . " OR `ClassTemplate` = " . $ClassID); } if ($this->db->errno) { return nc_print_status(CONTROL_CLASS_CONVERT_DB_ERROR, 'error', null, 1); } $classes_data = $this->db->get_results("SELECT * FROM `Class` WHERE `Class_ID` = " . $ClassID . " OR `ClassTemplate` = " . $ClassID . " ORDER BY `ClassTemplate` ASC", ARRAY_A); foreach ($classes_data as $class_data) { $templates = $this->get_class_templates_list(); //save tpls $class_editor = new nc_class_editor($nc_core->CLASS_TEMPLATE_FOLDER, $db); $class_editor->load($class_data['ClassTemplate']); $path = $class_editor->get_absolute_path() . $class_data['Class_ID'] . "/class_v40_backup.html"; if ($action == 'convert') { $new_temlates = $this->convert_class_templates($class_data); $class_editor->save_new_class($class_data['Class_ID'], $new_temlates); foreach ($templates as $field_name => $tmp) { $fields[$field_name] = "<!-- {$field_name} -->" . $class_data[$field_name] . "<!-- /{$field_name} -->"; } $backup_content = join("\n\n", $fields); nc_save_file($path, $backup_content); } else { $restore_content = nc_check_file($path) ? nc_get_file($path) : false; if (!$restore_content) { return nc_print_status(CONTROL_CLASS_CONVERT_UNDO_FILE_ERROR, 'error', null, 1); } foreach ($templates as $field_name => $tmp) { if (preg_match("#<!-- ?{$field_name} ?-->(.*)<!-- ?/ ?{$field_name} ?-->#is", $restore_content, $matches)) { $restored_templates[$field_name] = $matches[1]; } } if ($restored_templates && is_array($restored_templates)) { $query = array(); foreach ($restored_templates as $field_name => $tmp) { $query[] = "`" . $field_name . "` = '" . $db->prepare($tmp) . "'"; } // сообственно, добавление $SQL = "\nUPDATE `Class`"; $SQL .= "\n SET `File_Mode` = 0 , " . join(",\n ", $query); $SQL .= "\n WHERE `Class_ID` = " . $class_data['Class_ID']; $this->db->query($SQL); if ($this->db->errno) { return nc_print_status(CONTROL_CLASS_CONVERT_DB_ERROR, 'error', null, 1); } } } $message = nc_print_status(CONTROL_CLASS_CONVERT_OK, 'OK', null, 1); $message .= "<a href='index.php?phase=4&ClassID=" . $ClassID . "&fs=" . $fs . "'>" . CONTROL_CLASS_CONVERT_OK_GOEDIT . "</a>"; } return $message; //no errors }
public function __construct($path, nc_Db $db, $type = null) { self::$instanse = $this; $this->template = new nc_tpl($path, $db, $type); }
/** * @param $id * @param array $params * @return bool * @throws nc_Exception_DB_Error */ public function update($id, $params = array()) { $nc_core = nc_Core::get_object(); $db = $this->db; $id = intval($id); if (!$id || !is_array($params)) { return false; } $File_Mode = nc_get_file_mode('Class', $id); if ($File_Mode) { $class_editor = new nc_class_editor($nc_core->CLASS_TEMPLATE_FOLDER, $db); } if ($params['action_type'] == 1) { $params_int = array('CacheForUser'); $params_text = array('Class_Name', 'Class_Group'); } else { $params_int = array('AllowTags', 'RecordsPerPage', 'System_Table_ID', 'NL2BR', 'UseCaptcha', 'UseAltTitle'); $params_text = array('FormPrefix', 'FormSuffix', 'RecordTemplate', 'SortBy', 'RecordTemplateFull', 'TitleTemplate', 'AddTemplate', 'EditTemplate', 'AddActionTemplate', 'EditActionTemplate', 'SearchTemplate', 'FullSearchTemplate', 'SubscribeTemplate', 'Settings', 'AddCond', 'EditCond', 'SubscribeCond', 'DeleteCond', 'CheckActionTemplate', 'DeleteActionTemplate', 'CustomSettingsTemplate', 'ClassDescription', 'DeleteTemplate', 'TitleList'); } if ($File_Mode) { $class_editor->load($id); $class_editor->save_fields($only_isset_post = true); $params_text = $class_editor->get_clear_fields($params_text); } $query = array(); foreach ($params as $k => $v) { if (!in_array($k, $params_int) && !in_array($k, $params_text)) { continue; } $query[] = "`" . $db->escape($k) . "` = '" . $db->prepare($v) . "'"; } if (!empty($query)) { $ClassTemplate = $db->get_var("SELECT `ClassTemplate` FROM `Class` WHERE `Class_ID` = '" . $id . "' "); @$nc_core->event->execute("updateSystemTablePrep", 3); if (!$ClassTemplate) { $nc_core->event->execute("updateClassPrep", $id); } else { $nc_core->event->execute("updateClassTemplatePrep", $ClassTemplate, $id); } $db->query("UPDATE `Class` SET " . join(",\n ", $query) . " WHERE `Class_ID` = " . $id); if ($db->is_error) { throw new nc_Exception_DB_Error($db->last_query, $db->last_error); } if (!$ClassTemplate) { $nc_core->event->execute("updateClass", $id); } else { $nc_core->event->execute("updateClassTemplate", $ClassTemplate, $id); } @$nc_core->event->execute("updateSystemTable", 3); } $this->data = array(); return true; }
/** * Парсинг файла с импортированным шаблоном * * @param str file * * @return int component id */ function ParseClassFile4($file) { // system superior object $nc_core = nc_Core::get_object(); $db =& $nc_core->db; $db->captured_errors = array(); $VersionNumber = $nc_core->get_settings("VersionNumber"); $insert_flag = "%INSERT_ID%"; $f = fopen($file, "r"); // STEP 1: System version check $tpl_version = trim(fgets($f, 256)); if (!nc_version_control($tpl_version)) { nc_print_status(sprintf(CONTROL_CLASS_IMPORT_ERROR_VERSION_ID, $tpl_version, $VersionNumber), 'info'); return false; } // STEP 2: skip export info fgets($f, 256); $path_for_file_mode = array(); // STEP 3: import component data $insert_into_class = trim(fgets($f, 65536)); // все компоненты в utf-8 if (!$nc_core->NC_UNICODE) { $insert_into_class = $nc_core->utf8->utf2win($insert_into_class); } $res1 = $db->query($insert_into_class); if ($res1) { $insert_id = $db->insert_id; $path_for_file_mode[$insert_id] = "/{$insert_id}/"; } else { nc_print_status(CONTROL_CLASS_IMPORT_ERROR_CLASS_IMPORT, 'info'); return false; } // STEP 4: import component templates $tpl_id = array(); $next_data = trim(fgets($f, 65536)); while (preg_match("/^INSERT INTO/is", $next_data)) { // все компоненты в utf-8 if (!$nc_core->NC_UNICODE) { $next_data = $nc_core->utf8->utf2win($next_data); } // replace ID $next_data = str_replace($insert_flag, $insert_id, $next_data); if (!$db->query($next_data)) { nc_print_status(CONTROL_CLASS_IMPORT_ERROR_CLASS_TEMPLATE_IMPORT, 'info'); $db->query("DELETE FROM `Class` WHERE `Class_ID` = '" . $insert_id . "'"); return false; } $path_for_file_mode[$db->insert_id] = "/{$insert_id}/{$db->insert_id}/"; $tpl_id[] = $db->insert_id; $next_data = trim(fgets($f, 65536)); } foreach ($path_for_file_mode as $id => $path) { $SQL = "UPDATE `Class` \n SET `File_Path` = '{$path}' \n WHERE `Class_ID` = {$id}"; $db->query($SQL); } // STEP 5: create MessageXX table $create_message = $next_data; $create_message = str_replace($insert_flag, $insert_id, $create_message); $create_message = str_replace('%%MYSQL_CHARSET%%', $nc_core->MYSQL_CHARSET, $create_message); $res2 = $db->query($create_message); if ($res2 != 0) { nc_print_status(CONTROL_CLASS_IMPORT_ERROR_MESSAGE_TABLE, 'info'); $db->query("DELETE FROM `Class` WHERE `Class_ID` = '" . $insert_id . "' OR `ClassTemplate` = '" . $insert_id . "'"); return false; } // STEP 6: append component fields while (!feof($f) && ($insert_into_field = preg_replace("/FileMode.*/m", '', trim(fgets($f, 4096))))) { if (!$nc_core->NC_UNICODE) { $insert_into_field = $nc_core->utf8->utf2win($insert_into_field); } $insert_into_field = str_replace($insert_flag, $insert_id, $insert_into_field); if ($insert_into_field) { $res3 = $db->query($insert_into_field); if ($db->captured_errors) { nc_print_status(CONTROL_CLASS_IMPORT_ERROR_FIELD, 'info'); $db->query("DELETE FROM `Class` WHERE `Class_ID` = '" . $insert_id . "' OR `ClassTemplate` = '" . $insert_id . "'"); $db->query("DROP TABLE `Message" . $insert_id . "`"); $db->query("DELETE FROM `Field` WHERE `Class_ID` = '" . $insert_id . "'"); return false; } } } $import_all_text = file_get_contents($file); $serialize_field = str_replace('FileMode ', '', strstr($import_all_text, 'FileMode')); if ($serialize_field) { $serialize_field = explode('___class_templates___', $serialize_field); $fields = unserialize($serialize_field[0]); $_POST = array_merge($_POST, $fields); $file_class = new nc_class_editor($nc_core->CLASS_TEMPLATE_FOLDER, $nc_core->db, 'Class'); $file_class->save_new_class($insert_id); if ($serialize_field[1]) { $serialize_remplates = explode('___class_templates_separator___', $serialize_field[1]); $file_class->load($insert_id); for ($i = 0, $count = count($serialize_remplates); $i < $count; ++$i) { $fields = unserialize($serialize_remplates[$i]); $_POST = array_merge($_POST, $fields); $file_class->save_new_class($tpl_id[$i]); } } } return $insert_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); }