Solution for an automatic hierselect with DB_DataObject_FormBuilder up to 4 selects

From: Date: Thu, 23 Nov 2006 12:56:13 +0000
Subject: Solution for an automatic hierselect with DB_DataObject_FormBuilder up to 4 selects
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-25908@lists.php.net to get a copy of this message
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]:$virtualfield); $hierselect =& HTML_QuickForm::createElement('hierselect', $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('hierselect',$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

« previous php.pear.general (#25908) next »