/**
  * The constructor
  * @param string $form  The form we wish to cash into a table
  */
 public function __construct($form)
 {
     $this->form = $form;
     $factory = I2CE_FormFactory::instance();
     if (!$factory->exists($form)) {
         $msg = "Trying to cache form {$form}, but the form does not exist";
         I2CE::raiseError($msg);
         throw new Exception($msg);
     }
     $this->formObj = $factory->createContainer($this->form);
     if (!$this->formObj instanceof I2CE_Form) {
         $msg = "Cannot instantiate {$this->form}";
         I2CE::raiseError($msg);
         throw new Exception($msg);
     }
     $this->formMech = I2CE_FormStorage::getStorageMechanism($form);
     if (!$this->formMech instanceof I2CE_FormStorage_Mechanism) {
         $msg = "Cannot get storage mechansim for form {$form}";
         I2CE::raiseError($msg);
         throw new Exception($msg);
     }
     $this->short_table_name = $this->getCachedTableName($form, false);
     $this->table_name = $this->getCachedTableName($form, true);
     $this->tmp_table_name = $this->getCachedTableName($form, true, 'tmp_cached_form');
 }
 protected function loadObjects()
 {
     parent::loadObjects();
     if (($this->isPost() || $this->hasData()) && ($searchObj = $this->getPrimary()) instanceof CSD_Search && I2CE_FormStorage::getStorageMechanism($searchObj->getName()) instanceof I2CE_FormStorage_CSDSearch) {
         //this will populate "result" field
         $searchObj->setID("1");
         //so it will populate
         if (($maxField = $searchObj->getField('max')) instanceof I2CE_FormField_INT) {
             if ($maxField->getValue() > 200) {
                 $maxField->setValue(200);
             }
         }
         if (($entityIDField = $searchObj->getField('entityID')) instanceof I2CE_FormField_STRING_LINE) {
             list($form, $id) = array_pad(explode('|', $entityIDField->getValue(), 2), 2, '');
             if ($id) {
                 $entityIDField->setValue($id);
             }
         }
         $searchObj->populate(true);
         if (($matches = $searchObj->getField('matches')) instanceof I2CE_FormField_ASSOC_MAP_RESULTS && count($matches->getValue()) > 200) {
             $this->userMessage("To Many Results To Display.  Please refine your search");
             I2CE::raiseError("Too many results");
             $this->bad_result = true;
             $matches->setValue(array());
             if (($resultField = $searchObj->getField('result')) instanceof I2CE_FormField) {
                 $resultField->setValue('');
             }
         }
     }
     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;
         }
     }
 }
 /**
  * Method used to populate the cache table in case the form storage mechanism is not DB like
  * @param boolean $check_mod.  Defaults to true.  If false, it skips the mod time check
  */
 protected function slowPopulate($check_mod = true, $id = null)
 {
     $fields = array();
     $mdb2_types = array();
     $default_values = array();
     foreach ($this->formObj as $field => $fieldObj) {
         if (!$fieldObj->isInDB()) {
             continue;
         }
         $fields[] = $field;
         $mdb2_types[] = $fieldObj->getMDB2Type();
         $default_values[$field] = $fieldObj->getDBValue();
     }
     $default_values['last_modified'] = '1900-01-01 00:00:00';
     $default_values['created'] = '0000-00-00 00:00:00';
     $default_values['parent'] = '|';
     $insert_fields = $fields;
     $insert_fields[] = 'last_modified';
     $insert_fields[] = 'created';
     $fields[] = 'last_modified';
     $fields[] = 'created';
     $fields[] = 'parent';
     $insert_fields[] = 'parent';
     $insert_fields[] = "id";
     $update_fields = array();
     foreach ($insert_fields as &$field) {
         $field = '`' . $field . '`';
         $update_fields[] = "{$field}=values({$field})";
     }
     unset($field);
     $insertQry = 'INSERT INTO ' . $this->table_name . " (" . implode(',', $insert_fields) . " ) " . " VALUES (" . implode(',', array_fill(0, count($insert_fields), '?')) . ")" . " ON DUPLICATE KEY UPDATE " . implode(',', $update_fields);
     if (I2CE_CachedForm::$spam) {
         I2CE::raiseError("Slow populate:\n{$insertQry}");
     }
     $db = MDB2::singleton();
     $prep = $db->prepare($insertQry, $mdb2_types, MDB2_PREPARE_MANIP);
     if (I2CE::pearError($prep, "Error setting up form in the database:")) {
         return false;
     }
     if ($check_mod) {
         $mod_time = $this->getLastCachedTime();
     } else {
         $mod_time = -1;
     }
     $storage = I2CE_FormStorage::getStorageMechanism($this->form);
     if (!$storage instanceof I2CE_FormStorage_Mechanism) {
         I2CE::raiseError("form {$form} does not have valid form storage mechanism");
         return false;
     }
     if (I2CE_CachedForm::$spam) {
         I2CE::raiseError("Mod Time ={$mod_time}");
     }
     if ($id !== null) {
         $ids = array($id);
     } else {
         $ids = $storage->getRecords($this->form, $mod_time);
     }
     if (I2CE_CachedForm::$spam) {
         I2CE::raiseError(implode("\n", $ids));
     }
     foreach ($ids as $id) {
         $data = $storage->lookupField($this->form, $id, $fields, false);
         if (!is_array($data)) {
             continue;
         }
         $t_data = array();
         foreach ($fields as $field) {
             if (array_key_exists($field, $data)) {
                 $t_data[] = $data[$field];
             } else {
                 $t_data[] = $default_values[$field];
             }
         }
         $t_data[] = $this->form . "|" . $id;
         $res = $prep->execute($t_data);
         if (I2CE::pearError($res, "Error insert into cache table:")) {
             return false;
         }
     }
     I2CE_FormStorage::releaseStorage($this->form);
     if (I2CE_CachedForm::$spam) {
         I2CE::raiseError("Populated " . count($ids) . " entries for {$this->form}");
     }
     return true;
 }