Example #1
0
 /**
  * Create a data tree of the selectable forms.  Deisgned to be fed into tree select
  * @param array $fields an ordered array E.g array('village+county','county','district,'region+country','country').
  * it is an "bottom up" array of string where strings are of the form "$form" or "$form+$link_field".  In the case of
  * the former type, then $link_field is assumed to be the next form.  So for example, "county" has link field "district".
  * If a "$form(+$link_field)" is surrounded by brackets [ ] , it is not displayed.
  * @param array $forms An unorderd array of form names whose values we allow to be selected
  * @param array $limits An array with keys form names and value limit data
  * @param array $orders An array with keys form names and values array of field orders for that form.  
  *                      If the form name has no orders, we use default ordering for that form based on its displayed firelds
  * @param int $show_hidden 0=non-hidden, 1=All, 2=hidden only.  Defaults to 0
  * @param array $report A report name to use for the query instead of building it from form cache or directly.
  * @return array
  */
 public static function buildDataTree($fields, $forms, $limits, $orders = array(), $show_hidden = 0, $report = null, $style = 'default')
 {
     //$forms = array(village, county) -- order does not matter.
     //$fields -- order matters.
     //Example0:
     //$fields == array('village','county','[district+cssc_region],'cssc_region+cssc_country','country')
     //Example1:
     //$fields == array('village+county','county','district,'region+country','country')  -- you could just as easily have used 'region'
     //Example2:
     //$fields == array('village','county');
     //build the tree top down.  top  = country in Example 0,1 and top = county in Example 2
     if (!is_array($forms) || !is_array($fields) || count($fields) == 0) {
         return array();
     }
     $data = array();
     $displayed = array();
     $last_form = false;
     $fields = array_reverse($fields);
     foreach ($fields as &$field) {
         //start0: $form = country $link_field = false   $last_form = false
         //next0: $form = cssc_region $link_field = country $last_form = country
         //start1: $form = country $link_field = false   $last_form = false
         //next0: $form = region $link_field = country $last_form = country
         //start2: $form = county $link_field = false   $last_form = false
         //next2: $form = village $link_field = county  $last_form = county
         if (!is_string($field)) {
             return array();
         }
         $len = strlen($field);
         if ($len >= 2 && $field[0] == '[' && $field[$len - 1] == ']') {
             $field = substr($field, 1, $len - 2);
             $display = false;
         } else {
             $display = true;
         }
         if (($pos = strpos($field, '+')) !== false) {
             list($form, $link_field) = explode('+', $field, 2);
             if ($last_form == false) {
                 //throw away junk linked field data on the top level form
                 $link_field = false;
             }
         } else {
             $form = $field;
             $link_field = false;
         }
         if (!$form) {
             return array();
         }
         if (!$link_field) {
             $link_field = $last_form;
         }
         $field = array($form, $link_field);
         $displayed[$form] = $display;
         $last_form = $form;
     }
     unset($field);
     $styles = array();
     $ff = I2CE_FormFactory::instance();
     if ($last_form) {
         $avail_styles = array($last_form => $style);
         $curr_style = $style;
         foreach (array_reverse($fields) as $formfield) {
             list($form, $link_field) = $formfield;
             if (!$form || !($formObj = $ff->createContainer($form)) instanceof I2CE_Form) {
                 break;
             }
             if (array_key_exists($form, $avail_styles) && is_string($avail_styles[$form])) {
                 $curr_style = $avail_styles[$form];
             } else {
                 $curr_style = 'default';
             }
             $styles["{$form}+{$link_field}"] = $curr_style;
             if (!$link_field || !($fieldObj = $formObj->getField($link_field)) instanceof I2CE_FormField) {
                 break;
             }
             $avail_styles = I2CE_List::getDisplayFieldStyles($form, $style);
         }
     }
     if (is_array($report)) {
         $results = self::buildReportTree($fields, $forms, $displayed, $limits, $orders, $show_hidden, $report);
         if (count($results) > 0) {
             return $results;
         } else {
             I2CE::raiseError("buildReportTree returned no results so defaulting to regular display.  If there is data then something went wrong so it should be fixed.");
         }
     }
     $use_cache = true;
     if (I2CE_ModuleFactory::instance()->isEnabled("CachedForms")) {
         $fs = I2CE_FormStorage::getMechanismByStorage("cached");
         if ($fs instanceof I2CE_FormStorage_cached) {
             try {
                 return $fs->buildDataTree($fields, $forms, $displayed, $limits, $orders, $show_hidden, $style);
             } catch (Exception $e) {
                 $use_cache = false;
                 I2CE::raiseError("Could not cache {$form}");
             }
         }
     }
     $phonebook = array();
     //indexed by "$form|$id" with values (by reference) the arrays at which contains the 'children' sub-array  for $form|$id node
     $parent_links = array();
     //indexed by "$form|$id" with values "$pform|$pid" which is the form/id that "$form|$id" is linked against
     $display_string = array();
     foreach ($fields as $formfield) {
         list($form, $link_field) = $formfield;
         if (array_key_exists("{$form}+{$link_field}", $limits)) {
             $limit = $limits["{$form}+{$link_field}"];
         } elseif (array_key_exists($form, $limits)) {
             $limit = $limits[$form];
         } else {
             $limit = array();
         }
         if (!($formObj = $ff->createContainer($form)) instanceof I2CE_Form) {
             continue;
         }
         $style = 'default';
         if (array_key_exists("{$form}+{$link_field}", $styles)) {
             $style = $styles["{$form}+{$link_field}"];
         }
         //if we dont show the hidden list memmber we need to include the limit where i2ce_disabled is false
         $limit = self::showHiddenLimit($limit, $show_hidden);
         $disp_fields = I2CE_List::getDisplayFields($form, $style);
         $disp_str = I2CE_List::getDisplayString($form, $style);
         //start0:  $form = country, $fields = (name)
         //next0:  $form = cssc_region, $fields = (name, cssc_country)
         //next0:  $form =  district $fields = (name, cssc_region)
         //next0: $form = county $field = (name,distrcit)
         //end0: $form = villate $field = (name,county)
         //start1:  $form = country, $fields = (name)
         //next1:  $form = region, $fields = (name, country)
         //next1:  $form =  district $fields = (name, region)
         //etc.
         if (array_key_exists($form, $orders)) {
             $order = $orders[$form];
         } else {
             $order = I2CE_List::getSortFields($form, $style);
         }
         ksort($order);
         if ($link_field) {
             $field_datas = I2CE_FormStorage::listFields($form, $link_field, false, $limit, $order, false, -1, $use_cache);
         } else {
             $field_datas = I2CE_FormStorage::listFields($form, 'id', false, $limit, $order, false, -1, $use_cache);
         }
         $display_datas = I2CE_FormStorage::listFields($form, $disp_fields, false, $limit, $order, false, -1, $use_cache);
         $link_id = false;
         $last_link = false;
         $selectable = in_array($form, $forms);
         foreach ($field_datas as $id => $field_data) {
             $formid = $form . '|' . $id;
             if (!$link_field) {
                 //this should only be the case for the top form
                 $parent =& $data;
             } else {
                 //we are not at the top.
                 $link = $field_data[$link_field];
                 unset($field_data[$link_field]);
                 if ($last_link != $link) {
                     if (!array_key_exists($link, $phonebook)) {
                         //don't know where to put this as a child of the previous one so skip it
                         continue;
                     }
                     $last_link = $link;
                     if (!array_key_exists('children', $phonebook[$link])) {
                         $phonebook[$link]['children'] = array();
                     }
                     $parent =& $phonebook[$link]['children'];
                     //example: $diplayed == array(country=>true, region=>false,  district=>true, county=>true village => true)
                     //we have $form = district, $formid = district|30, $parent_link= region|40
                     end($displayed);
                     $disp_form = key($displayed);
                     while ($disp_form !== false && $disp_form !== $form) {
                         prev($displayed);
                         $disp_form = key($displayed);
                     }
                     //we end here either before the beginning of the array or where $disp_form == $form.
                     prev($displayed);
                     //we are now at the one before the $form.  if the current form was district, we are now at region
                     $parent_link = $link;
                 } else {
                     if (!array_key_exists($link, $phonebook) || !$phonebook[$link]) {
                         //don't know where to put this as a child of the previous one so skip it
                         continue;
                     }
                 }
                 $parent_links[$formid] = $link;
             }
             if (!array_key_exists($id, $display_datas)) {
                 continue;
             }
             $disp_array = array();
             foreach ($disp_fields as $field) {
                 if (array_key_exists($field, $display_datas[$id]) && ($fieldObj = $formObj->getField($field)) instanceof I2CE_FormField) {
                     $fieldObj->setFromDB($display_datas[$id][$field]);
                     $disp_array[] = $fieldObj->getDisplayValue(false, $style);
                 } else {
                     $disp_array[] = '';
                 }
             }
             $display = vsprintf($disp_str, $disp_array);
             $child_data = array('display' => $display, 'show' => $displayed[$form]);
             if ($selectable) {
                 $child_data['value'] = $formid;
             }
             $parent[] = $child_data;
             end($parent);
             $phonebook[$formid] =& $parent[key($parent)];
         }
     }
     self::removeNotShownNodes($data);
     return $data;
 }