For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > November 2006 > Solution for an automatic hierselect with DB_DataObject_FormBuilder up to 4 selects









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Solution for an automatic hierselect with DB_DataObject_FormBuilder up to 4 selects
Jose Manuel Zea Ginerÿffffffffffe9s

2006-11-23, 7:59 am

Hi all:


Months ago, I asked about how to get an automatic hierselect with DB_DataObject_FormBuilder, like in the example in http://opensource.21st.de/25.html#A11 but I needed more than two selects.

Thanks to the help of different users of this mail list, I could get the solution. And now I want to share it with you, in case you need it. The only thing is that is limited to 4 selects.

Maybe there is a better solution, or quicker, or whatever, but I want to share this with you. It´s working fine for me.

What I did is to declare my own class descending from DB_DataObject, and this is the definition:

require_once 'DB/DataObject.php';

class ltDB_DataObject extends DB_DataObject
{
var $fb_selectAddEmpty = array();

function table()
{
/* hierselect virtual fields */
$hierselects = array();
foreach ($this->conditionalFields as $virtualfield => $cf) {
$hierselects[$virtualfield] = DB_DATAOBJECT_STR;
}
return array_merge(parent::table(), $hierselects);
}

function preGenerateForm(&$fb)
{
$this->fb_preDefElements = array();

/* the hierselect construct */
foreach ($this->conditionalFields as $virtualfield => $cf) {
$desc = (isset($this->fb_fieldLabels[$virtualfield])?$this-> fb_fieldLabels[$virtualfield]:$virtualfi
eld);
$hierselect =& HTML_QuickForm::createElement('hierselec
t', $fb->getFieldName($virtualfield), $desc);
// ****************************************
****************************************
******************
$select = array();
$count = 0;
foreach ($cf as $key => $fields) {
list($tmp, $table) = explode(':', $key);
$do =& DB_DataObject::factory($table);
$do->find();
$select[$count] = array();
while ($do->fetch()) {
$curselect =& $select[$count];
for ($i = 0; $i < count($fields)-1; $i++) {
$field = $fields[$i];
if (!isset($curselect[$do->$field])) {
$curselect[$do->$field] = array();
}
$curselect =& $curselect[$do->$field];
}
$field = $fields[count($fields)-1];
/* check if we need an empty element */
if (in_array($field, $this->fb_selectAddEmpty)) {
if (!isset($curselect[0])) {
$curselect[0] = '';
}
}
/* at last get the displayed field and insert it */
$curselect[$do->$field] = $fb->getDataObjectString($do);
}
$count++;
}

//print_array($select);

$select1 = array();
$select2 = array();
$select3 = array();
$select4 = array();


$select1 = $select[0];

if (is_array($select1)){
foreach ($select1 as $key=>$value){
$select2[$key]=$select[1][$key];
}

foreach ($select1 as $key=>$value){
if ($select2[$key]){
foreach ($select2[$key] as $key2=>$value2){
$select3[$key][$key2]=$select[2][$key2];


}
}
}


foreach ($select1 as $key=>$value){
if ($select2[$key]){
foreach ($select2[$key] as $key2=>$value2){
if ($select3[$key][$key2]){
foreach ($select3[$key][$key2] as $key3=>$value3){
$select4[$key][$key2][$key3]=$select[3][
$key3];
}
}
}
}
}
}

$sel =& HTML_QuickForm::createElement('hierselec
t',$fb->getFieldName($virtualfield), $desc);

if ($count>=1){$options[]=$select1;}
if ($count>=2){$options[]=$select2;}
if ($count>=3){$options[]=$select3;}
if ($count>=4){$options[]=$select4;}

//print_array($options);
$sel->setOptions($options);
$options= array();


$this->fb_preDefElements[$virtualfield] = $sel;

}

/* init the hierselect with the values from the database */
foreach ($this->conditionalFields as $virtualfield => $cf) {
$a = array();
foreach ($cf as $key=>$fields) {
list($localfield, $tmp) = explode(':', $key);
$a[$tmp] = $this->$localfield;
}
$this->$virtualfield =& $a;
}
}

function preProcessForm(&$values)
{
/* put data from hierselect in corresponding database fields */
foreach ($this->conditionalFields as $virtualfield => $cf) {
$count = 0;
foreach ($cf as $key => $fields) {
list($localfield, $tmp) = explode(':', $key);
$this->$localfield = $values[$virtualfield][$count];
$count++;
}
}
}
}

And whenever you need an automatic hierselect, your dataobject must be a descendant of the new class, and define the conditional fields like the next example:

var $conditionalFields = array(
'codzonacodmunicipio' => array(
'codpais:paises' => array('codpais'),
'codprovincia:provincias' => array('codpais','codprovincia'),
'codmunicipio:municipios' => array('codprovincia','codmunicipio'),
'codzona:zonas' => array('codmunicipio', 'codzona')
)
);

With this example you can get something like Country -> Province -> City -> Area

I hope this can help anyone, desperate like I was.

Regards,

Jose Manuel Zea


---------------------------------

LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com