Exemplo n.º 1
0
 /**
  *  Generate the cached report
  * @param boolean $force.  Defaults to false.  If set to true, it will force the regeneration of the report if it is in_progress
  * @param boolean $cache_forms.  Defaults to True.  If set to true, it will cache the forms required by this report
  */
 public function generateCache($force = false, $cache_forms = true)
 {
     if (!self::reportExists($this->report)) {
         return false;
     }
     if (!$force && $this->getStatus($this->report) == 'in_progress') {
         I2CE::raiseError("Cached report in progress for {$this->report} and generation is not forced");
         return false;
     }
     if (!$force && !self::isStale($this->report)) {
         I2CE::raiseError("Report {$this->report} is not stale");
         //report is not stale
         return true;
     }
     if ($cache_forms) {
         $forms = $this->getFormsRequiredByReport();
         I2CE::raiseError("Attempting to cache the required forms for the report {$this->report}:\n\t" . implode(',', $forms));
         $failures = array();
         foreach ($forms as $form) {
             try {
                 $cachedForm = new I2CE_CachedForm($form);
             } catch (Exception $e) {
                 if (array_key_exists('HTTP_HOST', $_SERVER)) {
                     //we don't need to check here, b/c it won't error out.  we are doing it to keep the log file clean
                     $msgs = array('not_cached' => 'Unable to setup cached form');
                     foreach ($msgs as $k => &$v) {
                         I2CE::getConfig()->setIfIsSet($v, "/modules/CustomReports/text/user_messages/{$k}");
                     }
                     $this->userMessage($msgs['not_cached'] . ": {$form}");
                 }
                 $failures[] = $form;
                 continue;
             }
             if (!$cachedForm->generateCachedTable(!$force)) {
                 $failures[] = $form;
                 continue;
             }
         }
         if (count($failures) > 0) {
             I2CE::raiseError("Warning data may be out of date for report {$this->report} -- could not cache forms:\n\t" . implode(',', $failures));
         } else {
             I2CE::raiseError("Cached all forms");
         }
     }
     $this->setStatus($this->report, 'in_progress');
     $timeConfig = I2CE::getConfig()->traverse('/modules/CustomReports/times', true);
     $timeConfig->volatile(true);
     $timeConfig->generation->{$this->report} = time();
     if (!$this->_generateCache()) {
         $this->setStatus($this->report, 'failed');
         I2CE::raiseError("Report generation failed for {$this->report}");
         return false;
     }
     I2CE::raiseError("Report generation succeeded for {$this->report}");
     $this->setStatus($this->report, 'generated');
     $timeConfig->generation->{$this->report} = time();
     return true;
 }
 /**
  * Attempts to caches all forms one by one. 
  */
 public function cacheAll($not_force = true, $forms = null)
 {
     I2CE::raiseError("Caching all");
     $config = I2CE::getConfig()->modules->CachedForms;
     $factory = I2CE_FormFactory::instance();
     if ($forms === null) {
         $forms = $factory->getNames();
     }
     $sucess = array();
     $status = '';
     $config->cache_all->volatile(true);
     $config->setIfIsSet($status, 'cache_all/status');
     if ($status === 'in_progress' && $not_force) {
         I2CE::raiseError("In progress");
         return;
     }
     $config->cache_all->status == 'in_progress';
     foreach ($forms as $form) {
         try {
             $cachedForm = new I2CE_CachedForm($form);
         } catch (Exception $e) {
             if (array_key_exists('HTTP_HOST', $_SERVER)) {
                 //we don't need to check here, b/c it won't error out.  we are doing it to keep the log file clean
                 $this->userMessage("Unable to setup cached form {$form}");
             }
             $sucess[] = $form;
             continue;
         }
         if ($cachedForm->generateCachedTable($not_force, false)) {
             $msg = "Cached Table for {$form} generated";
         } else {
             $msg = "Cached Table for {$form} not generated";
             $sucess[] = $form;
         }
         if (array_key_exists('HTTP_HOST', $_SERVER)) {
             $this->userMessage($msg, 'notice');
         } else {
             I2CE::raiseError($msg);
         }
     }
     if (count($sucess) > 0) {
         I2CE::raiseError("CacheAll: Could not cache " . implode(',', $sucess));
         if (array_key_exists('HTTP_HOST', $_SERVER)) {
             $this->userMessage("Could not cache " . implode(',', $sucess));
         }
     }
     $config->cache_all->status = 'done';
     $config->cache_all->time = time();
     if (array_key_exists('HTTP_HOST', $_SERVER)) {
         $this->redirect("CachedForms");
     }
 }
 /**
  * Build the data tree for the given list of fields 
  * and limits.  This is called by I2CE_List::buildDataTree.
  * See that for more details
  * @see I2CE_List::buildDataTree
  * @param array $fields The fields to build the tree
  * @param array $forms The selectable forms
  * @param array $displayed The displayed forms for the tree
  * @param array $limits The list of limits for each form.
  * @param array $orders The order fields for each given form
  * @param int $show_hidden 0=non-hidden, 1=All, 2=hidden only.  Defaults to 0
  * @return array The ordered list of all entries in the tree.
  */
 public function buildDataTree($fields, $forms, $displayed, $limits, $orders, $show_hidden = 0)
 {
     $prev_form = false;
     $form_aliases = array();
     $selects = array();
     $displays = array();
     $all_orders = array();
     $formObjs = array();
     $skip_links = array();
     $skip_link_fields = array();
     $this->preserve_ids = true;
     foreach ($fields as $formfield) {
         list($form, $link_field) = $formfield;
         $cachedForm = new I2CE_CachedForm($form);
         $cachedForm->generateCachedTable(false);
         unset($cachedForm);
         $alias_form = $form . ($link_field ? "+{$link_field}" : "");
         if (array_key_exists($alias_form, $limits)) {
             $limit = $limits[$alias_form];
         } elseif (array_key_exists($form, $limits)) {
             $limit = $limits[$form];
         } else {
             $limit = array();
         }
         self::addFormIdToLimit($form, $limit);
         $limit = I2CE_List::showHiddenLimit($limit, $show_hidden);
         $disp_fields = I2CE_List::getDisplayFields($form);
         $field_list = $disp_fields;
         $field_list[] = 'id';
         if ($link_field && !in_array($link_field, $field_list)) {
             $field_list[] = $link_field;
         }
         if (array_key_exists($form, $orders)) {
             $order = $orders[$form];
         } else {
             $order = I2CE_List::getSortFields($form);
         }
         $sort_list = array();
         if (array_key_exists($form, $displayed) && $displayed[$form]) {
             $displays[$alias_form]['form'] = $form;
             $displays[$alias_form]['link_field'] = $link_field;
             foreach ($disp_fields as $disp) {
                 $displays[$alias_form]['fields'][$disp] = "{$alias_form}+{$disp}";
             }
             foreach ($order as $i => $ord) {
                 if (!is_string($ord)) {
                     unset($order[$i]);
                     continue;
                 }
                 if ($ord[0] == '-') {
                     $field = substr($ord, 1);
                     $all_orders[] = "`{$alias_form}`.`{$field}` DESC";
                 } else {
                     $field = $ord;
                     $all_orders[] = "`{$alias_form}`.`{$field}` ASC";
                 }
                 if (!in_array($field, $field_list)) {
                     $sort_list[] = $field;
                 }
             }
         } else {
             $skip_link_fields[$alias_form] = $link_field;
         }
         if (!array_key_exists($form, $formObjs)) {
             $formObjs[$form] = I2CE_FormFactory::instance()->createContainer($form);
             if (!$formObjs[$form] instanceof I2CE_Form) {
                 I2CE::raiseError("Could not instantiate {$form}");
                 return array();
             }
         }
         $where = false;
         if (is_array($limit) && count($limit) > 0) {
             $where = $formObjs[$form]->generateWhereClause($limit, array("I2CE_FormStorage_cached", "getSQLField"));
         }
         $query = $this->getRequiredFieldsQuery($form, array_merge($field_list, $sort_list), null, false, array("I2CE_FormStorage_cached", "getSQLField"));
         if (is_array($prev_form) && array_key_exists('form', $prev_form)) {
             if ($prev_form['form'] == $form) {
                 $froms[$alias_form] = " LEFT JOIN ";
             } else {
                 $froms[$alias_form] = " LEFT JOIN ";
             }
         } else {
             $froms[$alias_form] = "";
         }
         $froms[$alias_form] .= "({$query}" . ($where ? " WHERE " . $where : "") . ") AS `{$alias_form}`";
         if ($link_field) {
             $join_ons = array();
             $ff = $formObjs[$form]->getField($link_field);
             if ($ff instanceof I2CE_FormField_MAPPED) {
                 foreach ($ff->getSelectableForms() as $link_form) {
                     if (array_key_exists($link_form, $form_aliases)) {
                         foreach ($form_aliases[$link_form] as $link_alias) {
                             $join_ons[] = "`{$alias_form}`.`{$link_field}` = `{$link_alias}`.`id`";
                             if (!array_key_exists($link_alias, $displays) && array_key_exists($link_alias, $skip_link_fields)) {
                                 $skip_links[$alias_form . '+' . $link_field] = $link_alias . '+' . $skip_link_fields[$link_alias];
                             }
                         }
                     }
                 }
             } else {
                 I2CE::raiseMessage("Not sure what to link for {$form} {$link_field} so guessing.");
                 $join_ons[] = "`{$alias_form}`.`{$link_field}` = `" . $prev_form['alias'] . "`.`id`";
             }
             if (count($join_ons) > 0) {
                 $froms[$alias_form] .= " ON (" . implode(' OR ', $join_ons) . ") ";
             } else {
                 I2CE::raiseError("Don't know how to join on {$form} {$link_field}");
                 return array();
             }
         }
         foreach ($field_list as $field) {
             $selects[] = "`{$alias_form}`.`{$field}` AS `{$alias_form}+{$field}`";
         }
         $prev_form = array('form' => $form, 'alias' => $alias_form);
         $form_aliases[$form][] = $alias_form;
     }
     $or_wheres = array();
     foreach ($forms as $selectable) {
         if (array_key_exists($selectable, $form_aliases)) {
             foreach ($form_aliases[$selectable] as $required_form) {
                 $or_wheres[] = "`{$required_form}`.`id` IS NOT NULL";
             }
         }
     }
     $join_query = "SELECT " . implode(',', $selects) . " FROM " . implode('', $froms) . (count($or_wheres) > 0 ? " WHERE " . implode(' OR ', $or_wheres) : "") . (count($all_orders) > 0 ? " ORDER BY " . implode(',', $all_orders) : "");
     //I2CE::raiseMessage( $join_query );
     $res = $this->db->query($join_query);
     if (I2CE::pearError($res, "Bad query -- {$join_query}")) {
         return array();
     }
     $prev_ids = array();
     $results = array();
     $phonebook = array();
     $display_string = array();
     foreach ($displays as $fix_link_alias => &$fix_link_disp_data) {
         if (array_key_exists('link_field', $fix_link_disp_data) && $fix_link_disp_data['link_field'] != '') {
             $full_link_field = strtolower($fix_link_alias . "+" . $fix_link_disp_data['link_field']);
             while (array_key_exists($full_link_field, $skip_links)) {
                 $full_link_field = $skip_links[$full_link_field];
             }
             $fix_link_disp_data['link_field'] = $full_link_field;
         }
     }
     $display_copy = $displays;
     while ($data = $res->fetchRow()) {
         unset($prev_disp);
         foreach ($displays as $alias => $disp_data) {
             $id_field = strtolower($alias . "+id");
             if ($data->{$id_field} == null || array_key_exists($data->{$id_field}, $phonebook)) {
                 continue;
             }
             $curr = array();
             $add_this = false;
             if (in_array($disp_data['form'], $forms)) {
                 $curr['value'] = $data->{$id_field};
                 $add_this = true;
             }
             if (!$add_this) {
                 $check_ok = false;
                 foreach ($display_copy as $alias_copy => $disp_data_copy) {
                     if ($alias_copy == $alias) {
                         $check_ok = true;
                         continue;
                     }
                     if (!$check_ok) {
                         continue;
                     }
                     if (array_key_exists('link_field', $disp_data_copy) && $disp_data_copy['link_field'] != '') {
                         $link_field_copy = $disp_data_copy['link_field'];
                         if ($data->{$id_field} == $data->{$link_field_copy}) {
                             $add_this = true;
                             break;
                         }
                     }
                 }
             }
             if (!$add_this) {
                 continue;
             }
             if (!array_key_exists($disp_data['form'], $display_string)) {
                 $display_string[$disp_data['form']] = I2CE_List::getDisplayString($disp_data['form']);
             }
             $disp_array = array();
             $disp_str = $display_string[$disp_data['form']];
             $disp_str_arr = explode("%s", $disp_str);
             $fo = $formObjs[$disp_data['form']];
             $disp_count = 0;
             foreach ($disp_data['fields'] as $field => $dbfield) {
                 $disp_count++;
                 if ($field == $disp_data['link_field']) {
                     // Don't include the data from the link field since it will already
                     // be in the tree above it.
                     if ($disp_count == 1) {
                         unset($disp_str_arr[$disp_count]);
                     } else {
                         unset($disp_str_arr[$disp_count - 1]);
                     }
                     continue;
                 }
                 $dbfield = strtolower($dbfield);
                 $fieldObj = $fo->getField($field);
                 if (!$fieldObj instanceof I2CE_FormField) {
                     I2CE::raiseError("Could not get field {$field}");
                     continue;
                 }
                 if (isset($data->{$dbfield})) {
                     $fieldObj->setFromDB($data->{$dbfield});
                     $disp_array[$field] = $fieldObj->getDisplayValue();
                 } else {
                     $disp_array[$field] = null;
                 }
             }
             $disp_str = implode('%s', $disp_str_arr);
             $display = vsprintf($disp_str, $disp_array);
             //$display = $data->$disp_data['fields']['name'];
             $curr['display'] = $display;
             $phonebook[$data->{$id_field}] =& $curr;
             if ($disp_data['link_field'] != '') {
                 $link_field = $disp_data['link_field'];
                 if (array_key_exists($data->{$link_field}, $phonebook)) {
                     $add_to =& $phonebook[$data->{$link_field}];
                     if (!array_key_exists('children', $phonebook[$data->{$link_field}])) {
                         $phonebook[$data->{$link_field}]['children'] = array();
                     }
                     $phonebook[$data->{$link_field}]['children'][] =& $curr;
                 } else {
                     I2CE::raiseMessage("Couldn't find {$link_field} " . $data->{$link_field} . " in phonebook");
                 }
             } else {
                 $results[] =& $curr;
             }
             unset($curr);
         }
     }
     $this->preserve_ids = false;
     return $results;
 }
 /**
  * Get the forms that satisfy a relationship for the given primary form id
  * @param string $id the id of the primary form
  * @param array $fields keys are relationship formnames, values are array of fields we wan returned
  * @param array $ordering keys are relationship formnames, values are array of fields we want ordered by
  * @returns array of mixed.  The array may be empty if the $form_id  does not satisfy the relationship.  It is indexed by the named form and the form objects
  * are already populated.  If there was no matching form for a given named form, then the value of the array elemet will be false
  */
 public function getFormData($form, $id, $fields = array(), $ordering = array(), $as_iterator = true)
 {
     if ($this->use_cache) {
         $cforms = $this->getRequiredForms();
         I2CE::raiseError("Attempting to cache the required forms for the relationhip " . implode(",", $cforms));
         $failures = array();
         foreach ($cforms as $cform) {
             try {
                 $cachedForm = new I2CE_CachedForm($cform);
             } catch (Exception $e) {
                 if (array_key_exists('HTTP_HOST', $_SERVER)) {
                     //we don't need to check here, b/c it won't error out.  we are doing it to keep the log file clean
                     $msgs = array('not_cached' => 'Unable to setup cached form');
                     foreach ($msgs as $k => &$v) {
                         I2CE::getConfig()->setIfIsSet($v, "/modules/CustomReports/text/user_messages/{$k}");
                     }
                 }
                 $failures[] = $cform;
                 continue;
             }
             if (!$cachedForm->generateCachedTable()) {
                 $failures[] = $cform;
                 continue;
             }
         }
         if (count($failures) > 0) {
             I2CE::raiseError("Warning data may be out of date for relationsjip -- could not cache forms:\n\t" . implode(',', $failures));
         } else {
             I2CE::raiseError("Cached all forms");
         }
     }
     $results = array();
     if ($form != $this->getPrimaryForm()) {
         I2CE::raiseError("The given form ({$form}) does not match the primary form: " . $this->getPrimaryForm());
         return $results;
     }
     if (strlen($id) == 0 || $id == '0') {
         I2CE::raiseError("Passed empty id");
         return $results;
     }
     $this->_walkSatisfyingForms('primary_form', $form, $id, $fields, $ordering, $results);
     if (!array_key_exists($form, $fields) || !is_array($fields[$form])) {
         $fields[$form] = array();
     }
     if (!array_key_exists($form, $ordering)) {
         $ordering[$form] = array();
     }
     if ($this->use_disp_fields) {
         $data = I2CE_FormStorage::lookupDisplayField($form, $id, $fields['primary_form'], false);
     } else {
         $data = I2CE_FormStorage::lookupField($form, $id, $fields['primary_form'], false);
     }
     $data = array('primary_form' => array("{$form}|{$id}" => array('fields' => $data, 'joins' => $results)));
     if ($as_iterator) {
         return new I2CE_RelationshipData($this, $data);
     } else {
         return $data;
     }
 }
 public static function add_uuids($form, $field = 'csd_uuid')
 {
     I2CE::raiseError("ADDING UUIDS to {$form} on {$field}");
     try {
         //we probably don't need this, but
         $cache = new I2CE_CachedForm($form);
         $cache->dropTable();
     } catch (Exception $e) {
         I2CE::raiseError("Could not clear cache");
         return false;
     }
     $ff = I2CE_FormFactory::instance();
     $user = new I2CE_User();
     $forms = I2CE_FormStorage::listFields($form, array($field));
     foreach ($forms as $id => $fields) {
         if (is_array($fields) && array_key_exists('csd_uuid', $fields) && $fields['csd_uuid']) {
             continue;
         }
         if (!($form_obj = $ff->createContainer(array($form, $id))) instanceof I2CE_Form) {
             I2CE::raiseError("Could not instantiate {$form}|{$id}");
             return false;
         }
         $form_obj->populate();
         self::set_uuid_on_form($form_obj);
         $form_obj->save($user);
         $form_obj->cleanup();
     }
     try {
         //we probably don't need this, but
         $cache = new I2CE_CachedForm($form);
         $cache->dropTable();
         $cache->generateCachedTable();
     } catch (Exception $e) {
         I2CE::raiseError("Could not clear cache");
         return false;
     }
     return true;
 }
 /**
  * Get the forms that satisfy a relationship for the given primary form id
  * @param string $id the id of the primary form
  * @param array $fields keys are relationship formnames, values are array of fields we wan returned
  * @param array $ordering keys are relationship formnames, values are array of fields we want ordered by
  * @returns array of mixed.  The array may be empty if the $form_id  does not satisfy the relationship.  It is indexed by the named form and the form objects
  * are already populated.  If there was no matching form for a given named form, then the value of the array elemet will be false
  */
 public function getFormData($form, $id, $fields = array(), $ordering = array(), $as_iterator = true)
 {
     if ($this->use_cache && !$this->already_cached) {
         $this->already_cached = true;
         $cforms = $this->getRequiredForms();
         I2CE::raiseError("Attempting to cache the required forms for the relationhip " . implode(",", $cforms));
         $failures = array();
         foreach ($cforms as $cform) {
             try {
                 $cachedForm = new I2CE_CachedForm($cform);
             } catch (Exception $e) {
                 if (array_key_exists('HTTP_HOST', $_SERVER)) {
                     //we don't need to check here, b/c it won't error out.  we are doing it to keep the log file clean
                     $msgs = array('not_cached' => 'Unable to setup cached form');
                     foreach ($msgs as $k => &$v) {
                         I2CE::getConfig()->setIfIsSet($v, "/modules/CustomReports/text/user_messages/{$k}");
                     }
                 }
                 $failures[] = $cform;
                 continue;
             }
             if (!$cachedForm->generateCachedTable()) {
                 $failures[] = $cform;
                 continue;
             }
         }
         if (count($failures) > 0) {
             I2CE::raiseError("Warning data may be out of date for relationsjip -- could not cache forms:\n\t" . implode(',', $failures));
         } else {
             I2CE::raiseError("Cached all forms");
         }
     }
     $results = array();
     if ($form != $this->getPrimaryForm()) {
         I2CE::raiseError("The given form ({$form}) does not match the primary form: " . $this->getPrimaryForm());
         if ($as_iterator instanceof DOMElement) {
             return false;
         } else {
             return $results;
         }
     }
     if (strlen($id) == 0 || $id == '0') {
         I2CE::raiseError("Passed empty id");
         if ($as_iterator instanceof DOMElement) {
             return false;
         } else {
             return $results;
         }
     }
     if ($as_iterator instanceof DOMElement) {
         if (!array_key_exists('primary_form', $fields) || !is_array($fields['primary_form'])) {
             $fields['primary_form'] = array();
         }
         if (!array_key_exists('primary_form', $this->object_cache)) {
             if (!($this->object_cache['primary_form'] = $this->getContainer($form)) instanceof I2CE_Form) {
                 return false;
             }
         }
         $this->object_cache['primary_form']->resetDefaultValues();
         $this->object_cache['primary_form']->setID($id);
         $data = I2CE_FormStorage::lookupField($form, $id, $fields['primary_form'], false);
         foreach ($fields['primary_form'] as $field) {
             if (!($fieldObj = $this->object_cache['primary_form']->getField($field)) instanceof I2CE_FormField || !array_key_exists($field, $data) || !is_scalar($data[$field])) {
                 continue;
             }
             $fieldObj->setFromDB($data[$field]);
         }
         //            I2CE::raiseError(print_r($fields,true));
         if (!($primary_node = $this->object_cache['primary_form']->getXMLRepresentation(true, $as_iterator, $fields['primary_form'])) instanceof DOMElement) {
             I2CE::raiseError("Couldn't get XML representation of primary form");
             return false;
         }
         $as_iterator->setAttribute('form', $form);
         $as_iterator->setAttribute('name', $this->relationship);
         $as_iterator->setAttribute('id', $id);
         //            $joined_forms = $as_iterator->ownerDocument->createElement('joinedForms');
         //            $as_iterator->appendChild($joined_forms);
         //            $this->_walkSatisfyingForms('primary_form',$form,$id,$fields,$ordering,$joined_forms);
         $this->_walkSatisfyingForms('primary_form', $form, $id, $fields, $ordering, $as_iterator);
         return true;
     } else {
         if (!array_key_exists($form, $fields) || !is_array($fields[$form])) {
             $fields[$form] = array();
         }
         if (!array_key_exists($form, $ordering)) {
             $ordering[$form] = array();
         }
         $this->_walkSatisfyingForms('primary_form', $form, $id, $fields, $ordering, $results);
         if ($this->use_disp_fields) {
             $data = I2CE_FormStorage::lookupDisplayField($form, $id, $fields['primary_form'], false);
         } else {
             $data = I2CE_FormStorage::lookupField($form, $id, $fields['primary_form'], false);
         }
         $data = array('primary_form' => array("{$form}|{$id}" => array('fields' => $data, 'joins' => $results)));
         if ($as_iterator) {
             return new I2CE_RelationshipData($this, $data);
         } else {
             return $data;
         }
     }
 }