/**
  * Gets the storage options for the given form.
  * Since this is the cached version for this form then
  * we don't want to use any options set for that and 
  * use what's needed for the cached tables.
  * @param string $form
  * @return I2CE_MagicDataNode
  */
 protected function getStorageOptions($form)
 {
     if (!is_scalar($form)) {
         I2CE::raiseError("Bad call to get storage options for {$form}");
         return false;
     }
     $config = I2CE_MagicData::instance("temp_Module_CachedForm_storage_options");
     $config->table = I2CE_CachedForm::getCachedTableName($form);
     if ($this->preserve_ids) {
         $config->id->form_prepended = 0;
     } else {
         $config->id->form_prepended = 1;
     }
     return $config;
 }
 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);
 }
 protected function exportTables()
 {
     $mysqldump = trim(`which mysqldump`);
     $compress = $this->request_exists('compress') && $this->request('compress');
     if ($compress) {
         $compress = trim(`which bzip2`);
     }
     if (!$mysqldump) {
         I2CE::raiseError("No mysqldump");
         return false;
     }
     $forms = $this->getSelected();
     if (count($forms) == 0) {
         I2CE::raiseError("No forms selected for import");
         return false;
     }
     $mod_date = false;
     if ($this->request_exists('mod_date') && preg_match('/^(\\d+)-(\\d+)-(\\d+)$/', $this->request('mod_date'))) {
         $mod_date = $this->request('mod_date');
     }
     $db = MDB2::singleton();
     $tables = $db->queryCol("SHOW TABLES FROM " . $db->database_name);
     $uncached = array();
     foreach ($forms as $i => &$form) {
         $form = I2CE_CachedForm::getCachedTableName($form, false);
         if (!in_array($form, $tables)) {
             $uncached[] = $form;
             unset($forms[$i]);
         }
     }
     unset($form);
     if (count($uncached) > 0) {
         I2CE::raiseError("Skipping uncached forms from tables:\n" . implode(" ", $uncached) . "\nExisting tables are:\n" . implode(" ", $tables));
     }
     if (count($forms) == 0) {
         $this->userMessage("No tables have been cached for the requested forms");
         if (array_key_exists('HTTP_HOST', $_SERVER)) {
             $this->redirect("CachedForms");
         }
         return false;
     }
     if ($mod_date) {
         $opts = " --replace  --skip-add-drop-table ";
     } else {
         $opts = " --no-create-info ";
     }
     $cmd = $mysqldump . '  --quick --dump-date --complete-insert  ' . ' --user='******'username']) . ' --password='******'password']) . ' ' . escapeshellarg($db->database_name) . " " . implode(" ", $forms);
     if ($mod_date) {
         $cmd .= " --where=\"DATE(last_modified) >= '{$mod_date}'\" ";
     }
     //some of these header lines were stole from phpmyadmin.. thanks
     $date_str = date('Ymd');
     if ($this->request_exists('profile') && $this->request('profile')) {
         $filename = "cached_forms_export_" . $this->request('profile') . "_{$date_str}.sql";
     } else {
         $filename = "cached_forms_export_{$date_str}.sql";
     }
     if ($compress) {
         $filename .= '.bz2';
         $mime_type = "application/x-bzip2";
         $cmd .= " | " . $compress . " -c ";
     } else {
         $mime_type = "text/x-sql";
     }
     header('Content-Type: ' . $mime_type);
     header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
     header('Content-Disposition: attachment; filename="' . $filename . '"');
     header('Content-Disposition: attachment; filename="' . $filename . '"');
     if (preg_match('/\\s+MSIE\\s+\\d\\.\\d;/', $_SERVER['HTTP_USER_AGENT'])) {
         //internet explorer
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Pragma: public');
     } else {
         header('Pragma: no-cache');
         // test case: exporting a database into a .gz file with Safari
         // would produce files not having the current time
         // (added this header for Safari but should not harm other browsers)
         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
     }
     flush();
     //I2CE::raiseError("Dumping form caches via:\n$cmd");
     passthru($cmd);
     flush();
     die(0);
 }