Location: PHPKode > projects > Netautor Professional > netautor/napro4/admin/dtd/db_action.php
<?php
/* +----------------------------------------------------------------------+
   | Netautor Professional Application Server                             |
   +----------------------------------------------------------------------+
   | Copyright (C) 1998-2005 digiconcept GmbH. <www.digiconcept.net>      |
   +----------------------------------------------------------------------+
   | This file is subject to license, that is bundled with this package   |
   | in the file LICENSE.TXT, and is available at through the             |
   | world-wide-web at http://www.netautor.com/license/                   |
   |                                                                      |
   | If you did not receive a copy of the license and are unable to       |
   | obtain it through the world-wide-web, please send a note to          |
   | hide@address.com so we can mail you a copy.                      |
   +----------------------------------------------------------------------+
   | Authors: Stefan Rottensteiner <hide@address.com>    |
   |          Marek  Kadziela      <hide@address.com>	      |
   |          Gregor Wollner                                              |
   |          Christian Unger                                             |
   |          Helli  Kleinhans  									      |
   +----------------------------------------------------------------------+*/

   /**
    * @version $Revision: 1.12 $
    */
 	require_once('../../include/init.inc');
	if(!$USER->check_feature('Admin') && !$USER->check_feature('ac_schema'))
	{
		$GLOBALS['USER']->login_call();
	}

/************************************************************
* db_action include for dbmanager							*
************************************************************/

	//	---	include necessary functions
	//includeNaPro( 'mdb,array' );

	$AllowTypeChange = true;
	$OldTypeIndex	 = 0;

	switch ($todo)
	{
		/* Create or update Netautor Schema */
		case 'save_table':

			$table_def['mn_name'] 	= $mn_name;
			$table_def['mn_remark'] = $mn_remark;
			$table_def['mn_logs'] 	= $mn_logs;

			if(empty($mn_naid))
			{	/* create a new netautor database */
				$result 	= mdb_create_db($sql_world,$table_def);
				
				if($result > 0)
				{
					$erg 		= $sql_world->select("SELECT mn_id FROM mnames WHERE mn_naid = $result",0,1,2);
					$mn_id 		= $erg[0]['mn_id'];
					$mn_naid 	= $result;
				}
				$message 	= ($result ? $GLOBALS['LOCALE']->gettext('msg_table_created') : $sql_world->errstr);
			}
			else
			{	/* update netautor database */
				$table_def['mn_naid'] = $mn_naid;

				$result 	= mdb_alter_db($sql_world,$mn_naid,$table_def);
				$message 	= ($result?$GLOBALS['LOCALE']->gettext('msg_table_altered'):$sql_world->errstr);
			}
		break;
		case 'delete_table':
			/* delete a netautor database with all elements, documents and  layouts*/
			$result 	= mdb_drop_db($sql_world,$mn_naid);
			$message 	= ($result?$GLOBALS['LOCALE']->gettext('msg_table_deleted'):$sql_world->errstr);
		break;
			/* delete all documentes concering this netautor database */
		case 'clear_table':
			includeNaPro('mdoc');
			$result 	= mdoc_clear_database($sql_world,$mn_naid);
			$message 	= ($result?$GLOBALS['LOCALE']->gettext('msg_table_cleared'):$sql_world->errstr);
		break;
	}


	/**
	 * Hilfsfunktion, deren bedeutung angezweifelt wird und keiner mehr wirklich weiss
	 */
	function bereichsauswahl(&$sql_world,$me_mn_naid,$me_me_id='')
	{
		$script='';
		$erg= mdb_list_elements($sql_world,$me_mn_naid,true);

		@$me_me_id = (empty($me_me_id)?key($erg['me_me_id']):$me_me_id);

		$script="myform.elements['me_me_id'].options.length=0;";
		$i=0;

		if (!empty($erg['me_id']))
		{
			foreach($erg['me_id'] as $me_name => $me_id)
			{
				$script.="add_new('me_me_id','".$me_name."','".$me_id."');\r";
				if($me_me_id  == $me_id) 
				{
					$script.=" myform.elements['me_me_id'].selectedIndex = ".$i.";\r";
					$found=1;
				}
				$i++;
			}
		}
		if (!isset($found)) $script.=" myform.elements['me_me_id'].selectedIndex = 0;\r";
		return $script;
	}


	//--- ELEMENT LÖSCHEN ------------------------------
	if($todo=='delete_element' && !empty($me_id))
	{
		$result = mdb_drop_element($sql_world,$me_id);

		if($result)
		{
			unset($me_id);
			unset($me_me_id);
			$todo		='update_db_struktur';
			$message 	= $GLOBALS['LOCALE']->gettext('msg_field_deleted');
		}
		else
		{
			$todo		='';
			$message 	= $DC_ENV->message[0];
		}
	}

	//--- ELEMENT SPEICHERN ------------------------------
	if($todo=='save_element' && !empty($me_me_id))
	{

		// Alles was nicht A-Z,_ oder 0-9 ist und länger als 20 Zeichen weg
		@$me_name	= trim(substr(ereg_replace('[^A-Z_0-9]','',strtoupper($me_name)),0,20));
		// Bemerkung setzten
		$me_remark 	= empty($me_remark)?$me_name:trim($me_remark);
		$fobidden_names = array ('TABLE','SELECT','AS','FROM','WHERE','ORDER','AND','OR','COUNT','DROP','INSERT','TRUNCATE','CREATE','DATABASE','DELETE');

		if(in_array($me_name,$fobidden_names) || empty($me_name) )
		{
			$message =  $GLOBALS['LOCALE']->gettext('msg_invalid_element_name');
		}
		else
		{
			if(!empty($me_autoinc) && $me_content != 'num')
			{
				$me_autoinc = '';
				$script.="alert('Use autoinc only on number!');";
			}

			$my_me_content = ($me_content == 'area' ? '' : $me_content);

			$element_def['me_mn_naid']		= $me_mn_naid;
			$element_def['me_content']		= $my_me_content;
			$element_def['me_name']			= $me_name;
			$element_def['me_remark']		= $me_remark;			
			$element_def['me_me_id']		= $me_me_id;
			$element_def['me_mandatory']	= (empty($me_mandatory)	?0:1);
			$element_def['me_multiple']		= (empty($me_multiple)	?0:1);
			$element_def['me_doc_title']	= (empty($me_doc_title)	?0:1);
			$element_def['me_index']		= (empty($me_index)		?0:1);
			$element_def['me_unique']		= (empty($me_unique)	?0:1);
			$element_def['me_autoinc']		= ( (!empty($me_autoinc) && $me_content=='num') ? 1 : '' );
			$element_def['me_settings']		= unserialize($mv_values);

			if(!empty($element_def['me_settings']['max_length']))
			{
				$element_def['me_length'] = $element_def['me_settings']['max_length'];
			}

			if (!empty($element_def['me_settings']['default_save']))
			{
				$element_def['me_default'] = $element_def['me_settings']['default_save'];
			}

			if(empty($me_id))
			{ 	// Neues Element anlegen
				$result		=	mdb_add_element($sql_world,$me_mn_naid,$element_def);
				if ( !empty($result) && $result > 0 )
				{
					$message 	= $GLOBALS['LOCALE']->gettext('msg_field_created');
					$me_id		= $result;
					$element_def['me_id']	= $me_id;
				}
				else
				{
					$message 	= $GLOBALS['LOCALE']->gettext('msg_field_created_err',array($result));
				}
			}
			else
			{	// Element ändern
				$params					= array();
				$element_def['me_id']	= $me_id;

				$result		=	mdb_alter_element($sql_world,$me_mn_naid,$element_def,$params);
				$message 	=  	(!empty($result) && $result > 0 ? $GLOBALS['LOCALE']->gettext('msg_field_altered') : $GLOBALS['LOCALE']->gettext('msg_field_altered_err',array($result)) );
			}

			$todo = 'update_db_struktur';
		}
	}

	//--- FORMULAR LÖSCHEN ------------------------------
	if($todo=='new')
	{
		$me_id			='';
		$me_name		='';
		$me_remark		='';
		$me_content		='';
		$me_index		='';
		$me_unique		='';
		$me_mandatory	='';
		$me_multiple	='';
		$mv_value		='';
	}

	//--- ELEMENT AUSWÄHLEN ------------------------------
	if($todo=='click_db_item')
	{
		if(!empty($me_id))
		{

			$erg 	= mdb_get_element($sql_world,$me_id,true);

			extract($erg);
			if(empty($me_content)) $me_content='area';

			//wert für die auswahlliste der felder setzen
			$me_mn_naid_1 = $me_mn_naid;
			$mv_values = serialize($me_settings);
		}
		else
		{
			$mn_naid = trim(substr($db_item,strpos($db_item,'=')+1));
		}
	}
?>
Return current item: Netautor Professional