".$bool_text;
break;
}
return $html;
}
function setValue( $v )
{
$this->value_intvalue = $v;
}
}
// CustomFieldText - Produces an INPUT Element of the TEXT type in edit mode
class CustomFieldText extends CustomField
{
function CustomFieldText( $field_id, $field_name, $field_order, $field_description, $field_extratags )
{
$this->CustomField( $field_id, $field_name, $field_order, $field_description, $field_extratags );
$this->field_htmltype = 'textinput';
}
function getHTML($mode)
{
switch($mode)
{
case "edit":
$html = $this->field_description.":
field_name."\" value=\"".$this->charValue()."\" ".$this->field_extratags." />";
break;
case "view":
$html = $this->field_description.":
".$this->charValue();
break;
}
return $html;
}
}
// CustomFieldTextArea - Produces a TEXTAREA Element in edit mode
class CustomFieldTextArea extends CustomField
{
function CustomFieldTextArea( $field_id, $field_name, $field_order, $field_description, $field_extratags )
{
$this->CustomField( $field_id, $field_name, $field_order, $field_description, $field_extratags );
$this->field_htmltype = 'textarea';
}
function getHTML($mode)
{
switch($mode)
{
case "edit":
$html = $this->field_description.":
";
break;
case "view":
$html = $this->field_description.":
".nl2br($this->charValue());
break;
}
return $html;
}
}
// CustomFieldLabel - Produces just a non editable label
class CustomFieldLabel extends CustomField
{
function CustomFieldLabel($field_id, $field_name, $field_order, $field_description, $field_extratags) {
$this->CustomField( $field_id, $field_name, $field_order, $field_description, $field_extratags );
$this->field_htmltype = 'label';
}
function getHTML($mode) {
// We don't really care about its mode
return "field_extratags>$this->field_description";
}
}
// CustomFieldSeparator - Produces just an horizontal line
class CustomFieldSeparator extends CustomField
{
function CustomFieldSeparator($field_id, $field_name, $field_order, $field_description, $field_extratags) {
$this->CustomField( $field_id, $field_name, $field_order, $field_description, $field_extratags );
$this->field_htmltype = 'separator';
}
function getHTML($mode) {
// We don't really care about its mode
return "field_extratags />";
}
}
// CustomFieldSelect - Produces a SELECT list, extends the load method so that the option list can be loaded from a seperate table
class CustomFieldSelect extends CustomField
{
var $options;
function CustomFieldSelect( $field_id, $field_name, $field_order, $field_description, $field_extratags )
{
$this->CustomField( $field_id, $field_name, $field_order, $field_description, $field_extratags );
$this->field_htmltype = 'select';
$this->options = New CustomOptionList( $field_id );
$this->options->load();
}
function getHTML($mode)
{
switch($mode)
{
case "edit":
$html = $this->field_description.":
".$this->options->itemAtIndex($this->intValue());
break;
}
return $html;
}
function setValue( $v )
{
$this->value_intvalue = $v;
}
function value()
{
return $this->value_intvalue;
}
}
/* CustomFieldWeblink
** Produces an INPUT Element of the TEXT type in edit mode
** and a weblink in display mode
*/
class CustomFieldWeblink extends CustomField
{
function CustomFieldWeblink ( $field_id, $field_name, $field_order, $field_description, $field_extratags )
{
$this->CustomField( $field_id, $field_name, $field_order, $field_description, $field_extratags );
$this->field_htmltype = 'href';
}
function getHTML($mode)
{
switch($mode)
{
case "edit":
$html = $this->field_description.":
field_name."\" value=\"".$this->charValue()."\" ".$this->field_extratags." />";
break;
case "view":
$html = $this->field_description.':
'.$this->charValue().'';
break;
}
return $html;
}
}
// CustomFields class - loads all custom fields related to a module, produces a html table of all custom fields
// Also loads values automatically if the obj_id parameter is supplied. The obj_id parameter is the ID of the module object
// eg. company_id for companies module
class CustomFields
{
var $m;
var $a;
var $mode;
var $obj_id;
var $fields;
function CustomFields($m, $a, $obj_id = NULL, $mode = "edit")
{
$this->m = $m;
$this->a = 'addedit'; // only addedit pages can carry the custom field for now
$this->obj_id = $obj_id;
$this->mode = $mode;
// Get Custom Fields for this Module
$q = new DBQuery;
$q->addTable('custom_fields_struct');
$q->addWhere("field_module = '".$this->m."' AND field_page = '".$this->a."'");
//$q->addOrder('field_order DESC');
$rows = $q->loadList();
if ($rows == NULL)
{
// No Custom Fields Available
}
else
{
foreach($rows as $row)
{
switch ($row["field_htmltype"])
{
case "checkbox":
$this->fields[$row["field_name"]] = New CustomFieldCheckbox( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
case "href":
$this->fields[$row["field_name"]] = New CustomFieldWeblink( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
case "textarea":
$this->fields[$row["field_name"]] = New CustomFieldTextArea( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
case "select":
$this->fields[$row["field_name"]] = New CustomFieldSelect( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
case "label":
$this->fields[$row["field_name"]] = new CustomFieldLabel( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
case "separator":
$this->fields[$row["field_name"]] = new CustomFieldSeparator( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
default:
$this->fields[$row["field_name"]] = New CustomFieldText( $row["field_id"], $row["field_name"], $row["field_order"], stripslashes($row["field_description"]), stripslashes($row["field_extratags"]) );
break;
}
}
if ($obj_id > 0)
{
//Load Values
foreach ($this->fields as $key => $cfield)
{
$this->fields[$key]->load( $this->obj_id );
}
}
}
}
function add( $field_name, $field_description, $field_htmltype, $field_datatype, $field_extratags, &$error_msg )
{
GLOBAL $db;
$next_id = $db->GenID( 'custom_fields_struct_id', 1 );
$field_order = 1;
$field_a = 'addedit';
// TODO - module pages other than addedit
// TODO - validation that field_name doesnt already exist
$q = new DBQuery;
$q->addTable('custom_fields_struct');
$q->addInsert('field_id', $next_id);
$q->addInsert('field_module', $this->m);
$q->addInsert('field_page', $field_a);
$q->addInsert('field_htmltype', $field_htmltype);
$q->addInsert('field_datatype', $field_datatype);
$q->addInsert('field_order', $field_order);
$q->addInsert('field_name', $field_name);
$q->addInsert('field_description', $field_description);
$q->addInsert('field_extratags', $field_extratags);
if (!$q->exec())
{
//return "
".$sql."
";
$error_msg = $db->ErrorMsg();
$q->clear();
return 0;
}
else
{
$q->clear();
return $next_id;
}
}
function update( $field_id, $field_name, $field_description, $field_htmltype, $field_datatype, $field_extratags, &$error_msg )
{
GLOBAL $db;
$q = new DBQuery;
$q->addTable('custom_fields_struct');
$q->addUpdate('field_name', $field_name);
$q->addUpdate('field_description', $field_description);
$q->addUpdate('field_htmltype', $field_htmltype);
$q->addUpdate('field_datatype', $field_datatype);
$q->addUpdate('field_extratags', $field_extratags);
$q->addWhere("field_id = ".$field_id);
if (!$q->exec())
{
$error_msg = $db->ErrorMsg();
$q->clear();
return 0;
}
else
{
$q->clear();
return $field_id;
}
}
function fieldWithId( $field_id )
{
foreach ($this->fields as $k => $v)
{
if ($this->fields[$k]->field_id == $field_id)
return $this->fields[$k];
}
}
function bind( &$formvars )
{
if (!count($this->fields) == 0)
{
foreach ($this->fields as $k => $v)
{
// if ($formvars[$k] != NULL)
// {
$this->fields[$k]->setValue(@$formvars[$k]);
// }
}
}
}
function store( $object_id )
{
if (!count($this->fields) == 0)
{
$store_errors = '';
foreach ($this->fields as $k => $cf)
{
$result = $this->fields[$k]->store( $object_id );
if ($result)
{
$store_errors .= "Error storing custom field ".$k.":".$result;
}
}
//if ($store_errors) return $store_errors;
if ($store_errors) echo $store_errors;
}
}
function deleteField( $field_id )
{
GLOBAL $db;
$q = new DBQuery;
$q->setDelete('custom_fields_struct');
$q->addWhere("field_id = $field_id");
if (!$q->exec())
{
$q->clear();
return $db->ErrorMsg();
}
}
function count()
{
return count($this->fields);
}
function getHTML()
{
if ($this->count() == 0)
{
return "";
}
else
{
$html = "
\n";
foreach ($this->fields as $cfield)
{
$html .= "\t
".$cfield->getHTML($this->mode)."
\n";
}
$html .= "
\n";
return $html;
}
}
function printHTML()
{
$html = $this->getHTML();
echo $html;
}
function search($moduleTable, $moduleTableId, $moduleTableName, $keyword )
{
$q = new DBQuery;
$q->addTable('custom_fields_values', 'cfv');
$q->addQuery('m.'.$moduleTableId);
$q->addQuery('m.'.$moduleTableName);
$q->addQuery('cfv.value_charvalue');
$q->addJoin('custom_fields_struct', 'cfs', 'cfs.field_id = cfv.value_field_id');
$q->addJoin($moduleTable, 'm', 'm.'.$moduleTableId.' = cfv. value_object_id');
$q->addWhere('cfs.field_module = "'.$this->m.'"');
$q->addWhere('cfv.value_charvalue LIKE "%'.$keyword.'%"');
return $q->loadList();
}
}
class CustomOptionList
{
var $field_id;
var $options;
function CustomOptionList( $field_id )
{
$this->field_id = $field_id;
$this->options = array();
}
function load()
{
GLOBAL $db;
$q = new DBQuery;
$q->addTable('custom_fields_lists');
$q->addWhere("field_id = {$this->field_id}");
$q->addOrder("list_value");
if (!$rs = $q->exec()) {
$q->clear();
return $db->ErrorMsg();
}
$this->options = Array();
while ($opt_row = $q->fetchRow())
{
$this->options[$opt_row["list_option_id"]] = $opt_row["list_value"];
}
$q->clear();
}
function store()
{
GLOBAL $db;
if (! is_array($this->options))
$this->options = array();
//load the dbs options and compare them with the options
$q = new DBQuery;
$q->addTable('custom_fields_lists');
$q->addWhere("field_id = {$this->field_id}");
$q->addOrder("list_value");
if (!$rs = $q->exec()) {
$q->clear();
return $db->ErrorMsg();
}
$dboptions = Array();
while ($opt_row = $q->fetchRow())
{
$dboptions[$opt_row["list_option_id"]] = $opt_row["list_value"];
}
$q->clear();
$newoptions = Array();
$newoptions = array_diff($this->options, $dboptions);
$deleteoptions = array_diff($dboptions, $this->options);
//insert the new options
foreach($newoptions as $opt)
{
$optid = $db->GenID('custom_fields_option_id', 1 );
$q = new DBQuery;
$q->addTable('custom_fields_lists');
$q->addInsert('field_id', $this->field_id);
$q->addInsert('list_option_id', $optid);
$q->addInsert('list_value', db_escape(strip_tags($opt)));
if (!$q->exec()) $insert_error = $db->ErrorMsg();
$q->clear();
}
//delete the deleted options
foreach($deleteoptions as $opt => $value)
{
$q = new DBQuery;
$q->setDelete('custom_fields_lists');
$q->addWhere("list_option_id = $opt");
if (!$q->exec()) $delete_error = $db->ErrorMsg();
$q->clear();
}
return $insert_error.' '.$delete_error;
}
function delete()
{
$q = new DBQuery;
$q->setDelete('custom_fields_lists');
$q->addWhere("field_id = {$this->field_id}");
$q->exec();
$q->clear();
}
function setOptions( $option_array )
{
$this->options = $option_array;
}
function getOptions()
{
return $this->options;
}
function itemAtIndex( $i )
{
return $this->options[$i];
}
function getHTML( $field_name, $selected )
{
$html = "\n";
return $html;
}
}
?>