/** * Create a new style for the topic * * @param string $style_name Name of the style * @return string Style path */ private function _create_style($style_name) { if (isset($GLOBALS['midcom_style_inherited'])) { $up = midcom::get('style')->get_style_id_from_path($GLOBALS['midcom_style_inherited']); debug_add("Style inherited from {$GLOBALS['midcom_style_inherited']}"); } else { $up = midcom_connection::get('style'); debug_add("No inherited style found, placing the new style under host style ID: " . midcom_connection::get('style')); } $style = new midcom_db_style(); $style->name = $style_name; $style->up = $up; if (!$style->create()) { debug_print_r('Failed to create a new style due to ' . midcom_connection::get_error_string(), $style, MIDCOM_LOG_WARN); midcom::get('uimessages')->add('edit folder', sprintf(midcom::get('i18n')->get_string('failed to create a new style template: %s', 'midcom.admin.folder'), midcom_connection::get_error_string()), 'error'); return ''; } debug_print_r('New style created', $style); return midcom::get('style')->get_style_path_from_id($style->id); }
private function read_style($path, $parent_id) { $style_name = basename($path); $object_qb = midcom_db_style::new_query_builder(); $object_qb->add_constraint('up', '=', $parent_id); $object_qb->add_constraint('name', '=', $style_name); if ($object_qb->count() == 0) { // New style $style = new midcom_db_style(); $style->up = $parent_id; $style->name = $style_name; if (!$style->create()) { return false; } } else { $styles = $object_qb->execute(); $style = $styles[0]; } $directory = dir($path); $foldernames = array(); $filenames = array(); while (false !== ($entry = $directory->read())) { if (substr($entry, 0, 1) == '.') { // Ignore dotfiles continue; } if (is_dir("{$path}/{$entry}")) { // Recurse deeper $this->read_style("{$path}/{$entry}", $style->id); $foldernames[] = $entry; } // Deal with element // Check file type $filename_parts = explode('.', $entry); if (count($filename_parts) < 2) { continue; } $element_name = $filename_parts[0]; $field = false; switch ($filename_parts[count($filename_parts) - 1]) { case 'php': $field = 'value'; break; } if (!$field) { continue; } $filenames[] = $element_name; $file_contents = file_get_contents("{$path}/{$entry}"); $encoding = mb_detect_encoding($file_contents); if ($encoding != 'UTF-8') { $file_contents = @iconv($encoding, 'UTF-8', $file_contents); } $qb = midcom_db_element::new_query_builder(); $qb->add_constraint('style', '=', $style->id); $qb->add_constraint('name', '=', $element_name); if ($qb->count() == 0) { // New element $element = new midcom_db_element(); $element->style = $style->id; $element->name = $element_name; $element->{$field} = $file_contents; $element->create(); continue; } $elements = $qb->execute(); $element = $elements[0]; // Update existing elements only if they have actually changed if ($element->{$field} != $file_contents) { $element->{$field} = $file_contents; $element->update(); } } $directory->close(); if ($this->delete_missing) { // Then delete files and folders that are in DB but not in the importing folder $this->delete_missing_folders($foldernames, $style->id); $this->delete_missing_files($filenames, $style->id); } }