/**
 * This source file is is part of Saurus CMS content management software.
 * It is licensed under MPL 1.1 (http://www.opensource.org/licenses/mozilla1.1.php).
 * Copyright (C) 2000-2010 Saurused Ltd (http://www.saurus.info/).
 * Redistribution of this file must retain the above copyright notice.
 * 
 * Please note that the original authors never thought this would turn out
 * such a great piece of software when the work started using Perl in year 2000.
 * Due to organic growth, you may find parts of the software being
 * a bit (well maybe more than a bit) old fashioned and here's where you can help.
 * Good luck and keep your open source minds open!
 * 
 * @package		SaurusCMS
 * @copyright	2000-2010 Saurused Ltd (http://www.saurus.info/)
 * @license		Mozilla Public License 1.1 (http://www.opensource.org/licenses/mozilla1.1.php)
 * 
 */
function edit_objekt()
{
    global $site;
    global $objekt;
    global $keel;
    global $class_path;
    include_once $class_path . "adminpage.inc.php";
    $profile_id = $objekt->objekt_id ? $objekt->all[profile_id] : $site->fdat[profile_id];
    ?>
	<input type="hidden" name="profile_id" value="<?php 
    echo $profile_id;
    ?>
">
<?php 
    ####################
    # Additional info: attributes list
    # get profile
    $profile_def = $site->get_profile(array("id" => $profile_id));
    $profile_fields = unserialize($profile_def['data']);
    # profile_fields is now array of ALL fields, indexes are fieldnames
    if ($profile_fields['pealkiri']['is_general']) {
        unset($profile_fields['pealkiri']);
    }
    ###################
    # print profile fields rows
    print_profile_fields(array('profile_fields' => $profile_fields, 'field_values' => $objekt->all, 'fields_width' => '300px'));
}
</div>
            </div>
          </td>
        </tr>
	<?php 
        ###### profile fields row
        ?>
		<tr>
          <td valign=top colspan="2"> 
			<!-- Scrollable area -->
			<div id=listing class="scms_scroll_div" style="height: 180">
			<table width="100%" border="0" cellspacing="0" cellpadding="0" class="scms_table">
				<?php 
        ###################
        # print profile fields rows
        print_profile_fields(array('profile_fields' => $profile_fields, 'field_values' => $group->all, 'fields_width' => '300px'));
        ?>
			</table>
			</div>
			<!-- //Scrollable area -->
          </td>
        </tr>	
	<?php 
        ###### / profile fields row
        ?>
		</table>

    </td>
  </tr>
	  <?php 
        ###################
<?	if($form_def['profile_id']){ ?>

<table width="100%"  border="0" cellspacing="3" cellpadding="0" class="scms_table">
<tr class="scms_pane_header">
<td colspan=2><?=$form_def['name']?></td>
</tr>

<?		# get profile
		$profile_def = $site->get_profile(array("id"=>$form_def['profile_id'])); 
		$profile_fields = unserialize($profile_def['data']);	# profile_fields is now array of ALL fields, indexes are fieldnames

		###################
		# print profile fields rows
		print_profile_fields(array(
			'profile_fields' => $profile_fields,
			'field_values' => $objekt->all,
		));
?>
	</table>
<?	}
	### error: no profile id
	else {
		echo "Error! no profile found";
	}
?>
</div>
<!-- // Scrollable area -->

	</body>
	</html>
<?	exit;
/**
 * This source file is is part of Saurus CMS content management software.
 * It is licensed under MPL 1.1 (http://www.opensource.org/licenses/mozilla1.1.php).
 * Copyright (C) 2000-2010 Saurused Ltd (http://www.saurus.info/).
 * Redistribution of this file must retain the above copyright notice.
 * 
 * Please note that the original authors never thought this would turn out
 * such a great piece of software when the work started using Perl in year 2000.
 * Due to organic growth, you may find parts of the software being
 * a bit (well maybe more than a bit) old fashioned and here's where you can help.
 * Good luck and keep your open source minds open!
 * 
 * @package		SaurusCMS
 * @copyright	2000-2010 Saurused Ltd (http://www.saurus.info/)
 * @license		Mozilla Public License 1.1 (http://www.opensource.org/licenses/mozilla1.1.php)
 * 
 */

 
function edit_objekt () {
	global $site;
	global $objekt;
	global $keel;
	global $class_path;

	include_once($class_path."adminpage.inc.php");

	$aeg = $objekt->all[aeg] ? $site->db->MySQL_ee($objekt->all[aeg]) : $site->eesti_aeg();
	$profile_id = $objekt->objekt_id ? $objekt->all[profile_id] : $site->fdat[profile_id];
	# kui ikka veel profiil poel teada, kasuta defautl profiili
	if(!$profile_id) {
		# POOLELI - mis on tegelikult dokumendi profiili default ID?
		$profile_id = 55;
	}

?>
	<script>
	function setPealkiri (strPealkiri) {
		var algus=0;
		var saved_algus=0;

		if (document.frmEdit.pealkiri.value=='') {
			while (algus!=-1) {
				saved_algus=algus;
				algus=strPealkiri.indexOf('\\',algus+1);
			}
			algus=saved_algus;
			while (algus!=-1) {
				saved_lopp=algus;
				algus=strPealkiri.indexOf('\.',algus+1);
			}

			document.frmEdit.pealkiri.value = strPealkiri.substr(saved_algus+1,saved_lopp-saved_algus-1);
		}
	}
	</script>
	 <?########### fail ?>
		<tr>
		  <td nowrap><?=$site->sys_sona(array(sona => "filename", tyyp=>"editor"))?>:</td>
		  <td><input type="file" name=file onChange="setPealkiri(file.value)" class="scms_flex_input" style="width:100%"></td>
		</tr>
	 <?########### kirjeldus ?>
		<tr>
		  <td nowrap><?=$site->sys_sona(array(sona => "Kirjeldus", tyyp=>"editor"))?>:</td>
		  <td><textarea name="kirjeldus" rows=5  style="width:100%"><?=htmlspecialchars(stripslashes($objekt->all[kirjeldus]))?></textarea></td> 
		</tr>
	 <?########### autor ?>

	<?
	$sql = "select distinct autor from obj_dokument where autor not like '' order by autor";
	$sth = new SQL($sql);
	$site->debug->msg($sth->debug->get_msgs());
	?>
		<tr>
		  <td nowrap><?=$site->sys_sona(array(sona => "Autor", tyyp=>"editor"))?>:</td>
		  <td><input type="text" class="scms_flex_input" style="width:80px" name=autor value="<?=htmlspecialchars($objekt->all[autor])?>">
		  <select name=autor_select onChange="frmEdit.autor.value = frmEdit.autor_select[frmEdit.autor_select.selectedIndex].value">
			<option>vali</option>
<?
	while ($autor=$sth->fetchsingle()) {
			print "<option value=\"$autor\">$autor</option>";
	}
?>
		  </select>
		  </td>
		</tr>

	 <?########### aeg ?>
		<tr>
		  <td nowrap><?=$site->sys_sona(array(sona => "Aeg", tyyp=>"editor"))?>:</td>
		  <td><input type="text" class="scms_flex_input" style="width:80px"  name=aeg value="<?=htmlspecialchars($aeg)?>"></td>
		</tr>
<?
	####################
	# Additional info: attributes list

		# get profile
		$profile_def = $site->get_profile(array("id"=>$profile_id)); 
		$profile_fields = unserialize($profile_def['data']);	# profile_fields is now array of ALL fields, indexes are fieldnames
		### unset default fields:
	#	unset($profile_fields['fail']);
	#	unset($profile_fields['autor']);
	#	unset($profile_fields['kirjeldus']);
		#printr($profile_fields);

		###################
		# print profile fields rows
		print_profile_fields(array(
			'profile_fields' => $profile_fields,
			'field_values' => $objekt->all,
			'fields_width' => '300px',
		));
}
	function print_profiles()
	{
		global $site, $objekt;

		$sql = $site->db->prepare("SELECT profile_id AS id, source_table AS parent, name FROM object_profiles WHERE source_table=? ORDER BY name",'obj_artikkel');
		$sth = new SQL($sql);

		# get object profile
		if($objekt->all['profile_id']) {
			$profile_def = $site->get_profile(array("id"=>$objekt->all['profile_id']));
			$site->fdat['profile_id'] = $profile_def['profile_id'];
		}
		# if still not found then use default profile for this class
		if(!$profile_def['profile_id'] && !$site->fdat['profile_id']) {

			$site->fdat['profile_id'] = $site->get_default_profile_id(array("source_table" => 'obj_artikkel'));
			$profile_def = $site->get_profile(array("id"=>$site->fdat['profile_id']));
		}
		?>

<fieldset>
	<legend>
		<select onchange="changeProfile(this)" name="profile_id">
		<?php
		$all_profiles_hash = array();
		while ($profile_data = $sth->fetch()){
			$all_profiles_hash[] = $profile_data['id'];
			print "<option value='".$profile_data['id']."' ".($profile_data['id']==$site->fdat['profile_id'] ? '  selected':'').">".$site->sys_sona(array(sona => $profile_data['name'], tyyp=>"custom"))."</option>";
		} ?>
		</select>
	</legend>

	<?php foreach($all_profiles_hash as $profile_id) {	?>

	<div id="profile_<?= $profile_id ?>" style="display: <?=($site->fdat['profile_id'] == $profile_id ? 'block' : 'none');?>;">
		<table cellpadding="0" cellspacing="0">
	<?php
		$profile_def = $site->get_profile(array("id"=>$profile_id));
		$profile_fields = unserialize($profile_def['data']);

		# if profile fields exist
		if(is_array($profile_fields) && sizeof($profile_fields)>0){

			## add suffix for each field, to get unique id-s
			foreach($profile_fields as $key=>$tmp_prof){
				$profile_fields[$key]['html_fieldname'] = $profile_fields[$key]['name']."_".$profile_id;

				# field can be INPUT or READ-ONLY value - this info may be passed from triggers file "actions.inc.php", using "$site->fdat" array
				$profile_fields[$key]['is_readonly'] = $site->fdat['is_readonly_'.$key];
			}
			#printr($profile_fields);

			###################
			# print profile fields rows
			print_profile_fields(array(
				'profile_fields' => $profile_fields,
				'field_values' => $objekt->all,
			));

		} # if profile fields exist

	?>
		</table>
	</div>

	<?php } //foreach ?>

</fieldset>

<fieldset>
	<legend><?=$site->sys_sona(array('sona' => 'visible_to_visitors', 'tyyp' => 'editor'))?></legend>
	<input type="radio" name="publish" id="object_published" value="1"<?=($site->fdat['publish'] || $objekt->all['on_avaldatud'] ? ' checked' : '')?>> <label for="object_published"><?=$site->sys_sona(array('sona' => 'published', 'tyyp' => 'editor'))?></label><br>
	<input type="radio" name="publish" id="object_unpublished" value="0"<?=($site->fdat['publish'] == 0 && $objekt->all['on_avaldatud'] == 0 ? ' checked' : '')?>> <label for="object_unpublished"><?=$site->sys_sona(array('sona' => 'unpublished', 'tyyp' => 'editor'))?></label><br>
</fieldset>

<fieldset>
	<?php ####### dont show checkbox "Headline is visible" if config variable "killheadlineisvisible" is true in file config.php
	if($site->CONF['killheadlineisvisible']) { ?>

	<input type="hidden" id="on_pealkiri" name="on_pealkiri" value="<?=($site->fdat['op'] == 'new' ? 1 : $objekt->all['on_pealkiri'])?>">

	<?php } else { # by default: show it ?>
	<div>
		<input type="checkbox" id="on_pealkiri" name="on_pealkiri" value="1" <?=($site->fdat['op'] == 'new') ? 'checked' : ($objekt->all['on_pealkiri'] ? 'checked' : null)?>>
		<label for="on_pealkiri"><?=$site->sys_sona(array(sona => 'Pealkiri on nahtav', tyyp => 'editor'));?></label>
	</div>
	<?php } ?>
	<div>
		<input type="checkbox" name="on_foorum" id="on_foorum" value="1" <?=($objekt->all['on_foorum'] || ($site->fdat['op'] == 'new' && ($site->CONF['default_comments'] || $site->fdat['allow_comments'])) ? 'checked' : null)?>>
		<label for="on_foorum"><?=$site->sys_sona(array(sona => "Foorum lubatud", tyyp=>"editor"))?></label>
	</div>

	<?php if ($site->CONF['enable_mailing_list']) {  ## Bug #2590
	## fuzzy logic: if editor checks here checkbox "is mailinglist",
	# then field "on_saadetud" is set to "0" and that means article is included in next mailinglist routine.
	?>
	<div>
		  <input type="checkbox" name="on_saadetud" id="on_saadetud" value="1" <?=($objekt->all['on_saadetud'] ? null : "checked");?>>
		  <label for="on_saadetud"><?=$site->sys_sona(array(sona => 'On meilinglist', tyyp=>'editor'));?></label>
	</div>
	<?php } ?>

</fieldset>

<fieldset>
	<table cellpadding="1" cellspacing="1" border="0"  style="color: #999;">
		<?php if ($objekt->all['created_user_name']) { ?>
		<?php if ($site->CONF['allow_change_position']) { ?>
		<tr>
			<td><?=$site->sys_sona(array('sona' => 'position', 'tyyp' => 'Editor'));?>:</td>
			<td>
				<input type="text" name="kesk" value="<?=$objekt->all['kesk']?>" style="width: 40px; text-align: right;">
			</td>
		</tr>
		<?php } ?>
		<tr>
			<td><?=$site->sys_sona(array('sona' => 'object_created', 'tyyp' => 'Editor'));?>:</td>
			<td><?=date('d.m.Y H:i', strtotime($objekt->all['created_time']));?></td>
		</tr>
		<tr>
			<td></td>
			<td><?=$objekt->all['created_user_name'];?></td>
		</tr>
		<?php } ?>
		<?php if ($objekt->all['changed_user_name']) { ?>
		<tr>
			<td><?=$site->sys_sona(array('sona' => 'object_changed', 'tyyp' => 'Editor'));?>:</td>
			<td><?=date('d.m.Y H:i', strtotime($objekt->all['changed_time']));?></td>
		</tr>
		<tr>
			<td></td>
			<td><?=$objekt->all['changed_user_name'];?></td>
		</tr>
		<?php } ?>
	</table>
</fieldset>




	<?php
	} // end function print_profile()
    ?>
</td></tr>
	<tr><td nowrap>Profile ID</td><td><input type="text" name="profile_id" class="scms_flex_input" value="<?php 
    echo $site->fdat['profile_id'];
    ?>
"></td></tr>
<?php 
    ####################
    # Additional info: attributes list
    # get profile
    $profile_def = $site->get_profile(array("id" => $site->fdat['profile_id']));
    $profile_fields = unserialize($profile_def['data']);
    # profile_fields is now array of ALL fields, indexes are fieldnames
    ###################
    # print profile fields rows
    print_profile_fields(array('profile_fields' => $profile_fields, 'field_values' => $rec_data, 'fields_width' => '300px'));
    ?>

        </table>
			
		</div>
    </td>
  </tr>
	<?php 
    #################### BUTTONS ###########
    ?>
	<tr> 
    <td align="right" valign="top" class="scms_dialog_area_bottom"> 
     <input type="button" value="<?php 
    echo $site->sys_sona(array(sona => "apply", tyyp => "editor"));
    ?>
/**
 * This source file is is part of Saurus CMS content management software.
 * It is licensed under MPL 1.1 (http://www.opensource.org/licenses/mozilla1.1.php).
 * Copyright (C) 2000-2010 Saurused Ltd (http://www.saurus.info/).
 * Redistribution of this file must retain the above copyright notice.
 * 
 * Please note that the original authors never thought this would turn out
 * such a great piece of software when the work started using Perl in year 2000.
 * Due to organic growth, you may find parts of the software being
 * a bit (well maybe more than a bit) old fashioned and here's where you can help.
 * Good luck and keep your open source minds open!
 * 
 * @package		SaurusCMS
 * @copyright	2000-2010 Saurused Ltd (http://www.saurus.info/)
 * @license		Mozilla Public License 1.1 (http://www.opensource.org/licenses/mozilla1.1.php)
 * 
 */
function smarty_function_init_profile($params, &$smarty)
{
    global $site, $class_path;
    include_once $class_path . 'adminpage.inc.php';
    // for print_profile_fields()
    extract($params);
    $id = (int) $id;
    if (!isset($name)) {
        $name = 'profile';
    }
    // make comma separated readonly fields into array
    if ($readonly_fields) {
        $readonly_fields = explode(',', $readonly_fields);
        // trim whitespace
        for ($i = 0; $i < sizeof($readonly_fields); $i++) {
            $readonly_fields[$i] = trim($readonly_fields[$i]);
        }
    } else {
        $readonly_fields = array();
    }
    # get all profile data from cash
    # profile name is case insensitive
    $profile = strtolower($profile);
    $profile = $site->get_profile(array('name' => $profile, 'id' => (int) $profile_id));
    # sanity check: kui ei leitud sellise nimega profiili, anda toimetajale veateade
    if (!$profile['profile_id']) {
        if ($site->admin) {
            print "<font color=red><b>Profile '" . $profile['name'] . "' not found!</b></font>";
        }
        return;
    }
    switch ($profile['source_table']) {
        case 'users':
            $source_table_id_column = 'user_id';
            break;
        case 'groups':
            $source_table_id_column = 'group_id';
            break;
        default:
            $source_table_id_column = 'objekt.objekt_id';
            break;
    }
    // if source_table is ext_ table
    if (strpos($profile['source_table'], 'ext_') === 0) {
        $source_table_id_column = 'id';
    }
    //printr($profile);
    $source_table_columns = array();
    $profile_data = unserialize($profile['data']);
    //printr($profile_data);
    $do_left_join_objekt = false;
    foreach ($profile_data as $column => $data) {
        if ($data['is_active']) {
            if ($data['is_general']) {
                $source_table_columns[] = 'objekt.' . $column;
                $do_left_join_objekt = true;
            } else {
                $source_table_columns[] = $profile['source_table'] . '.' . $column;
            }
        }
    }
    $profile_field_values = array();
    $profile_data['id'] = 0;
    foreach (array_keys($profile_data) as $key) {
        $profile_field_values[$key] = '';
    }
    if ($id) {
        $sql = 'select ' . $source_table_id_column . ' as id' . (sizeof($source_table_columns) ? ', ' . implode(', ', $source_table_columns) : '') . ' from ' . $profile['source_table'] . ($do_left_join_objekt ? ' left join objekt using(objekt_id)' : '') . ' where ' . $source_table_id_column . ' = ' . $id;
        $result = new SQL($sql);
        //printr($sql);
        if ($result->rows) {
            $profile_field_values = $result->fetch('ASSOC');
        }
    }
    $profile_field_html = print_profile_fields(array('profile_fields' => unserialize($profile['data']), 'field_values' => $profile_field_values, 'return_fields' => true, 'load_defaults' => $id ? false : true));
    $profile_out = null;
    $profile_out->all = $profile;
    $profile_out->id = $profile['profile_id'];
    $profile_out->table = $profile['source_table'];
    $profile_out->name = $profile['name'];
    $profile_out->label = $site->sys_sona(array('sona' => $profile['name'], 'tyyp' => 'custom'));
    $profile_out->title = $profile_out->label;
    $profile_out->data = array();
    $i = 0;
    foreach ($profile_data as $data) {
        if ($data['is_active']) {
            $profile_out->data[$i] = null;
            $profile_out->data[$i]->value = $data['type'] == 'DATETIME' || $data['type'] == 'DATE' ? $site->db->MySQL_ee($profile_field_values[$data['name']]) : $profile_field_values[$data['name']];
            $profile_out->data[$i]->type = $data['type'];
            $profile_out->data[$i]->is_required = $data['is_required'];
            $profile_out->data[$i]->name = $data['name'];
            $profile_out->data[$i]->label = $site->sys_sona(array('sona' => $data['name'], 'tyyp' => 'custom'));
            $profile_out->data[$i]->title = $profile_out->data[$i]->label;
            if ($site->user->all['is_readonly'] == 1) {
                if (!in_array($data['name'], $readonly_fields)) {
                    $profile_out->data[$i]->HTML = $profile_out->data[$i]->html = $profile_out->data[$i]->value;
                }
            } else {
                if (!in_array($data['name'], $readonly_fields)) {
                    $profile_out->data[$i]->HTML = $profile_out->data[$i]->html = $profile_field_html[$data['name']];
                } else {
                    $profile_out->data[$i]->HTML = $profile_out->data[$i]->html = $profile_out->data[$i]->value;
                }
            }
            $i++;
        }
    }
    //printr($profile_out);
    $smarty->assign($name, $profile_out);
}
            </div>
          </td>
        </tr>
	<?php 
            ###### profile fields row
            ?>

        <tr> 
          <td valign=top colspan="2" style="height:140px"> 
			<!-- Scrollable area -->
			<div id=listing class="scms_scroll_div">
			<table width="100%" border="0" cellspacing="0" cellpadding="0" class="scms_table">
				<?php 
            ###################
            # print profile fields rows
            print_profile_fields(array('profile_fields' => $profile_fields, 'field_values' => $user->all, 'fields_width' => "200px"));
            ?>
			</table>
			</div>
			<!-- //Scrollable area -->
          </td>
        </tr>	

	<?php 
            ###### / profile fields row
            ?>
	</table>
		<?php 
        }
        ?>
	<?php 
function edit_objekt()
{
    global $site;
    global $objekt;
    global $keel;
    global $class_path;
    if ($objekt->parent_id) {
        $parent_folder = new Objekt(array('objekt_id' => $objekt->parent_id, 'on_sisu' => 1));
    } elseif ($site->fdat['parent_id']) {
        $parent_folder = new Objekt(array('objekt_id' => $site->fdat['parent_id'], 'on_sisu' => 1));
    } elseif ($site->fdat['dir']) {
        $sql = $site->db->prepare('select objekt_id from obj_folder where relative_path = ?', '/' . $site->fdat['dir']);
        $result = new SQL($sql);
        $parent_folder = new Objekt(array('objekt_id' => $result->fetchsingle(), 'on_sisu' => 1));
    } else {
        //screwed, dont know where to put the object
        exit;
    }
    ?>
<script type="text/javascript">

var form = document.frmEdit;

// save the submit() function
form.actual_submit = form.submit;

// overwrite submit() function
form.submit = function()
{
	if(this.fileupload.value != '')
	{
		var filename = (this.fileupload.value.match(/\\/) ? this.fileupload.value.split(/\\/) : this.fileupload.value.split('/'));
		filename = filename[filename.length - 1];
		
		$.ajax({
		    url: 'ajax_response.php?rand=' + Math.random(9999),
		    data: {op: 'check_file', name: '<?php 
    echo $parent_folder->all['relative_path'];
    ?>
/' + filename},
		    type: 'POST',
		    dataType: 'json',
		    timeout: 10000,
		    error: function(){
		    },
		    success: function(response){
				if(response.file_exists && confirm('<?php 
    echo $site->sys_sona(array('sona' => 'overwrite_file', 'tyyp' => 'Admin'));
    ?>
'))
				{
					// use the original submit function
					form.actual_submit();
				}
				else if(!response.file_exists)
				{
					// use the original submit function
					form.actual_submit();
				}
				else
				{
					document.body.style.cursor = 'default';
				}
		    }
		});
	}
	else
	{
		// use the original submit function
		form.actual_submit();
	}
}

function setPealkiri (strPealkiri) {
	var algus=0;
	var saved_algus=0;
	var p_elem = document.getElementById('pealkiri');
	var filename = document.getElementById('filename');
	filename.value = strPealkiri;
	
	// \ to /
	strPealkiri = strPealkiri.replace(/\\/g, '/');
	// strip out file extension
	strPealkiri = strPealkiri.replace(/\.[^\.]*$/g, '');
	// strip out file path
	strPealkiri = strPealkiri.replace(/^(.*)\//g, '');
	
	p_elem.value = strPealkiri;
}
</script>

	<input type="hidden" name="file" value="<?php 
    echo $site->fdat['file'];
    ?>
">
	<input type="hidden" name="dir" value="<?php 
    echo $site->fdat['dir'];
    ?>
">
	<input type="hidden" name="parent_id" value="<?php 
    echo $parent_folder->objekt_id;
    ?>
">
	<input type="hidden" name="in_wysiwyg" value="<?php 
    echo $site->fdat['dir'] ? 1 : 0;
    ?>
">
	<input type="hidden" name="publish" value="1"> <?php 
    ### Bug #2321
    ?>

    <input type="hidden" name="sorting" value="<?php 
    echo $site->fdat['sorting'];
    ?>
">

	<input type="hidden" name="callback" value="<?php 
    echo $site->fdat['callback'];
    ?>
">

	<?php 
    ################ upload file
    ?>

		<tr>
          <td width="20%" nowrap><?php 
    echo $site->sys_sona(array(sona => "Upload", tyyp => "files"));
    ?>
:</td>
		  <input type="hidden" name="filename" id="filename" value="">
          <td width="80%" nowrap>
            <input name="fileupload" id="fileupload" onChange="setPealkiri(this.value)" type="file" class="scms_flex_input" style="border:0;">
          </td>
			<?php 
    #################### thumbnail
    ?>
			<td rowspan="2" align="center" valign="middle" style="padding-right:15px">
				<?php 
    if ($objekt->objekt_id && $objekt->all['relative_path']) {
        //Find the correct thumbnail
        $thumbnail_file = str_replace($objekt->all['filename'], '.thumbnails/' . $objekt->all['filename'], $objekt->all['relative_path']);
        $thumbnail_path = preg_replace('#/$#', '', $site->absolute_path) . $thumbnail_file;
        $thumbnail_url = $site->CONF['wwwroot'] . $thumbnail_file;
        $thumb = $thumbnail_url;
        $img_href = $site->CONF['wwwroot'] . '/file.php?' . $objekt->objekt_id;
        $i_width = 10;
        $i_height = 10;
        ?>
                <!-- Thumbnail -->
				<a href="javascript:void(openpopup('<?php 
        echo $img_href;
        ?>
', 'popup',<?php 
        echo $i_width;
        ?>
, <?php 
        echo $i_height;
        ?>
, 'yes'))"><IMG SRC="<?php 
        echo $thumb;
        ?>
" BORDER="0" ALT=""></a>
				<!-- // Thumbnail -->   
				<?php 
    }
    ?>
               </td>
        </tr>


	<?php 
    ################ filename
    ?>
	  <tr>
			<td width="20%" nowrap valign="top"><?php 
    echo $site->sys_sona(array(sona => "filename", tyyp => "files"));
    ?>
:</td>
			<td width="100%" valign="top">
				<?php 
    echo $objekt->all['filename'];
    ?>
			</td>
		</tr>

	</table>

    <!-- Profile fields -->
	<?php 
    ############################ PROFILE TABLES
    $sql = $site->db->prepare("SELECT profile_id AS id, source_table AS parent, name FROM object_profiles WHERE source_table=? ORDER BY name", 'obj_file');
    $sth = new SQL($sql);
    # get object profile
    if ($objekt->all['profile_id']) {
        $profile_def = $site->get_profile(array("id" => $objekt->all['profile_id']));
    } elseif ($site->fdat['profile_id']) {
        $profile_def = $site->get_profile(array("id" => $site->fdat['profile_id']));
    }
    # if still not found then use default profile for this class
    if (!$profile_def['profile_id']) {
        $site->fdat['profile_id'] = $site->get_default_profile_id(array(source_table => 'obj_file'));
        $profile_def = $site->get_profile(array("id" => $site->fdat['profile_id']));
    }
    $site->fdat['profile_id'] = $profile_def['profile_id'];
    ?>
		<br />
		<table width="100%"  border="0" cellspacing="3" cellpadding="0" class="scms_borderbox">

		<tr>
			<td colspan="2">
            <div style="position:relative">
              <div class="scms_borderbox_label">

              <SELECT onchange="changeProfile(this)" NAME="profile_id" class="scms_flex_input" style="width:120px">
				<?php 
    $all_profiles_hash = array();
    while ($profile_data = $sth->fetch()) {
        $all_profiles_hash[] = $profile_data['id'];
        print "<option value='" . $profile_data['id'] . "' " . ($profile_data['id'] == $site->fdat['profile_id'] ? '  selected' : '') . ">" . $site->sys_sona(array(sona => $profile_data['name'], tyyp => "custom")) . "</option>";
    }
    ?>
				</SELECT>
			</div>
	<?php 
    ##### hidden field "profile_id" if selectbox is disabled OR module "Profile" is not allowed
    if ($site->fdat['profile_locked']) {
        ?>
		<input type="hidden" name="profile_id" value="<?php 
        echo $site->fdat['profile_id'];
        ?>
">
	<?php 
    }
    ?>
            </div>
          </td>
        </tr>
	<?php 
    ###### profile fields row
    ?>
		<tr>
			<td valign=top colspan="2" style="height:130px">
	<?php 
    #################
    # Loop throug all profiles
    foreach ($all_profiles_hash as $profile_id) {
        ?>
	<!-- Scrollable area -->
	<div id="profile_<?php 
        echo $profile_id;
        ?>
" class="scms_scroll_div" style="display: <?php 
        echo $site->fdat['profile_id'] == $profile_id ? 'block' : 'none';
        ?>
; height: 130px">

		<table width="90%" border=0 cellspacing=0 cellpadding=0>
			<tr><td colspan=2>&nbsp;</td></tr>
		<?php 
        $profile_def = $site->get_profile(array("id" => $profile_id));
        $profile_fields = unserialize($profile_def['data']);
        # if profile fields exist
        if (is_array($profile_fields) && sizeof($profile_fields) > 0) {
            ## add suffix for each field, to get unique id-s
            foreach ($profile_fields as $key => $tmp_prof) {
                $profile_fields[$key]['html_fieldname'] = $profile_fields[$key]['name'] . "_" . $profile_id;
            }
            #printr($profile_fields);
            ###################
            # print profile fields rows
            print_profile_fields(array('profile_fields' => $profile_fields, 'field_values' => $objekt->all, 'fields_width' => '300px'));
        }
        # if profile fields exist
        ?>
		</table>
	</div>
	<?php 
    }
    //foreach
    ?>
	</td>
	</tr>
	<!-- //Profile fields -->
	<?php 
    ############################ / PROFILE TABLES
}