/**
  * Perform the migrate actions for this module.
  * @return boolean
  */
 protected function migrate()
 {
     $user = new I2CE_User(1, false, false, false);
     $class_config = I2CE::getConfig()->modules->forms->formClasses;
     $migrate_path = "/I2CE/formsData/migrate_data/4.1.8";
     if (!I2CE_FormStorage::migrateForm("training_classification", "entry", $user, $migrate_path)) {
         return false;
     }
     if (I2CE_ModuleFactory::instance()->isEnabled("CachedForms")) {
         $cachedForm = new I2CE_CachedForm("training_classification");
         $cachedForm->dropTable();
     }
     if (!I2CE_FormStorage::migrateField("training", array("training_classification" => "training_classification"), $migrate_path, $user)) {
         return false;
     }
     return true;
 }
 public function __construct($user = false)
 {
     I2CE_CachedForm::$spam = false;
     if (!$user instanceof I2CE_User) {
         $user = new I2CE_User();
     }
     $this->user = $user;
     $this->factory = I2CE_FormFactory::instance();
     $this->unique_fields = array();
 }
 /**
  * Hooked method to drop a cached table
  * @param mixed $args.   an array of two elements whose first element is a form object, the second is the user
  */
 public function dropTable_hook($args)
 {
     if (!is_array($args) || !array_key_exists('form', $args)) {
         return;
     }
     $form = $args['form'];
     if ($form instanceof I2CE_Form) {
         $form = $form->getName();
     }
     if (!is_string($form)) {
         return;
     }
     try {
         $cache = new I2CE_CachedForm($form);
     } catch (Exception $e) {
         return;
     }
     $cache->dropTable();
 }
 /**
  * Update config for given values
  * @param array $vals.  An array of values (presumably from $_POST) to update magic data from
  * @returns  true on sucess
  */
 public function processValues($vals)
 {
     $changed = false;
     foreach (array('provider', 'service', 'facility', 'organization') as $directory) {
         if (array_key_exists($directory, $vals)) {
             $t_changed = $this->getField($directory) != $vals[$directory];
             $changed |= $t_changed;
             if ($t_changed) {
                 I2CE::raiseError("Setting remote CSD {$directory} to {$vals[$directory]}");
                 $this->setField($directory, $vals[$directory]);
             }
         }
     }
     if ($changed) {
         I2CE::raiseError("Dropping all cached forms");
         $forms = I2CE_FormFactory::instance()->getNames();
         $failure = array();
         $config = I2CE::getConfig()->modules->CachedForms;
         if ($config instanceof I2CE_MagicDataNode) {
             $config->cache_all->erase();
         }
         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->page->userMessage("Unable to setup cached form {$form}");
                 }
                 continue;
             }
             $cachedForm->dropTable();
         }
         $this->page->userMessage("Dropped Cache For All Forms");
     }
     return true;
 }
 /**
  * Perform the main actions of the page.
  * @return boolean
  */
 protected function action()
 {
     if (!parent::action()) {
         return false;
     }
     if (!$this->hasPermission("role(admin)")) {
         $this->userMessage("You do not have permission to view this page.");
         return false;
     }
     $pos_mech = I2CE_FormStorage::getStorageMechanism("position");
     $pers_pos_mech = I2CE_FormStorage::getStorageMechanism("person_position");
     if (!$pos_mech instanceof I2CE_FormStorage_entry || !$pers_pos_mech instanceof I2CE_FormStorage_entry) {
         I2CE::raiseMessage("Invalid storage type for position and person position forms. " . get_class($pos_mech) . get_class($pers_pos_mech));
         $this->template->addFile("mass_delete_by_search_error_invalid.html");
         return true;
     }
     $people = $this->post('people');
     if (!is_array($people) || count($people) < 1) {
         $this->template->addFile("mass_delete_by_search_empty.html");
     } else {
         $step = 'choose';
         if ($this->post_exists('step')) {
             $step = $this->post('step');
         }
         if ($step == "delete") {
             if ($this->post('yes') != 'yes') {
                 $this->template->appendFileById("mass_delete_by_search_error_yes.html", "p", "error");
                 $step = "confirm";
             }
             $userAccess = new I2CE_UserAccess_Mechanism();
             if (!$this->post_exists('admin_pass') || !$userAccess->userHasPassword('i2ce_admin', $this->post('admin_pass'))) {
                 $this->template->appendFileById("mass_delete_by_search_error_password.html", "p", "error");
                 $step = "confirm";
             }
         }
         switch ($step) {
             case "choose":
                 $this->template->addFile("mass_delete_by_search_form.html");
                 $msgNode = $this->template->addFile("mass_delete_by_search_confirm_message.html");
                 foreach ($people as $person) {
                     $persObj = I2CE_FormFactory::instance()->createContainer($person);
                     $persObj->populate();
                     $persNode = $this->template->appendFileById("mass_delete_by_search_each.html", "li", "search_list");
                     $this->template->setDisplayDataImmediate("people[]", array('value' => $person, 'id' => "check_{$person}"), $persNode);
                     $this->template->setDisplayDataImmediate("person_name", $persObj->surname . ', ' . $persObj->firstname, $persNode);
                     $label = $this->template->query("label[@name='search_label']", $persNode);
                     if ($label->length == 1) {
                         $label->item(0)->setAttribute("for", "check_{$person}");
                     }
                 }
                 break;
             case "confirm":
                 $list = $this->getDeleteList($people);
                 if ($list === null) {
                     $this->template->addFile("mass_delete_by_search_error_notfound.html");
                 } elseif (count($list) < 1) {
                     I2CE::raiseMessage("Invalid return data from getDeleteList!");
                     $this->template->addFile("mass_delete_by_search_error_unkonwn.html");
                 } else {
                     $formNode = $this->template->addFile("mass_delete_by_search_form.html");
                     $this->template->setDisplayDataImmediate("step", "delete");
                     $addNode = $this->template->addFile("mass_delete_by_search_authenticate_form.html");
                     $would_delete = I2CE_FormStorage_entry::massDelete($list, array());
                     $msgNode = $this->template->addFile("mass_delete_by_search_delete_count.html");
                     $this->template->setDisplayDataImmediate("delete_count", $would_delete, $msgNode);
                     foreach ($people as $person) {
                         $persObj = I2CE_FormFactory::instance()->createContainer($person);
                         $persObj->populate();
                         $persNode = $this->template->appendFileById("mass_delete_by_search_each_final.html", "li", "search_list");
                         $this->template->setDisplayDataImmediate("people[]", $person, $persNode);
                         $this->template->setDisplayDataImmediate("person_name", $persObj->surname . ', ' . $persObj->firstname, $persNode);
                     }
                 }
                 break;
             case "delete":
                 $list = $this->getDeleteList($people);
                 if ($list === null) {
                     $this->template->addFile("mass_delete_by_search_error_notfound.html");
                 } elseif (count($list) < 1) {
                     I2CE::raiseMessage("Invalid return data from getDeleteList!");
                     $this->template->addFile("mass_delete_by_search_error_unkonwn.html");
                 } else {
                     $formNode = $this->template->addFile("mass_delete_by_search_form.html");
                     $this->template->setDisplayDataImmediate("step", "delete");
                     $addNode = $this->template->addFile("mass_delete_by_search_authenticate_form.html");
                     I2CE_ModuleFactory::callHooks("pre_mass_delete_person", $people, $this->post());
                     if (($deleted = I2CE_FormStorage_entry::massDelete($list, array(), false)) !== false) {
                         $node = $this->template->addFile("mass_delete_by_search_success.html");
                         $this->template->setDisplayDataImmediate("delete_count", $deleted, $node);
                         if (I2CE_ModuleFactory::instance()->isEnabled("CachedForms")) {
                             $forms = I2CE_FormFactory::instance()->getNames();
                             $success = array();
                             $failure = array();
                             foreach ($forms as $form) {
                                 try {
                                     $cachedForm = new I2CE_CachedForm($form);
                                 } catch (Exception $e) {
                                     $success[] = $form;
                                     continue;
                                 }
                                 if (!$cachedForm->dropTable()) {
                                     $failure[] = $form;
                                 }
                             }
                             if (count($failure) > 0) {
                                 $this->template->addFile("mass_delete_by_search_cache_fail.html", "p");
                             } else {
                                 $this->template->addFile("mass_delete_by_search_cache_success.html", "p");
                             }
                         }
                     } else {
                         I2CE::raiseError("An error occurred trying to mass delete by search.");
                         $this->template->addFile("mass_delete_by_search_error_unkonwn.html");
                     }
                 }
                 break;
         }
     }
 }
 public function generateLimit_min_parent_form($fieldObj, $vals, $ref, $parent_ref = null)
 {
     if ($parent_ref !== null) {
         $parent_id = $parent_ref;
     } else {
         if (array_key_exists('parent_id', $vals) && $vals['parent_id']) {
             $parent_id = "'" . addslashes($vals['parent_id']) . "'";
         } else {
             $parent_id = '`parent_form`.`id`';
             // Would this be better?
             //$parent_id = '`' . $fieldObj->getContainer()->getName() . '`.`parent`';
         }
     }
     return $ref . ' = (SELECT MIN( `' . $fieldObj->getName() . '`) FROM ' . I2CE_CachedForm::getCachedTableName($fieldObj->getContainer()->getName()) . ' WHERE  ' . I2CE_CachedForm::getCachedTableName($fieldObj->getContainer()->getName()) . '.parent = ' . $parent_id . ')';
 }
 protected function getCreateField($form, $field, $name)
 {
     $result = $this->get_field_def->execute(array(I2CE_CachedForm::getCachedTableName($form, false), $field));
     if (I2CE::pearError($result, "Unable to get field defintion for " . I2CE_CachedForm::getCachedTableName($form, false) . ".{$field}")) {
         return false;
     }
     if ($result->numRows() != 1) {
         I2CE::raiseError("Unexpected number of rows " . $result->numRows() . " when determining field defintion for {$form}.{$field}");
         return false;
     }
     $result = $result->fetchRow();
     $field = "`{$name}` " . $result->column_type . ' ';
     if ($result->character_set_name) {
         $field .= ' CHARACTER SET ' . $result->character_set_name . ' ';
     }
     if (isset($result->collation_name)) {
         $field .= ' COLLATE ' . $result->collation_name;
     }
     return array('type' => $result->column_type, 'field' => $field);
 }
 /**
  * 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;
 }
 /**
  * Hooked method to marks a form as dirty (needs to be cached).
  * @param mixed $args.   an array of two elements whose first element is a form object, the second is the user
  */
 public function markFormDirty_hook($args)
 {
     if (!is_array($args) || !array_key_exists('form', $args)) {
         return;
     }
     $form = $args['form'];
     if (!$form instanceof I2CE_Form) {
         return;
     }
     $form_name = $form->getName();
     try {
         $cache = new I2CE_CachedForm($form_name);
         if (!$cache->updateCachedTable($form->getID())) {
             self::markFormDirty($form_name);
         }
     } catch (Exception $e) {
         self::markFormDirty($form_name);
     }
 }
 /**
  * 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;
         }
     }
 }