From 3620474df37f0e98a28126f7890b4124875a651a Mon Sep 17 00:00:00 2001 From: Patrick Cool Date: Wed, 11 Mar 2009 11:45:48 +0100 Subject: [PATCH] [svn r18955] * allow defining of platform templates * refactoring and cleaning up of fckeditor/fcktemplates.xml.php * improved performance of user templates --- main/admin/settings.php | 287 +++++++++++++++++++- main/inc/lib/fckeditor/fcktemplates.xml.php | 190 ++++++++++--- 2 files changed, 431 insertions(+), 46 deletions(-) diff --git a/main/admin/settings.php b/main/admin/settings.php index fa99684cb0..32d501b1c4 100644 --- a/main/admin/settings.php +++ b/main/admin/settings.php @@ -1,4 +1,4 @@ -display(); } @@ -557,7 +560,7 @@ function handle_stylesheets() $allowed_file_types = array ('css'); $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types); $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required'); - $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok')); + $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save')); if( $form->validate() AND is_writable(api_get_path(SYS_CODE_PATH).'css/')) { $values = $form->exportValues(); @@ -840,4 +843,284 @@ function handle_search() { } } +/** + * wrapper for the templates + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function handle_templates() +{ + if ($_GET['action'] == 'add' OR ( $_GET['action'] == 'edit' AND is_numeric($_GET['id']))) + { + add_edit_template(); + } + else + { + if ($_GET['action'] == 'delete' and is_numeric($_GET['id'])) + { + delete_template($_GET['id']); + } + echo ''.get_lang('AddTemplate').''; + display_templates(); + } +} + +/** + * Display a sortable table with all the templates that the platform administrator has defined. + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function display_templates() +{ + $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data',1); + $table->set_additional_parameters(array('category'=>$_GET['category'])); + $table->set_header(0, get_lang('Image')); + $table->set_header(1, get_lang('Title')); + $table->set_header(2, get_lang('Actions')); + $table->set_column_filter(2,'actions_filter'); + $table->set_column_filter(0,'image_filter'); + $table->display(); +} + +/** + * Get the number of templates that are defined by the platform admin. + * + * @return integer + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function get_number_of_templates() +{ + // Database table definition + $table_system_template = Database :: get_main_table('system_template'); + + // The sql statement + $sql = "SELECT COUNT(id) AS total FROM $table_system_template"; + $result = api_sql_query($sql, __FILE__, __LINE__); + $row = Database::fetch_array($result); + + // returning the number of templates + return $row['total']; +} + +/** + * Get all the template data for the sortable table + * + * @param integer $from the start of the limit statement + * @param integer $number_of_items the number of elements that have to be retrieved from the database + * @param integer $column the column that is + * @param string $direction the sorting direction (ASC or DESCą + * @return array + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function get_template_data($from, $number_of_items, $column, $direction) +{ + // Database table definition + $table_system_template = Database :: get_main_table('system_template'); + + // the sql statement + $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template"; + $sql .= " ORDER BY col$column $direction "; + $sql .= " LIMIT $from,$number_of_items"; + $result = api_sql_query($sql, __FILE__, __LINE__); + while ($row = Database::fetch_array($result)) + { + $return[]=$row; + } + + // returning all the information for the sortable table + return $return; +} + +/** + * display the edit and delete icons in the sortable table + * + * @param integer $id the id of the template + * @return html code for the link to edit and delete the template + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function actions_filter($id) +{ + $return .= ''.Display::return_icon('edit.gif').''; + $return .= ''.Display::return_icon('delete.gif').''; + return $return; +} + +/** + * Display the image of the template in the sortable table + * + * @param string $image the image + * @return html code for the image + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function image_filter($image) +{ + return ''.$image.''; +} + +/** + * Add (or edit) a template. This function displays the form and also takes care of uploading the image and storing the information in the database + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function add_edit_template() +{ + // initiate the object + $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.$_GET['action'].'&id='.$_GET['id']); + + // settting the form elements: the header + if ($_GET['action'] == 'add') + { + $title = get_lang('AddTemplate'); + } + else + { + $title = get_lang('EditTemplate'); + } + $form->addElement('header', '', $title); + + // settting the form elements: the title of the template + $form->add_textfield('title', get_lang('Title'), false); + + // settting the form elements: the content of the template (wysiwyg editor) + $form->addElement('html_editor', 'template_text', get_lang('Text')); + + // settting the form elements: the form to upload an image to be used with the template + $form->addElement('file','template_image',get_lang('Image'),''); + + // settting the form elements: a little bit information about the template image + $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70')); + + // getting all the information of the template when editing a template + if ($_GET['action'] == 'edit') + { + // Database table definition + $table_system_template = Database :: get_main_table('system_template'); + $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + $row = Database::fetch_array($result); + + $defaults['template_id'] = $_GET['id']; + $defaults['template_text'] = $row['content']; + $defaults['title'] = $row['title']; + + // adding an extra field: a hidden field with the id of the template we are editing + $form->addElement('hidden','template_id'); + + // adding an extrra field: a preview of the image that is currently used + $form->addElement('static','template_image_preview', '', ''.$row['image'].''); + + // setting the information of the template that we are editing + $form->setDefaults($defaults); + } + // settting the form elements: the submit button + $form->addElement('submit', 'submit', get_lang('Ok')); + + // setting the rules: the required fields + $form->addRule('title', '
'.get_lang('ThisFieldIsRequired'), 'required'); + $form->addRule('template_text', '
'.get_lang('ThisFieldIsRequired'), 'required'); + + // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed) + if( $form->validate() ) + { + // exporting the values + $values = $form->exportValues(); + + // upload the file + if (!empty($_FILES['template_image']['name'])) + { + include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php'); + $upload_ok = process_uploaded_file($_FILES['template_image']); + + if ($upload_ok) + { + // Try to add an extension to the file if it hasn't one + $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']); + + // upload dir + $upload_dir = api_get_path(SYS_PATH).'home/'; + + // move the uploaded file to the home folder + $result= @move_uploaded_file($_FILES['template_image']['tmp_name'], $upload_dir.$new_file_name); + } + } + + // store the information in the database (as insert or as update) + $table_system_template = Database :: get_main_table('system_template'); + if ($_GET['action'] == 'add') + { + $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".Database::escape_string($values['template_text'])."','".Database::escape_string($new_file_name)."')"; + $result = api_sql_query($sql, __FILE__, __LINE__); + + // display a feedback message + Display::display_confirmation_message('TemplateAdded'); + } + else + { + $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."', + content = '".Database::escape_string($values['template_text'])."'"; + if (!empty($new_file_name)) + { + $sql .= ", image = '".Database::escape_string($new_file_name)."'"; + } + $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + + // display a feedback message + Display::display_confirmation_message('TemplateEdited'); + } + display_templates(); + } + else + { + // display the form + $form->display(); + } +} + +/** + * Delete a template + * + * @param integer $id the id of the template that has to be deleted + * + * @author Patrick Cool , Ghent University, Belgium + * @version August 2008 + * @since Dokeos 1.8.6 + */ +function delete_template($id) +{ + // first we remove the image + $table_system_template = Database :: get_main_table('system_template'); + $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + $row = Database::fetch_array($result); + if (!empty($row['image'])) + { + unlink(api_get_path(SYS_PATH).'home/'.$row['image']); + } + + // now we remove it from the database + $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + + // display a feedback message + Display::display_confirmation_message('TemplateDeleted'); +} ?> diff --git a/main/inc/lib/fckeditor/fcktemplates.xml.php b/main/inc/lib/fckeditor/fcktemplates.xml.php index 9fdfdb9005..43bb69ab0e 100644 --- a/main/inc/lib/fckeditor/fcktemplates.xml.php +++ b/main/inc/lib/fckeditor/fcktemplates.xml.php @@ -1,16 +1,61 @@ -'; + +// is this needed? $IMConfig['base_url'] = $_configuration['root_web'].'main/img/gallery/'; + +// load a stylesheet +$css = loadCSS(api_get_setting('stylesheets')); + +// setting some paths +$img_dir = api_get_path(REL_CODE_PATH).'img/'; +$default_course_dir = api_get_path(REL_CODE_PATH).'default_course_document/'; + +// start the templates node +echo ''; + +// load the templates that were defined by the platform admin +load_platform_templates(); + +// load the personal templates +load_personal_templates(api_get_user_id()); + +// load the hardcoded templates +load_hardcoded_templates(); + +// end the templates node +echo ''; +exit; + + +/** + * load a given css style (default.css) + * + * @param string $css_name the folder name of the style + * @return html code for adding a css style