Source for file edit_document.php
Documentation is available at edit_document.php
<?php // $Id: edit_document.php 15550 2008-06-11 17:24:32Z juliomontoya $
==============================================================================
Dokeos - elearning and course management software
Copyright (c) 2004-2008 Dokeos SPRL
Copyright (c) 2003 Ghent University (UGent)
Copyright (c) 2001 Universite catholique de Louvain (UCL)
Copyright (c) Olivier Brouckaert
Copyright (c) Roan Embrechts
Copyright (c) Rene Haentjens (RH) (update 2004/09/30)
Copyright (c) Bart Mollet, Hogeschool Gent
For a full list of contributors, see "credits.txt".
The full license can be read in "license.txt".
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See the GNU General Public License for more details.
Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
==============================================================================
==============================================================================
* This file allows editing documents.
* Based on create_document, this file allows
* - edit metadata (requires a document table entry)
* - edit html content (only for htm/html files)
* - show editable name field
* - show editable comments field
* Additionally, for html and text files
* Remember, all files and folders must always have an entry in the
* database, regardless of wether they are visible/invisible, have
* @package dokeos.document
* @todo improve script structure (FormValidator is used to display form, but
* not for validation at the moment)
==============================================================================
// name of the language file that needs to be included
$language_file = 'document';
------------------------------------------------------------------------------
------------------------------------------------------------------------------
include('../inc/global.inc.php');
<script type="text/javascript">
function InnerDialogLoaded()
var B=new window.frames[0].FCKToolbarButton(\'Templates\',window.frames[0].FCKLang.Templates);
function FCKeditor_OnComplete( editorInstance )
document.getElementById(\'frmModel\').innerHTML = "<iframe height=950px; width=100%; frameborder=0 src=\''. api_get_path(WEB_LIBRARY_PATH). 'fckeditor/editor/fckdialogframe.html \'>";
$_SESSION['whereami'] = 'document/create';
include(api_get_path(LIBRARY_PATH). 'fileManage.lib.php');
include(api_get_path(LIBRARY_PATH). 'fileUpload.lib.php');
include(api_get_path(LIBRARY_PATH). 'events.lib.inc.php');
include(api_get_path(LIBRARY_PATH). 'document.lib.php');
include_once(api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php');
require_once(api_get_path(LIBRARY_PATH). 'formvalidator/FormValidator.class.php');
$fck_attribute['Width'] = '100%';
$fck_attribute['Height'] = '950';
$fck_attribute['ToolbarSet'] = 'Full';
$fck_attribute['Config']['FullPage'] = true;
------------------------------------------------------------------------------
------------------------------------------------------------------------------
//echo('file: '.$file.'<br>');
//echo('doc: '.$doc.'<br>');
$dir= $_GET['curdirpath'];
//echo('dir: '.$dir.'<br>');
//echo('file_name: '.$file_name.'<br>');
$baseServUrl = $_configuration['url_append']. "/";
$courseDir = $_course['path']. "/document";
$baseWorkDir = $baseServDir. $courseDir;
$use_document_title = (get_setting('use_document_title')== 'true')? true: false;
------------------------------------------------------------------------------
------------------------------------------------------------------------------
/* please do not modify this dirname formatting */
if($dir[strlen($dir)- 1] != '/')
$filepath= api_get_path('SYS_COURSE_PATH'). $_course['path']. '/document'. $dir;
$filepath= api_get_path('SYS_COURSE_PATH'). $_course['path']. '/document/';
/**************************************************/
if(!empty($_SESSION['_gid']))
$req_gid = '&gidReq='. $_SESSION['_gid'];
$interbreadcrumb[]= array ("url"=> "../group/group_space.php?gidReq=". $_SESSION['_gid'], "name"=> get_lang('GroupSpace'));
$interbreadcrumb[]= array("url"=> "./document.php?curdirpath=". urlencode($_GET['curdirpath']). $req_gid, "name"=> get_lang('Documents'));
==============================================================================
==============================================================================
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
These do the actual work that is expected from of this tool, other functions
are only there to support these ones.
------------------------------------------------------------------------------
This function changes the name of a certain file.
It needs no global variables, it takes all info from parameters.
function change_name($baseWorkDir, $sourceFile, $renameTo, $dir, $doc)
$file_name_for_change = $baseWorkDir. $dir. $sourceFile;
//api_display_debug_info("call my_rename: params $file_name_for_change, $renameTo");
if (isset ($dir) && $dir != "")
$sourceFile = $dir. $sourceFile;
$new_full_file_name = dirname($sourceFile). "/". $renameTo;
$sourceFile = "/". $sourceFile;
$new_full_file_name = "/". $renameTo;
update_db_info("update", $sourceFile, $new_full_file_name); //fileManage API
$info_message = get_lang('fileModified');
$GLOBALS['file_name'] = $renameTo;
$GLOBALS['doc'] = $renameTo;
------------------------------------------------------------------------------
Code to change the comment
------------------------------------------------------------------------------
Step 2. React on POST data
if (isset ($_POST['newComment']))
//to try to fix the path if it is wrong
// Check if there is already a record for this file in the DB
$result = api_sql_query ("SELECT * FROM $dbTable WHERE path LIKE BINARY '". $commentPath. "'",__FILE__ ,__LINE__ );
$attribute['path' ] = $row['path' ];
$attribute['comment' ] = $row['title'];
//Determine the correct query to the DB
//new code always keeps document in database
$query = "UPDATE $dbTable
SET comment='". $newComment. "', title='". $newTitle. "'
LIKE BINARY '". $commentPath. "'";
$oldComment = $newComment;
$info_message = get_lang('fileModified');
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Step 2. react on POST data - change the name
if (isset ($_POST['renameTo']))
$info_message = change_name($baseWorkDir, $_GET['sourceFile'], $_POST['renameTo'], $dir, $doc);
//assume name change was successful
------------------------------------------------------------------------------
Code to change the comment
------------------------------------------------------------------------------
Step 1. Create dialog box.
/** TODO check if this code is still used **/
/* Search the old comment */ // RH: metadata: added 'id,'
$result = api_sql_query("SELECT id,comment,title FROM $dbTable WHERE path LIKE BINARY '$dir$doc'",__FILE__ ,__LINE__ );
$message = "<i>Debug info</i><br>directory = $dir<br>";
$message .= "document = $file_name<br>";
$message .= "comments file = " . $file . "<br>";
//Display::display_normal_message($message);
$oldComment = $row['comment'];
$oldTitle = $row['title'];
$docId = $row['id']; // RH: metadata
------------------------------------------------------------------------------
WYSIWYG HTML EDITOR - Program Logic
------------------------------------------------------------------------------
if($_POST['formSent']== 1)
if(isset ($_POST['renameTo']))
$extension= explode('.',$_POST['filename']);
$extension= $extension[sizeof($extension)- 1];
$_POST['filename']= str_replace('.'. $extension,'',$_POST['filename']);
if(!strstr($texte,'/css/frames.css'))
$texte= str_replace('</title></head>','</title><link rel="stylesheet" href="../css/frames.css" type="text/css" /></head>',$texte);
// RH commented: $filename=replace_dangerous_char($filename,'strict');
//if($_POST['extension'] != 'htm' && $_POST['extension'] != 'html')
$extension = $_POST['extension'];
$file= $dir. $filename. '.'. $extension;
$read_only_flag= $_POST['readonly'];
if (!empty($read_only_flag))
$show_edit= $_SESSION['showedit'];
//unset($_SESSION['showedit']);
if($fp= @fopen($filepath. $filename. '.'. $extension,'w'))
//if flv player, change absolute paht temporarely to prevent from erasing it in the following lines
$path_to_remove= api_get_path('WEB_COURSE_PATH'). $_course['path']. '/document'. $dir;
$texte= str_replace('mp3player.swf?son='. urlencode($path_to_remove),'mp3player.swf?son=.%2F',$texte);
// for flv player : change back the url to absolute
$perm = octdec(!empty($perm)? $perm: '0770');
mkdir($filepath. 'css',$perm);
if(!is_file($filepath. 'css/frames.css'))
$doc_id= add_document($_course,$dir. 'css/frames.css','file',filesize($filepath. 'css/frames.css'),'frames.css');
// "WHAT'S NEW" notification: update table item_property (previously last_tooledit)
$file_size = filesize($filepath. $filename. '.'. $extension);
//$msgError=get_lang('Impossible');
$file_size = filesize($filepath. $filename. '.'. $extension);
$filepath. $filename. '.'. $extension;
$file_size = filesize($filepath. $filename. '.'. $extension);
//replace relative paths by absolute web paths (e.g. "./" => "http://www.dokeos.com/courses/ABC/document/")
$extension= $extension[sizeof($extension)- 1];
/*if(!in_array($extension,array('html','htm'))) // that was wrong
$extension=$filename=$texte='';
if(in_array($extension,array('html','htm')))
$texte= file($filepath. $doc);
$path_to_append= api_get_path('WEB_COURSE_PATH'). $_course['path']. '/document'. $dir;
$texte= str_replace('="./','="'. $path_to_append,$texte);
$texte= str_replace('mp3player.swf?son=.%2F','mp3player.swf?son='. urlencode($path_to_append),$texte);
==============================================================================
==============================================================================
if( isset ($info_message))
$sql = 'SELECT id, readonly FROM '. $dbTable. ' WHERE path LIKE BINARY "'. $dir. $doc. '"';
WHERE tool LIKE "document"
$form->addElement('hidden','filename');
$form->addElement('hidden','extension');
$form->addElement('hidden','file_path');
$form->addElement('hidden','commentPath');
$form->addElement('hidden','showedit');
$form->add_textfield('newTitle',get_lang('Title'));
$defaults['newTitle'] = $oldTitle;
$form->addElement('hidden','renameTo');
$form->addElement('hidden','formSent');
$defaults['formSent'] = 1;
$read_only_flag= $_POST['readonly'];
$defaults['texte'] = $texte;
if($extension == 'htm' || $extension == 'html')
if (empty($readonly) && $readonly== 0)
$form->addElement('submit','submit',get_lang('Ok'));
$form->add_html_editor('texte','',false,true);
$metadata_link = '<a href="../metadata/index.php?eid='. urlencode('Document.'. $docId). '">'. get_lang('AddMetadata'). '</a>';
$form->addElement('static',null,get_lang('Metadata'),$metadata_link);
$form->addElement('textarea','newComment',get_lang('Comment'),'rows="3" style="width:300px;"');
$renderer = $form->defaultRenderer();
$renderer->setElementTemplate('<div class="row"><div class="label"></div><div class="formw">{element}{label}</div></div>', 'readonly');
$checked = &$form->addElement('checkbox','readonly',get_lang('ReadOnly'));
$checked->setChecked(true);
$form->addElement('submit','submit',get_lang('Ok'));
$defaults['filename'] = $filename;
$defaults['extension'] = $extension;
$defaults['file_path'] = $_GET['file'];
$defaults['commentPath'] = $file;
$defaults['renameTo'] = $file_name;
$defaults['newComment'] = $oldComment;
$form->setDefaults($defaults);
$form->addElement('html','<div id="frmModel" style="display:block; height:950px;width:20%; position:absolute; top:135px; left:1px;"></div>');
//Display::display_error_message(get_lang('ReadOnlyFile')); //main API
==============================================================================
==============================================================================
|