Location: PHPKode > projects > Site-Assistant > sas/sas/plug.class.php
<?php
/*******************************************************************************
Basisklasse Plug-In
*******************************************************************************/

class plug {
var $site;
var $cache_output = TRUE;

function init($site) {
	$this->site = $site;
}

function label() {
	if ($this->label) return $this->label;
	else return get_class($this);
}

function table_exist() {
	if ($this->property or $this->add_table_def) return TRUE;
	else return FALSE;
}

// Hat das Plug ein Formular zur Bestimmung der Parameter
function form_exist() {
	return (count($this->form));
}

function table_name() {
	// Tabellenname ist per default einfach Klassenname incl. prefix
  	global $database;
	return $database["prefix"] . "_" . get_class($this);
}

function name() {
	// Tabellenname ist per default einfach Klassenname
	return get_class($this);
}

function get_table_def() {
	$table_def = "(";
	// Datenfelder mit Typangabe hinzufügen
	foreach ($this->property as $property_name => $property_params) {
		if ($property_params['type'])
			$table_def .= "$property_name $property_params[type], ";
	}
	$table_def = substr($table_def, 0, strlen($table_def)-2);

	// Zusätzliche Angaben hinzufügen
	if ($this->add_table_def) $table_def .= ", " . $this->add_table_def;

	$table_def .= ")";
	return $table_def;
}

function output($content_id, $content_list, $mode) {
	// Wird nur aufgerufen, wenn (z.B. während der Entwicklung) das Plug keine eigene Ausgabe hat!
	$values = $this->get_content_params($content_id);
	// Wenn keine Parameter gespeichert sind
	if ($values['instance_id'] == -1) return "Ausgabe " . $this->label();
	// Ausgabe aller Eigenschaften mit Bezeichnung
	foreach ($this->property as $property_name => $property_params) {
		if ($property_params['label']) {
			$str .= $values[$property_name] . "<br />";
		}
	}
	return $str;
}

function output_print($content_id, $content_list, $mode) {
	return $this->output($content_id, $content_list, $mode);
}

function admin_title($content_id, $form_id, $plug_edit){
	GLOBAL $REQUEST_URI,$page_id;
	// Gibt den Titel eines Plugs zurück
	if (isset($this->form[$form_id]['label'])) $form_label = $this->form[$form_id]['label'];
	else $form_label = "";
	if ($plug_edit) $href = "href='index.php?query=content_form&amp;page_id=$page_id&content_id=$content_id&form_id=$form_id'";
	else $href = "";

	if ($form_id == "main") {
		$link = "$this->label";
		if ($form_label) {
			$link .= $form_label;
		}
	}
	else {
		if (!$form_label) $form_label = "Formular " . ucfirst($form_id);
		$link = $form_label;
	}
	$str = "<a $href title='Element $this->label bearbeiten' class='red'>$link</a>";
	return $str;
}

function admin_properties($form_id, &$values){
	global $core;
	$str = "";
	for ($i = 0; $i < count($this->form[$form_id]['property']); $i++) {
		$property_name = $this->form[$form_id]['property'][$i];
		$property_params = $this->property[$property_name];
	  	if ($property_params['label'] AND isset($values[$property_name])) {
	  		$str .= "<span style='font-size:8pt;color:#00233A'>$property_params[label]</span> - &nbsp;";
	  		// Muß Wert als Option angezeigt werden?
	  		$str .= "<span style='font-size:8pt;color:004573;'>";
	  		if (! isset($property_params['options']))
	  			$str .= htmlspecialchars($core->cutstr($values[$property_name], 90)) . "<br />";
	  		else {
				$options = explode("|", $property_params['options']);
	  			$str .= $options[$values[$property_name]] . "<br />";
	  		}
	  		$str .= "</span>";
	  	}
  	}
	return $str;
}

function admin_options($content_id){
	return FALSE;
}

function new_site($content_id, $plug_edit,$position) {
	global $tbl_contents,$core,$page_id;
	$str = "<div style=\"margin:3px;border-top:1px solid black;\">";
	$row = $core->sv_query("SELECT position, instance_id, db_page_area, created, edited FROM $tbl_contents WHERE id=$content_id");

	// Sind Daten für plug gespeichert
	if (!isset($instance_id)) $instance_id = "";
	if ($instance_id == -1) {
		$str .= "Keine Daten für $this->label gespeichert.";
		return $str;
	}

	// Werte auslesen
	$values = $this->get_params($row['instance_id']);
	$str .= "".$position.". - \n\n";
	foreach ($this->form as $form_name => $form_params) {
		$str .= " [ ".$this->admin_title($content_id, $form_name, $plug_edit)."  ] \n\n";
	}

	if (!empty($page_id)) $page = $page_id;
	else $page = 1;

	// Formular verschieben oder löschen
	if ($plug_edit and $this->site->content_struct_editable) {
   	$cnt = $core->sv_query("select count(*) from $tbl_contents where page_id=$page and db_page_area=".$row['db_page_area']);
   	if ($cnt > 1) $str .= "<form style='float:left;' action='index.php?query=content_move&content_id=$content_id' method='post'>\n
   	<fieldset>\n
  	<input title='Tragen Sie in dieses Feld die gewünschte neue Position ein' name='new_position' size='4' /> \n
  	<input type='hidden' name='page_id' value='$GLOBALS[page_id]' />\n
  	<input type='submit' value='Verschieben' title='$this->label auf die gewünschte Position in dieser Spalte verschieben' />\n
  	</fieldset></form>\n";

  	$str .= "<form style='float:left;' action='index.php?query=content_delete&content_id=$content_id&page_id=$page' method='post'>\n
  	<fieldset>\n
  	<input type='hidden' name='page_id' value='$GLOBALS[page_id]' />\n
  	<input type='submit' value='löschen' title='$this->label löschen' />\n
  	</fieldset></form>\n";
	}

	$str .= "<div style=\"clear:both;\"></div></div>\n";

	return $str;
}
function new_admin($content_id, $plug_edit,$position) {
	global $tbl_contents,$core,$page_id;
	$str = "";
	$row = $core->sv_query("SELECT position, instance_id, db_page_area, created, edited FROM $tbl_contents WHERE id=$content_id");

	// Sind Daten für plug gespeichert
	if (!isset($instance_id)) $instance_id = "";
	if ($instance_id == -1) {
		$str .= "Keine Daten für $this->label gespeichert.";
		return $str;
	}

	// Werte auslesen
	$values = $this->get_params($row['instance_id']);

	foreach ($this->form as $form_name => $form_params) {
		$str .= "<h2 class='red'>".$position.".";
		$str .= $this->admin_title($content_id, $form_name, $plug_edit)."</h2>\n\n";
		$str .= "<div class='properties'>".$this->admin_properties($form_name, $values)."</div>\n\n";
	}

	$options = $this->admin_options($content_id);

	if ($options and $plug_edit)
		for ($i = 0; $i < count($options); $i++) {
			$str .= "<div class='options'>".$options[$i]."</div>\n\n";
		}

	if (!empty($page_id)) $page = $page_id;
	else $page = 1;

	// Formular verschieben oder löschen
	if ($plug_edit and $this->site->content_struct_editable) {
   	$cnt = $core->sv_query("select count(*) from $tbl_contents where page_id=$page and db_page_area=".$row['db_page_area']);
   	$str .= "<div id='plug_edit'>";
   	if ($cnt > 1) $str .= "<form style='float:left;' action='index.php?query=content_move&content_id=$content_id' method='post'>
   	<fieldset>
  	<input title='Tragen Sie in dieses Feld die gewünschte neue Position ein' name='new_position' size='4' />
  	<input type='hidden' name='page_id' value='$GLOBALS[page_id]' />
  	<input type='submit' value='Verschieben' title='$this->label auf die gewünschte Position in dieser Spalte verschieben' />
  	</fieldset></form>\n";

  	$str .= "<form style='float:left;' action='index.php?query=content_delete&content_id=$content_id&page_id=$page' method='post'>
  	<fieldset>
  	<input type='hidden' name='page_id' value='$GLOBALS[page_id]' />
  	<input type='submit' value='löschen' title='$this->label löschen' />
  	</fieldset></form></div>\n";
	}

	$str .= "<p style='margin:0px;padding:1px;clear:both;'><span style='font-size:0.8em;font-weight:normal;'>erstellt</span> ";
	if (!empty($row['created'])) $str .= $row['created'];
	if (!empty($row['edited'])) $str .= "<br /> <span style='font-size:0.8em;font-weight:normal;'>geändert</span> ".$row['edited'];
	$str .= "</p></div>";

	return $str;
}

function form_init_values(&$values, $form_id) {
}

function form_prepare($instance_id, &$values, $form_id) {
}

// Formular mit $instance_id als Parameter
function form($instance_id, $form_id = "main", $add_param = "") {
	$this->form_instance_id = $instance_id;
	if ($instance_id > 0) {
		$values = $this->get_params($instance_id);
		$this->form_prepare($instance_id, $values, $form_id);
	}
	else $this->form_init_values($values, $form_id);
	return $this->show_form($values, $form_id, $add_param);
}

// Formular mit ausgelesenen Werten als Parameter
function show_form($values, $form_id = "main", $add_param = "") {
	$str  = "";
	for ($i = 0; $i < count($this->form[$form_id]['property']); $i++) {
		$property_name = $this->form[$form_id]['property'][$i];
		$property_params = $this->property[$property_name];
		$str .= $this->show_form_element($property_name, $values[$property_name], $form_id);
	}

	if (isset($this->form_notice)) $str .= "<p class='formNotice'><strong>Hinweis:</strong> " . $this->form_notice . "</p>";

	return $str;
}

function show_form_element($property_name, &$value, $form_id = "main") {
	global $core,$start_vars;
		$property_params = $this->property[$property_name];
		if ($property_params['save_as'] == 'none') return "";

		$str = "<p class='controlHeader'>" . $property_params['label'] . "</p>";
		// Steuerelemente nach Typdefinition anzeigen
		if (eregi("tinyint", $property_params['type']) and $property_params['options']) {
			// Optionen in Array auslesen und als Radio-Buttons anzeigen
			$options = explode("|", str_replace(" ", "&nbsp;", $property_params['options']));
			$cnt_options = count($options);
			// Bei Radio-Buttons immer mindestens eine Auswahl selektieren
			if ($value == "") $value = "0";

			for ($t = 0; $t < $cnt_options; $t++) {
				$checked = "";
				if ($value == $t)
					$checked = "checked";
				$str .= "<input type='radio' name='$property_name' value='$t' $checked>$options[$t]&nbsp; &nbsp; ";
			}

		}
		elseif (eregi("text", $property_params['type'])) {
			if (!isset($property_params['rows'])) $rows = 15;
      		else $rows = $property_params['rows'];

        	if ($property_name == "content" || $property_name == "description") {

	$oFCKeditor = new FCKeditor('FCKeditor1') ;
	$oFCKeditor->Value	= $value;
	$str .= $oFCKeditor->Create() ;
}
else $str .= "\n<p><textarea id='$t_id' name='$property_name' cols='68' rows='$rows'>$value</textarea></p>\n";

    		}
		elseif (eregi("int|varchar", $property_params['type'])) $str .= "<p><input name='$property_name' value='$value' size='80'></p>";
		return $str;
}

function form_test(&$POST_VARS, $error_text) {
	return TRUE;
}

function save(&$POST_VARS, $form_id = "main") {
	global $core;
	// SQL Befehl für Insert zusammensetzen
	$sql = "INSERT INTO " . $this->table_name();
	$cols = "(";
	$values = "VALUES(";
	global $lang;

	// Parameter hinzufügen
	for ($i = 0; $i < count($this->form[$form_id]['property']); $i++) {
		$property_name = $this->form[$form_id]['property'][$i];
		$property_params = $this->property[$property_name];

		if ($property_params['save_as'] == "number") {
			$cols .= "$property_name, ";
			$values .= $POST_VARS[$property_name] . ", ";
		}
		elseif ($property_params['save_as'] == "string") {
			$cols .= "$property_name, ";
			$values .= "'" . $POST_VARS[$property_name] . "', ";
		}
	}
	$cols = substr($cols, 0, strlen($cols)-2) . ")";
	$values = substr($values, 0, strlen($values)-2) . ")";
	$sql = "$sql $cols $values";

	$core->query ($sql);

	return mysql_insert_id();
}

function dco_update(&$values, $instance_id) {
	// SQL Befehl für Update zusammensetzen
	$sql = "UPDATE " . $this->table_name() . " SET ";

	// Parameter hinzufügen
	foreach ($values as $property_name => $value) {
		$property_params = $this->property[$property_name];

		if ($property_params['save_as'] == "number")
			$sql .= "$property_name = " . $value . ", ";
		elseif ($property_params['save_as'] == "string")
			$sql .= "$property_name = '" . $value . "', ";
	}
	$sql = substr($sql, 0, strlen($sql)-2) . " WHERE id=$instance_id";
	query($sql);
}

function update(&$POST_VARS, $instance_id, $form_id = "main") {
	global $core;
	// SQL Befehl für Update zusammensetzen
	$sql = "UPDATE " . $this->table_name() . " SET ";
	global $database;

	// Parameter hinzufügen
	for ($i = 0; $i < count($this->form[$form_id]['property']); $i++) {
		$property_name = $this->form[$form_id]['property'][$i];
		$property_params = $this->property[$property_name];

		if ($property_params['save_as'] == "number")
			$sql .= "$property_name = " . $POST_VARS[$property_name] . ", ";
		elseif ($property_params['save_as'] == "string")
			$sql .= "$property_name = '" . $POST_VARS[$property_name]. "', ";
	}

	$sql = substr($sql, 0, strlen($sql)-2) . " WHERE id=$instance_id";
	$core->query($sql);

}

function get_content_editable($content_id){
	global $HTTP_SESSION_VARS;

	$arr = $this->get_content_params($content_id);
	if (!empty($_SESSION['dco_enabled']) and $this->content_list->plug_edit) {

 		$constant_attr = "contentEditable='true' class='editFrame'";
 		foreach ($arr as $property_name => $value) {
			if ($this->property[$property_name]['editable'] and $arr[$property_name]) {
				$a_id = "dco_" . $content_id . "_$property_name";
				$attr = "id='$a_id' onFocus=\"document.all['$a_id'].className = 'actEditFrame';\" onBlur=\"document.all['$a_id'].className = 'editFrame';\" $constant_attr";
				$arr[$property_name] = "<div $attr>$arr[$property_name]</div>";
			}
		}
	}
	return $arr;
}

function get_content_params($content_id){
	global $tbl_contents,$core;
	$instance_id = $core->sv_query("SELECT instance_id FROM $tbl_contents WHERE id=$content_id");
	if ($instance_id != -1)	$arr = $this->get_params($instance_id);
	$arr['instance_id'] = $instance_id;
	return $arr;
}

function get_params($instance_id) {
	global $core;
	$cols = "id, ";
	foreach ($this->property as $property_name => $property_params) {
		$cols .= $property_name . ", ";
	}
	$cols = substr($cols, 0, strlen($cols)-2);
	$sql = "SELECT $cols FROM " . $this->table_name() . " WHERE id=$instance_id";
	return $core->sv_query($sql);
}

function delete($content_id){
	global $tbl_contents, $paths, $database,$core;

	extract(sv_query("SELECT page_id, db_page_area, position, plug_name, instance_id FROM $tbl_contents WHERE id = $content_id"));
	//eingefügt um pluginhalt auch zu löschen
	$pre = $database["prefix"];
	$plug_tbl =  $pre."_".$plug_name;
	$core->query("DELETE FROM $plug_tbl WHERE id = $instance_id");
	$core->delete_position($position, $db_page_area, $page_id);
	$core->query("DELETE FROM $tbl_contents WHERE id = $content_id");
}

function delete_irreversible($content_id){
	global $database, $paths,$core;
	$tbl_contents = $database["prefix"]."_contents";
	extract($core->sv_query("SELECT page_id, db_page_area, position FROM $tbl_contents WHERE id = $content_id"));
	$core->delete_position($position, $db_page_area, $page_id);
	$core->query("DELETE FROM $tbl_contents WHERE id = $content_id");
}

function delete_all_irreversible() {
	// Alle Inhalte werden endgültig gelöscht
	// private Tabelle bleibt erhalten
	global $database;
	$tbl_contents = $database["prefix"] . "_contents";

	// Bestehende Inhalte aus contents löschen
	$result = query("SELECT id FROM $tbl_contents WHERE plug_name = '" . get_class($this) . "'");
	while ($row = mysql_fetch_array($result))
		$this->delete_irreversible($row['id']);

	// Problem falls bei Aufruf durch register Tabelle noch nicht existiert!!!
	// query("DELETE FROM " . $this->table_name());
}

function register() {
	// Registrierung des Plugs
	// echo "<h2>Plug $this->label wird registriert. (von Objekt plug)</h2>";
	// Bestehende Inhalte aus contents löschen
	$this->delete_all_irreversible();
	query("DROP TABLE IF EXISTS " . $this->table_name());
	if ($this->table_exist()) {
		$sql = "CREATE TABLE " . $this->table_name() . " " . $this->get_table_def();
		// echo $sql;
		query($sql);
	}
}

}

?>
Return current item: Site-Assistant