Location: PHPKode > projects > Drafter classes > drafter-classes/class_drafter.php
<?php
/**
 *	Drafter
 *
 * This class offers a 'white board' where multiple people can
 * write messages.
 * Its intended to build TO-DO lists, Brainstorm ideas, some
 * kind of idea repository or so...
 *
 * As it doesn't have (for the moment) very security elements
 * (userlist, etc), it is intended to be used in a secured
 * directory by a team of people (or one alone)
 *
 * @version 1.0
 * @author Juan Prada
 *
 */


include_once 'funkpack_drafter.php';

class Drafter {
	// Drafter system data
	var $data = array();



	/**
	 * Constructor
	 */
	function Drafter($a_data='') {
		$this->data = $a_data;

		// Tries to retrieve the browser's language
		$ls_lang = $this->data['lang'];
		if (($ls_lang != 'en') && ($ls_lang != 'es')) {
			$ls_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
			if (($ls_lang != 'en') && ($ls_lang != 'es')) $ls_lang = 'en';
		}
		include_once($this->data['classpath'].'lang.'.$ls_lang.'.php');
	}


	/**
	 * Unique call for doing it all
	 */
	function run() {
		global $fndrf;

		$fndrf->db_login($this->data['dblogin']);

		if (!$fndrf->db_comprueba_tabla($this->data['dbtabla'])) {
			$this->of_db_create_tables();
		}

		$this->of_operation();
		echo $this->of_paint_all();

		$fndrf->db_desconectar();
	}



	/**
	 * Does the basic operation of the utility
	 */
	function of_operation() {
		global $fndrf;

		// Logo
		$this->data["primero"] = $fndrf->f_draw_link("?","<b>".$this->data['titulo']."</b>")."<br>".$this->data['descrip'];



		// Coming from editing a message, saves it
		if (isset($_POST["cb_edituna"])) {
			unset($_GET["msgdelete"]);
			unset($_GET["msgedit"]);
			unset($_GET["msgnuevo"]); // Remove this if want to create a new one
			//echo "grabamos mensaje {$_POST["id"]}";
			$this->of_mensaje_modificar($_POST);
		}


		// Pressed new messaje, edit the existing or duplicate it
		if (isset($_GET["msgnuevo"])) {
			unset($_GET["msgedit"]);
			if ($_GET["msgnuevo"] > 0) {
				$_GET["msgedit"] = $this->of_mensaje_crea_basado_en($_GET["msgnuevo"]);
			} else {
				$this->data["contenidos"] .= $this->of_mensaje_listar_nuevo();
			}
		}


		// Presed delete the message
		if ($_GET["msgdelete"] > 0) {
			unset($_GET["msgedit"]);
			$this->of_mensaje_borra($_GET["msgdelete"]);
		}


		// Prepare the contents area
		$this->data["contenidos"] .= $this->of_mensajes_listar($_GET["msgedit"]);


		// Upper menu
		$this->data["superior"] = $this->of_mensajes_menu_principal();

	}




	/**
	 *
	 *	Paint all the HTML
	 *
	 */
	function of_paint_all() {

	$ss = '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>'.$this->data['pagina_titulo'].'</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="drafter.css" rel="stylesheet" type="text/css">
</head>

<body class="texto">
<table width="100%"  border="0" class="texto">
  <tr>
    <td width="'.$this->data['menu_ancho'].'" height="'.$this->data['menu_alto'].'" align="left" valign="top" class="texto">
	 '.$this->data["primero"].'
	 </td>
    <td align="left" valign="top" class="texto">'.$this->data["superior"].'</td>
  </tr>
  <tr>
    <td align="left" valign="top" class="texto">'.$this->data["lateral"].'</td>
    <td align="left" valign="top" class="tdcontenidos">'.$this->data["contenidos"].'</td>
  </tr>
</table>
</body>
</html>
	';
	return $ss;
	}






	//
	//	of_draw_mensaje_ver()
	//
	//	Draw message in view mode
	//
	//	$as_datos	Array with all data, fields and other values
	//
	function of_draw_mensaje_ver($as_datos) {
		global $fndrf;

		array($l_text);

		$l_text["arriba"] =
								$fndrf->f_draw_td($as_datos["titulo"], "titulo", "", "").
								$fndrf->f_draw_td($as_datos["fecha"], "texto", "", "130").
								$fndrf->f_draw_td($as_datos["autor"], "texto", "", "110").
								$fndrf->f_draw_td($as_datos["orden"], "texto", "", "20")
								;

		$l_text["abajo"] =
								$fndrf->f_draw_td($fndrf->f_string_parseBBCode(nl2br($as_datos["texto"])), "texto");

		$l_text["mas"] =
								$fndrf->f_draw_td(
									$fndrf->f_draw_link("?msgedit={$as_datos["id"]}", _DRFT_EDIT_BOTON_, _DRFT_EDIT_TXT_)."&nbsp;".
									$fndrf->f_draw_link("?msgnuevo={$as_datos["id"]}", _DRFT_DUPL_BOTON_, _DRFT_DUPL_TXT_)."&nbsp;"
								,"texto").
								$fndrf->f_draw_td(
									$fndrf->f_draw_link("?msgdelete={$as_datos["id"]}", _DRFT_ELIM_BOTON_, _DRFT_ELIM_TXT_,'','',' onClick="return confirm(\''._DRFT_ELIM_SEG_.'\');"')."&nbsp;",
									"texto", 'align="right"'
								);

		return $this->of_draw_td_mensaje($l_text);
	}



	//
	//	of_draw_mensaje_editar()
	//
	//	Shows the message in edit mode
	//
	//	$as_datos	Array with all data, fields and other values
	//
	function of_draw_mensaje_editar($as_datos) {
		global $fndrf;

		array($l_text);

		$l_text["arriba"] =
								$fndrf->f_draw_td($fndrf->f_form_text_a("titulo", $as_datos, "titulo", 42, 255, false), "titulo", "", "").
								$fndrf->f_draw_td($fndrf->f_form_text_a("texto", $as_datos, "fecha", 23, 19, false), "", "", "130").
								$fndrf->f_draw_td($fndrf->f_form_text_a("texto", $as_datos, "autor", 32, 128, false), "", "", "110").
								$fndrf->f_draw_td($fndrf->f_form_text_a("texto", $as_datos, "orden", 4, 4, false), "", "", "20")
								;

		$l_text["abajo"] =
								$fndrf->f_draw_td($fndrf->f_form_textarea_a("texto", $as_datos, "texto", 100, 5, false));

		$l_text["mas"] =
								$fndrf->f_draw_td(
									$fndrf->f_form_boton("texto", "cb_edituna", "Aceptar", false).
									$fndrf->f_form_hidden_a($as_datos, "id")
								);

		$l_text["form"] = "edituna";

		return $this->of_draw_td_mensaje($l_text);
	}




	//
	//	of_draw_td_mensaje()
	//
	//	Draw the message, for viewing or for editing
	//
	//	$as_datos	Array with the data (rows)
	//
	function of_draw_td_mensaje($a_text) {
		global $fndrf;

		$ls_ret =	$fndrf->f_draw_tabla(
							$fndrf->f_draw_tr($a_text["arriba"], "mensajeprimrow")
						,"", "", "100%", "", "0", "0", "0");

		$ls_ret .= $fndrf->f_draw_tabla(
							$fndrf->f_draw_tr($a_text["abajo"] )
						,"", "", "100%", "", "0", "0", "0");


		// Third line
		if (isset($a_text["mas"])) {
			$ls_ret .= $fndrf->f_draw_tabla(
								$fndrf->f_draw_tr($a_text["mas"] )
							,"", "", "100%", "", "0", "0", "0");
		}


		// Put the form
		if (isset($a_text["form"])) {
			$ls_ret = $fndrf->f_form_crear($ls_ret, 'texto', $a_text["form"]);
		}

		return $fndrf->f_draw_td($ls_ret, 'mensaje');
	}




	//
	//
	//	of_mensajes_listar()
	//
	//	List all messages
	//
	//	long	$al_id_editar	ID for editing
	//
	//
	function of_mensajes_listar($al_id_editar=0) {
		global $fndrf;

		// List all messages
		$ls_q = "
		SELECT id, fecha, autor, titulo, texto, icono, orden
		FROM {$this->data['dbtabla']}
		WHERE pertenencia='{$this->data["pertenencia"]}'
		ORDER BY orden, fecha";

		$ll_rows = $fndrf->GetSelectListDataEx($ls_q, $ls_ds);

		// List them
		for ($n = 0; $n < $ll_rows; $n++) {
			if ($ls_ds[$n]["id"] == $al_id_editar) {
				// Edit it
				$ls_renglon = $this->of_draw_mensaje_editar($ls_ds[$n]);
			} else {
				// Show it
				$ls_renglon = $this->of_draw_mensaje_ver($ls_ds[$n]);
			}

			$ls_html_tr.= $fndrf->f_draw_tr($ls_renglon);
		}

		return $fndrf->f_draw_tabla($ls_html_tr);
	}



	//
	//
	//	of_mensaje_listar_nuevo()
	//
	//	Show fields to edit message
	//
	function of_mensaje_listar_nuevo() {
		global $fndrf;
		$l_primeros_datos["orden"] = $fndrf->GetSelectValor("SELECT max(orden) + 10 AS valor FROM {$this->data['dbtabla']} WHERE pertenencia = '{$this->data["pertenencia"]}'");
		$l_primeros_datos["fecha"] = $fndrf->GetSelectValor("SELECT NOW() AS valor");

		// For the momento wont list that
		$l_primeros_datos["tipo"] = "";
		$l_primeros_datos["pertenencia"] = "";

		$ls_renglon = $this->of_draw_mensaje_editar($l_primeros_datos);

		$ls_html_tr.= $fndrf->f_draw_tr($ls_renglon);

		return $fndrf->f_draw_tabla($ls_html_tr);
	}






	//
	//	of_mensaje_modificar()
	//
	//	Modifies the message
	//
	//	Array: $a_campo	Fields to modify (ID has to be there)
	//
	function of_mensaje_modificar($a_campo) {
		global $fndrf;

		// Build the querys
		//$a_campo["autor"] = addslashes($a_campo["autor"]);
		//$a_campo["titulo"] = addslashes($a_campo["titulo"]);
		//$a_campo["texto"] = addslashes($a_campo["texto"]);

		if (!empty($a_campo["id"])) {
			// If the message exists...
			$ls_q = "
				UPDATE {$this->data['dbtabla']}
				SET	fecha='{$a_campo["fecha"]}',
						autor='{$a_campo["autor"]}',
						titulo='{$a_campo["titulo"]}',
						orden='{$a_campo["orden"]}',
						texto='{$a_campo["texto"]}'
				WHERE id = {$a_campo["id"]}
			";
		} else {
			// If it is a new message...
			$ls_q = "
				INSERT INTO {$this->data['dbtabla']}
				(fecha, autor, titulo, orden, texto, pertenencia)
				VALUES	(
							'{$a_campo["fecha"]}',
							'{$a_campo["autor"]}',
							'{$a_campo["titulo"]}',
							'{$a_campo["orden"]}',
							'{$a_campo["texto"]}',
							'{$this->data["pertenencia"]}'
							)
			";
		}


		$fndrf->do_mysql_query($ls_q);

		return mysql_insert_id();
	}




	//
	//	of_mensaje_crea_basado_en()
	//
	//	Creates a new message based in the indicated
	//
	//	long	$al_id_msg	Message ID
	//
	//	Retorno:	long		New message ID
	function of_mensaje_crea_basado_en($al_id_msg) {
		global $fndrf;

		$ls_q = "
			SELECT fecha, autor, titulo, texto, icono, orden, pertenencia, tipo
			FROM {$this->data['dbtabla']}
			WHERE id=$al_id_msg
		";

		$l_data = $fndrf->GetSelectData($ls_q);

		// Actualiza algunos valores
		$l_data["orden"] = $fndrf->GetSelectValor("SELECT orden + 10 AS valor FROM {$this->data['dbtabla']} WHERE id = $al_id_msg");
		$l_data["fecha"] = $fndrf->GetSelectValor("SELECT NOW() AS valor");

		$l_data["autor"] = addslashes($l_data["autor"]);
		$l_data["titulo"] = addslashes($l_data["titulo"]);
		$l_data["texto"] = addslashes($l_data["texto"]);

		// Lo graba
		$ll_nuevo_id = $this->of_mensaje_modificar($l_data);

		return $ll_nuevo_id;
	}



	//
	//	of_mensaje_borra()
	//
	//	Erases the indicated message
	//
	//	long	$as_id	Message ID to erase
	//
	function	of_mensaje_borra($as_id) {
		global $fndrf;

		$ls_q = "
			DELETE FROM {$this->data['dbtabla']} WHERE id = '$as_id' AND pertenencia = '{$this->data["pertenencia"]}'
		";

		$fndrf->do_mysql_query($ls_q);
	}


	//
	//	of_mensajes_menu_principal()
	//
	//	Builds the principal menu to messages
	//
	function of_mensajes_menu_principal() {
		global $fndrf;
		$ls_ret = $fndrf->f_draw_link("?msgnuevo=0", _DRFT_NUEV_BOTON_, _DRFT_NUEV_TXT_);

		return $ls_ret;
	}



	/**
	 * Creates the database table
	 */

	function of_db_create_tables() {
		global $fndrf;
		$sql="
CREATE TABLE {$this->data['dbtabla']} (
  id bigint(20) NOT NULL auto_increment,
  fecha datetime NOT NULL default '0000-00-00 00:00:00',
  autor varchar(128) default NULL,
  tipo varchar(32) default NULL,
  titulo varchar(255) default NULL,
  texto longtext,
  id_ref bigint(20) default NULL,
  pertenencia varchar(16) default NULL,
  icono varchar(128) default NULL,
  orden bigint(20) default NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM COMMENT='Created by class_drafter';";

		$fndrf->do_mysql_query($sql);
	}

}
?>
Return current item: Drafter classes