Location: PHPKode > projects > DOOR - Digital Open Object Repository > door/admin/add_edit_object.php
<?php
    // include libraries
    require_once("../setup.php");
    
    // ENSURE THAT TREE FILE ARE CREATED START ----------------------------------------------
    // require tree generator script
    require_once('../tree/tree_generator/tree_generator.php');
    // generate tree
    generate_tree();
    // ENSURE THAT TREE FILE ARE CREATED END ------------------------------------------------
     
	//This page is used to add or edit an object, it's always called 
	//with an argument, $_GET['mode']. $_GET['mode'] is stored in $mode.
	//$mode:
	//- 0 -> Add
	//- 1 -> Edit
    
	//Save the mode 0 => add, 1 => edit 
    
    // check for parameters
    if(!isset($_GET['mode'])){
        @ob_clean();
        error_catcher(44);
        exit;
    }
    
    $mode;
	if($_GET['mode']=="Add"){
		$mode=0;
	}else if($_GET['mode']=="Edit"){
		$mode=1;
    }else{
        @ob_clean();
        error_catcher(45);
        exit;
    }
	
	//If the mode of the page is Edit we need the library for the connection with the DB
	if($mode)
		require_once('../lib/db/dbcom.php');
	
    //Check the login
	if($logged_in != 1) { //not logged
		error_catcher(1);
		exit;
	}else{
        // check if the user can access the page
        authorized_to_access_page('add_edit_object');
		//If the mode of the page is Edit the object is instantied
		if($mode){
            // check for parameters
            if(!isset($_GET['id'])){
                @ob_clean();
                error_catcher(44);
                exit;
            }
            
            // check if the user can modify the object
            authorized_to_manage_object($_GET['id']);
                    
			$id = $_GET['id'];
			$obj = objectinfo($id);
			if(is_string($obj)){
				if($obj=="DBError"){
					error_catcher(4);
					exit;
				}
			}else{			
				//Javascript functions required = Edit
				print('
					<SCRIPT LANGUAGE="JavaScript">
						function validate(){
							var title = document.theForm.title.value;
							var description = tinyMCE.activeEditor.getContent();
							var filetype;
							var i=0;
							for(i=0;;i++){
								if (document.theForm.filetype[i].checked) {
									filetype = document.theForm.filetype[i].value;
									break;
								}
							}
							var userfile = document.theForm.userfile.value;
							
							if(title=="" || description==""){
								alert("'.get_string('script', 'ScriptTitleDescriptionEmpty').'");
								return false;
							}else if(filetype=="url"){
								if(userfile=="" || userfile=="http://"){
									alert("'.get_string('script', 'ScriptEnterAnUrl').'");
									return false;
								}
							}else if(filetype=="local"){
								if(userfile==""){
									alert("'.get_string('script', 'ScriptChooseAFile').'");
									return false;
								}						
							}else{
								return true;
							}
						}
					</SCRIPT>
				');
			}
		}else{
			//Javascript functions required in mode = Add
			print('
				<SCRIPT LANGUAGE="JavaScript">
					function validate(){
						var title = document.theForm.title.value;
						var description = tinyMCE.activeEditor.getContent();
						var filetype;
						var i=0;
						for(i=0;;i++){
							if (document.theForm.filetype[i].checked) {
								filetype = document.theForm.filetype[i].value;
								break;
							}
						}
						var userfile = document.theForm.userfile.value;
						
						if(title=="" || description==""){
							alert("'.get_string('script', 'ScriptTitleDescriptionEmpty').'");
							return false;
						}else if(filetype=="url"){
							if(userfile=="" || userfile=="http://"){
								alert("'.get_string('script', 'ScriptEnterAnUrl').'");
								return false;
							}
						}else if(filetype=="local"){
							if(userfile==""){
								alert("'.get_string('script', 'ScriptChooseAFile').'");
								return false;
							}						
						}else{
							return true;
						}
					}
				</SCRIPT>
			');
		}
		print("
			<SCRIPT LANGUAGE='JavaScript'>
				function choiceElement(){
					var elements = document.theForm.filetype.length;
					if(elements==3){
						if(document.theForm.filetype[0].checked)
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"hidden\" style=\" width: 410px; \"/>';
						else if(document.theForm.filetype[1].checked)
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"file\" style=\" width: 410px; \" size=\"65\" /><br>".get_string('object', 'ObjectPropertyAttachmentLocalFileMaximum').' '.ini_get('upload_max_filesize')."b';
						else
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"text\" style=\" width: 410px; \" value=\"http://\"/><br>".get_string('object', 'ObjectPropertyAttachmentUrlMessage')."';
					}else{
						if(document.theForm.filetype[0].checked)
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"hidden\" style=\" width: 410px; \"/>';
						else if(document.theForm.filetype[1].checked)
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"hidden\" style=\" width: 410px; \"/>';
						else if(document.theForm.filetype[2].checked)
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"file\" style=\" width: 410px; \" size=\"65\" /><br>".get_string('object', 'ObjectPropertyAttachmentLocalFileMaximum').' '.ini_get('upload_max_filesize')."b';
						else
							document.getElementById('lo_choice').innerHTML='<input name=\"userfile\" type=\"text\" style=\" width: 410px; \" value=\"http://\"/><br>".get_string('object', 'ObjectPropertyAttachmentUrlMessage')."';
					}						
				}
				
				function showCheckList(action) {
					var vals;
                    var width = (action == 'teaching_disciplines') ? 400 : 250;
					switch(action){
						case 'keywords':
                            vals = document.theForm.keywords.value;
                            break;
                        case 'teaching_disciplines':
                            vals = document.theForm.teaching_disciplines.value;
                            break;
						case 'language':
							var vals = document.theForm.language.value;
							break;
						case 'type_of_resource':
							var vals = document.theForm.type_of_resource.value;
							break;
						case 'useBy':
							var vals = document.theForm.useBy.value;
							break;
						case 'useIn':
							var vals = document.theForm.useIn.value;
							break;
					}							
                    sList = window.open('../admin/popupList.php?action='+action+'&form=1&vals='+URLEncode(vals), 'list', 'width=' + width + ',height=500,scrollbars=yes,resizable=yes,status=no');
                }
                
                var progress_bar_tmp;
                if (document.images){
                    progress_bar_tmp= new Image(347,25);
                    progress_bar_tmp.src=\"../images/progress_bar.gif\";
                }                    
                function show_progress_bar(){
                    document.getElementById('sidebar_area').style.height = document.getElementById('sidebar_area').offsetHeight + 100;
                    document.getElementById('content_area').style.height = document.getElementById('content_area').offsetHeight + 100;
                    document.getElementById(\"work_in_progress\").innerHTML = \"<p class=\'pagetitle\'>".get_string('object', 'ObjectProgressBarWorkInProgress')."</p><img style=\'margin-left: 20px; border: 1px solid #CC6600;\' src=\'\"+progress_bar_tmp.src+\"\' />\";
                    document.getElementById(\"work_in_progress\").style.visibility = \"visible\";
		            document.getElementById(\"work_in_progress\").style.width = \"auto\";
		            document.getElementById(\"work_in_progress\").style.height = \"auto\";
		            document.getElementById(\"work_in_progress\").style.display = \"block\";
                }
                
			</SCRIPT>
		");
?>
		<div id="content_area">
		<p class="pagetitle"> <?php if($mode) echo get_string('object', 'ObjectEditObjectTitle'); else echo get_string('object', 'ObjectAddObjectTitle'); ?></p>
	
		<form action="<?php if($mode) print("../admin/edit.php?location=".$_SERVER['PHP_SELF']); else print("../admin/upload.php"); ?>" method="post" enctype="multipart/form-data" id="theForm" name="theForm" class="txt" onSubmit="show_progress_bar()">
			<?php if($mode){ ?>
                <input name="id" type="hidden" value="<?php echo $id ?>">
            <?php } ?>

			<table width="530" border="0" cellspacing="4" cellpadding="2">
          		<tr> 
            		<td width="120" valign="top" class="txt"><?php echo get_string('object', 'ObjectPropertyTitle'); ?></td>
            		<td width="410" class="txt"> <input id="title" name="title" style=" width: 410px; " type="text" value="<?php if($mode) echo htmlspecialchars($obj->title); ?>"></td>
         			<td class="txt"><b style="color:#FF0000"> *</b></td>
				</tr>
          		<tr> 
            		<td valign="top" class="txt"><?php echo get_string('object', 'ObjectPropertyDescription'); ?></td>
            		<td class="txt"> <textarea id="description" name="description" style=" width: 410px; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;" rows="20"><?php if($mode) echo htmlspecialchars($obj->description); ?></textarea></td>
          			<td class="txt"><b style="color:#FF0000"> *</b></td>
				</tr>
          		<tr> 
            		<td class="txt"><?php echo get_string('object', 'ObjectPropertyKeywords'); ?></td>
            		<td class="txt"> 
						<nobr>
                        <input type="text" name="keywords" style=" width: 333px;" value="<?php if($mode) echo htmlspecialchars($obj->keywords); ?>">
						<input class="button" name="button" type="button" style=" width: 75px;" value="<?php echo get_string('object', 'ObjectSetButton'); ?>" onClick="
							<?php
								print("showCheckList('keywords')");
							?>
						"></nobr>
						<br>
						<?php echo get_string('object', 'ObjectPropertyKeywordsMessage'); ?>
					</td>
          		</tr>
         		<?php if($mode) {?>
					<tr> 
            			<td class="fixfields"><?php echo get_string('object', 'ObjectPropertyCatalogReference'); ?></td>
            			<td class="fixfields">
							<?php echo $obj->catalog_reference." - ".$obj->catalog_reference_name ?>
							<input type="hidden" name="old_catalog_tree_id" value="<?php if($mode) echo htmlspecialchars($obj->catalog_reference); ?>">
						</td>
          			</tr>
         			<tr> 
            			<td class="fixfields"><?php echo get_string('object', 'ObjectPropertyNewCatalogReference'); ?></td>
            			<td class="fixfields"> 
							<select name="catalog_tree_id" style="width: 390px">
								<option value="nochange" SELECTED>--<?php echo get_string('object', 'ObjectPropertyNewCatalogReferenceInstruction'); ?>--</OPTION>
								<?php require_once($CFG->tree_cfg_dir . DIRECTORY_SEPARATOR . 'tree_menu_active.js');?>
              				</select>
			  				<br><?php echo get_string('object', 'ObjectPropertyCatalogReferenceMessage'); ?>
              				&nbsp; 
             			</td>
          			</tr>
				<?php } else { ?>
					<tr> 
            			<td class="fixfields"><?php echo get_string('object', 'ObjectPropertyCatalogReference'); ?></td>
            			<td class="fixfields"> 
							<select name="catalog_tree_id" style="width: 390px"><?php require_once($CFG->tree_cfg_dir . DIRECTORY_SEPARATOR . 'tree_menu_active.js');?></select>
            			</td>
          			</tr>
				<?php } ?>
                <?php if (isset($settings->teaching_disciplines_support) AND $settings->teaching_disciplines_support === "yes") { ?>
                <tr> 
                    <td class="fixfields"><?php echo get_string('object', 'ObjectPropertyTeachingDisciplines'); ?></td>
                    <td class="fixfields"> 
                        <input class="small_field" type="text" name="teaching_disciplines_labels" value="<?php if($mode) echo $obj->teaching_disciplines_labels; ?>" readonly>
                        <input type="hidden" name="teaching_disciplines" value="<?php if($mode) echo htmlspecialchars($obj->teaching_disciplines); ?>" readonly><input class="button" name="button" type="button" value="<?php echo get_string('object', 'ObjectSetButton'); ?>" onClick="
                            <?php
                                print("showCheckList('teaching_disciplines')");
                            ?>
                        ">
                    </td>
                  </tr>
                <?php } ?>
          		<tr> 
            		<td class="fixfields"><?php echo get_string('object', 'ObjectPropertyLanguages'); ?></td>
            		<td class="fixfields"> 
						<input class="small_field" type="text" name="language" value="<?php if($mode) echo htmlspecialchars($obj->language); ?>" readonly>
             			<input class="button" name="button" type="button" value="<?php echo get_string('object', 'ObjectSetButton'); ?>" onClick="
							<?php
								print("showCheckList('language')");
							?>
						">
            		</td>
          		</tr>
          		<tr> 
            		<td valign="top" class="fixfields"><?php echo get_string('object', 'ObjectPropertyTypesOfResources'); ?></td>
            		<td class="fixfields"> 
						<input class="small_field" type="text" name="type_of_resource" value="<?php if($mode) echo htmlspecialchars($obj->type_of_resource); ?>" readonly>
              			<input class="button" name="button2" type="button" value="<?php echo get_string('object', 'ObjectSetButton'); ?>" onClick="
							<?php
								print("showCheckList('type_of_resource')");
							?>
						">
					</td>
          		</tr>
          		<tr> 
            		<td class="fixfields"><?php echo get_string('object', 'ObjectPropertyUseBy'); ?></td>
            		<td class="fixfields"> 
						<input class="small_field" type="text" id="useBy" name="useBy" value="<?php if($mode) echo htmlspecialchars($obj->useBy); ?>" readonly>
             			<input class="button" name="button" type="button" value="<?php echo get_string('object', 'ObjectSetButton'); ?>" onClick="
           					<?php
								print("showCheckList('useBy')");
							?>
						">
					</td>
         		</tr>
          		<tr> 
            		<td class="fixfields"><?php echo get_string('object', 'ObjectPropertyUseIn'); ?></td>
            		<td class="fixfields"> 
						<input class="small_field" type="text" id="useIn" name="useIn" value="<?php if($mode) echo htmlspecialchars($obj->useIn); ?>" readonly>
              			<input class="button" name="button" type="button" value="<?php echo get_string('object', 'ObjectSetButton'); ?>" onClick="
            				<?php
								print("showCheckList('useIn')");
							?>
						">
					</td>
         		</tr>
          		<tr> 
            		<td class="txt"><?php echo get_string('object', 'ObjectPropertyTimeRequired'); ?></td>
            		<td class="txt">  
					<?php echo get_string('object', 'ObjectPropertyTimeRequiredHours'); ?>:
						<?php 	if(isset($obj)){
									$minutes = $obj->required_time % 60;
				  					$hours = ($obj->required_time - $minutes) / 60; 
								}else{
									$minutes = 0;
									$hours = 0;
								}
						?> 
			  			<select name="time_hour">
						<?php for($i=0; $i<=23; ++$i){ ?>
                			<?php if($i==$hours){?>
								<option selected><?php echo $hours ?></option>
							<?php } else { ?>
                				<option><?php echo $i; ?></option>
							<?php } ?>
						<?php } ?>
              			</select>
              		<?php echo get_string('object', 'ObjectPropertyTimeRequiredMinutes'); ?>:
              			<select name="time_minutes">
						<?php for($i=0; $i<=59; ++$i){ ?>
                			<?php if($i==$minutes){?>
								<option selected><?php echo $minutes ?></option>
							<?php } else { ?>
                				<option><?php echo $i; ?></option>
							<?php } ?>
						<?php } ?>
              			</select></td>

          		</tr>
          		<?php if($mode){ ?>
					<!-- Added only if we are in edit mode -->
					<tr> 
            			<td class="txt"><?php echo get_string('object', 'ObjectPropertyCurrentAttachment'); ?>:</td>
            			<td class="txt">
							<?php 	if(is_int( strpos($obj->submit['userfile']['type'], "zip")))
										$obj->submit['userfile']['name'] = $id.".zip";
									//Rebuild full url of uploaddir
									$file_viewer = $CFG->main_url."/file.php";
									if($obj->submit['userfile']['name']!="")
										echo "<a href='".$file_viewer."/".$id."/".$obj->submit['userfile']['name']."' target='_blank'>".$obj->submit['userfile']['name']."</a><br>";
									else
										echo get_string('object', 'ObjectPropertyAttachmentNone');;
								?>
						</td>
          			</tr>
				<?php } ?>
	      		<tr> 
            		<td class="txt"><?php echo get_string('object', 'ObjectPropertyAttachment'); ?></td>
            		<td colspan="3" class="txt">
						<?php if($mode) print("<input type=\"radio\" name=\"filetype\" value=\"same\" onClick=\"javascript:choiceElement();\" CHECKED>" . get_string('object', 'ObjectPropertyAttachmentSame')); ?>
						<input type="radio" name="filetype" value="no" onClick="javascript:choiceElement();" <?php if(!$mode) print("CHECKED");?>><?php echo get_string('object', 'ObjectPropertyAttachmentNone'); ?>
						<input type="radio" name="filetype" value="local" onClick="javascript:choiceElement();"><?php echo get_string('object', 'ObjectPropertyAttachmentLocalFile'); ?> 
						<input type="radio" name="filetype" value="url" onClick="javascript:choiceElement();"><?php echo get_string('object', 'ObjectPropertyAttachmentUrl'); ?> 
						<p id="lo_choice">
							<input name="userfile" type="hidden" size="30" />
						</p>
					</td>
         		</tr>
          		<tr>
                    <td /> 
            		<td class="txt">  
                        <input class="button" name="submit" type="submit" value="<?php if($mode) echo get_string('object', 'ObjectSaveChangesButton'); else echo get_string('object', 'ObjectInsertObjectButton'); ?>" onclick="return validate()"/>
						<?php   
                            if(!$mode) {
							    echo '<input class="button" name="reset" type="reset" value="' . get_string('object', 'ObjectResetButton') . '" />';
                            } else {
                                echo '<input class="button" name="discard" type="button" value="' . get_string('object', 'ObjectDiscardChangesButton') . '" onclick="location.href=\''.$_SERVER['PHP_SELF'].'?action=object_info&id='. $id .'\'" />';
                            }
                        ?>
					</td>
          		</tr>
                <tr>
					<td colspan="3" class="txt"><b style="color:#FF0000">*</b>&nbsp;<?php echo get_string('object', 'ObjectRequiredFields'); ?><br /><br /><br /></td>
				<tr>
        	</table>
		</form>
        <div id="work_in_progress">
            <p class='pagetitle'><?php echo get_string('object', 'ObjectProgressBarWorkInProgress'); ?></p>
            <img style='margin-left: 20px; border: 1px solid #CC6600;' src='../images/progress_bar.gif' />
        </div>
        <script>setFocus('title')</script>
 		</div>
<?php
	}
?>
Return current item: DOOR - Digital Open Object Repository