Example #1
1
 function Dataface_SearchForm($tablename, $db = '', $query = '', $fields = null)
 {
     $widgetTypes = array();
     $this->tablename = $tablename;
     $this->db = $db;
     $this->_query = is_array($query) ? $query : array();
     if (!isset($this->_query['-cursor'])) {
         $this->_query['-cursor'] = 0;
     }
     $this->_resultSet =& Dataface_QueryTool::loadResult($tablename, $db, $this->_query);
     parent::HTML_QuickForm($tablename, 'post');
     // Get column information directly from the database
     $this->tablename = preg_replace('/ /', '', $this->tablename);
     $this->_table =& Dataface_Table::loadTable($this->tablename, $this->db);
     $this->_fields = array();
     if (!isset($fields)) {
         $fields = array_keys($this->_table->fields(false, true));
         foreach ($this->_table->relationships() as $relationship) {
             if (@$relationship->_schema['visibility'] and @$relationship->_schema['visibility']['find'] == 'hidden') {
                 continue;
             }
             $rfields = $relationship->fields(true);
             $fkeys = $relationship->getForeignKeyValues();
             $removedKeys = array();
             foreach ($fkeys as $fkeyTable => $fkey) {
                 foreach (array_keys($fkey) as $fkeyKey) {
                     $removedKeys[] = $fkeyTable . '.' . $fkeyKey;
                 }
             }
             $rfields = array_diff($rfields, $removedKeys);
             foreach ($rfields as $rfield) {
                 list($rtable, $rfield) = explode('.', $rfield);
                 $fields[] = $relationship->getName() . '.' . $rfield;
             }
             unset($rfields);
             unset($relationship);
         }
     }
     $this->_fields = array();
     foreach ($fields as $fieldname) {
         $this->_fields[$fieldname] =& $this->_table->getField($fieldname);
     }
 }
Example #2
0
 /**
  * Loads the parent record for this form.  This may be called in static context. 
  * This method is called in the constructor if no valid record is supplied.
  */
 function &getRecord()
 {
     if (Dataface_ImportForm::formSubmitted()) {
         $record = new Dataface_Record($_POST['-table'], array());
         $io = new Dataface_IO($_POST['-table']);
         $io->read($_POST['__keys__'], $record);
         return $record;
     } else {
         $app =& Dataface_Application::getInstance();
         $qt =& Dataface_QueryTool::loadResult($app->_currentTable);
         $record =& $qt->loadCurrent();
         return $record;
     }
 }
Example #3
0
 function HTML_QuickForm_related_select(&$relationship)
 {
     $sql = $relationship->getDomainSQL();
     // The sql query to return all candidate rows of relationship
     $fkeys = $relationship->getForeignKeyValues();
     // Values of foreign keys (fields involved in where and join clauses)
     $table = $relationship->getDomainTable();
     if (isset($fkeys[$table])) {
         $query = $fkeys[$table];
         foreach ($query as $key => $val) {
             if (strpos($val, '$') === 0 or $val == '__' . $table . '__auto_increment__') {
                 unset($query[$key]);
             }
         }
     } else {
         $query = array();
     }
     $qt = new Dataface_QueryTool($table, $relationship->_sourceTable->db, $query);
     $options = $qt->getTitles();
     //print_r($options);
     $this->HTML_QuickForm_select('test', 'test', $options);
     echo $this->toHtml();
 }
Example #4
0
 function Dataface_ResultController($tablename, $db = '', $baseUrl = '', $query = '')
 {
     $this->_tablename = $tablename;
     $this->_db = $db;
     $this->_baseUrl = $baseUrl ? $baseUrl : $_SERVER['PHP_SELF'];
     $app =& Dataface_Application::getInstance();
     $this->_table =& Dataface_Table::loadTable($this->_tablename);
     $this->_fields =& $this->_table->fields();
     if (!$query) {
         $this->_query =& $app->getQuery();
         $query =& $this->_query;
         $this->_queryBuilder = new Dataface_QueryBuilder($this->_query['-table'], $query);
         $this->_resultSet =& $app->getResultSet();
     } else {
         if (!is_array($query)) {
             $query = array("-mode" => $app->_conf['default_mode']);
         }
         $this->_resultSet =& Dataface_QueryTool::loadResult($tablename, $db, $query);
         if (!isset($query['-limit'])) {
             $query['-limit'] = $GLOBALS['Dataface_ResultController_limit'];
         }
         if (!isset($query['-skip'])) {
             $query['-skip'] = $GLOBALS['Dataface_ResultController_skip'];
         }
         if (!isset($query['-cursor'])) {
             $query['-cursor'] = $query['-skip'];
         }
         if (!isset($query['-mode'])) {
             $query['-mode'] = $app->_conf['default_mode'];
         }
         $this->_queryBuilder = new Dataface_QueryBuilder($this->_tablename, $query);
         $this->_query =& $this->_queryBuilder->_query;
     }
     // set the title column
     foreach ($this->_fields as $field) {
         if (preg_match('/char/i', $field['Type'])) {
             $this->_titleColumn = $field['name'];
             break;
         }
     }
     if (!isset($this->_titleColumn)) {
         reset($this->_fields);
         $field = current($this->_fields);
         $this->_titleColumn = $field['name'];
     }
     // set the position
     $this->_pos = $query['-cursor'];
     $this->_displayedRecords = $query['-limit'];
 }
Example #5
0
 function Dataface_ResultList($tablename, $db = '', $columns = array(), $query = array())
 {
     $app =& Dataface_Application::getInstance();
     $this->_tablename = $tablename;
     if (empty($db)) {
         $db = $app->db();
     }
     $this->_db = $db;
     $this->_columns = $columns;
     if (!is_array($columns)) {
         $this->_columns = array();
     }
     $this->_query = $query;
     if (!is_array($query)) {
         $this->_query = array();
     }
     $this->_table =& Dataface_Table::loadTable($tablename);
     $fieldnames = array_keys($this->_table->fields(false, true));
     $fields =& $this->_table->fields(false, true);
     if (count($this->_columns) == 0) {
         foreach ($fieldnames as $field) {
             if (@$fields[$field]['filter']) {
                 $this->_filterCols[] = $field;
             }
             if ($fields[$field]['visibility']['list'] != 'visible') {
                 continue;
             }
             if ($this->_table->isPassword($field)) {
                 continue;
             }
             if (isset($fields[$field]) and !preg_match('/blob/i', $fields[$field]['Type'])) {
                 $this->_columns[] = $field;
             }
         }
     } else {
         foreach ($fieldnames as $field) {
             if (@$fields[$field]['filter']) {
                 $this->_filterCols[] = $field;
             }
         }
     }
     $this->_resultSet =& Dataface_QueryTool::loadResult($tablename, $db, $query);
 }
Example #6
0
 /**
  * Returns a valuelist of the records that can be added to this relationship.
  *
  * @param Dataface_Record &$record The parent record.
  * @param array $query A query to filter the possible records.
  * @returns array Associative array where the keys are of the form
  * 		key1=value1&key2=value2  and the values are the title of the record.
  */
 function getAddableValues(&$record, $filter = array())
 {
     $filter = array_merge($this->getAddExistingFilters(), $filter);
     $t =& Dataface_Table::loadTable($this->getDomainTable());
     $r =& $this->_schema;
     $tkey_names = array_keys($t->keys());
     if (!is_a($record, 'Dataface_Record')) {
         throw new Exception("Attempt to call getAddableValues() without providing a Dataface_Record as context.", E_USER_ERROR);
     }
     if (($res = $record->callDelegateFunction($this->_name . '__' . __FUNCTION__)) !== null) {
         return $res;
     }
     if (isset($this->_schema['vocabulary']['existing'])) {
         // A custom vocabulary has been specified in the relationships.ini
         // file for this relationship.
         $options_temp = $t->getValuelist($r['vocabulary']['existing']);
         $options = array();
         foreach (array_keys($options_temp) as $optkey) {
             if (strpos($optkey, '=') === false) {
                 $options[$tkey_names[0] . '=' . urlencode($optkey)] = $options_temp[$optkey];
             } else {
                 $options[$optkey] = $options_temp[$optkey];
             }
         }
     } else {
         // No custom vocabulary has been specified.  Let's do our best
         // to figure out what the vocabulary should be.
         //$fkeys = $this->getForeignKeyValues(null,null,$record);
         $table = $this->getDomainTable();
         if (isset($fkeys[$table])) {
             $query = $fkeys[$table];
             foreach ($query as $key => $val) {
                 if ($this->isNullForeignKey($val) or strpos($val, '$') === 0 or $val == '__' . $table . '__auto_increment__') {
                     unset($query[$key]);
                 }
             }
         } else {
             $query = array();
         }
         $query = array_merge($filter, $query);
         $qt = new Dataface_QueryTool($table, $this->_sourceTable->db, $query);
         $options = $qt->getTitles(true, false, true);
     }
     return $options;
 }
Example #7
0
 function &df_get_records($table, $query = null, $start = null, $limit = null, $preview = true)
 {
     import('Dataface/QueryTool.php');
     $app = Dataface_Application::getInstance();
     if ($query === null and $start === null and $limit === null) {
         $queryTool = Dataface_QueryTool::loadResult($table);
     } else {
         if ($query === null or !$query) {
             $query = array();
         }
         if ($start !== null) {
             $query['-skip'] = $start;
         }
         if ($limit !== null) {
             $query['-limit'] = $limit;
         }
         $queryTool = new Dataface_QueryTool($table, null, $query);
     }
     $queryTool->loadSet('', false, false, $preview);
     $it = $queryTool->iterator();
     return $it;
 }
Example #8
0
 function &getRecord()
 {
     if (Dataface_QuickForm::formSubmitted()) {
         $record = new Dataface_Record($_POST['-table'], array());
         $io = new Dataface_IO($_POST['-table']);
         $query = $_POST['__keys__'];
         if (is_array($query)) {
             foreach (array_keys($query) as $postKey) {
                 if ($query[$postKey][0] != '=') {
                     $query[$postKey] = '=' . $query[$postKey];
                 }
             }
         }
         $io->read($query, $record);
         return $record;
     } else {
         $app =& Dataface_Application::getInstance();
         $qt =& Dataface_QueryTool::loadResult($app->_currentTable);
         $curr =& $qt->loadCurrent();
         return $curr;
     }
 }
Example #9
0
 function test_get_titles()
 {
     $qt = new Dataface_QueryTool('Profiles', $this->table1->db);
     $titles = $qt->getTitles();
     $this->assertEquals(array('id=10' => 'John', 'id=11' => 'Johnson', 'id=12' => 'William'), $titles);
 }
Example #10
0
 /**
  *
  * Blob requests are ones that only want the content of a blob field in the database.
  * These requests are special in that they will not generally return a content-type of
  * text/html.  These are often images.
  *
  * @param $request  A reference to the global $_REQUEST variable generally.
  *
  */
 static function _handleGetBlob($request)
 {
     $app = Dataface_Application::getInstance();
     import('Dataface/Table.php');
     import('Dataface/QueryTool.php');
     if (strpos(@$request['-field'], '.') !== false) {
         $request = self::_parseRelatedBlobRequest($request);
     }
     if (!isset($request['-field'])) {
         die("Could not complete request.  No field name specified.");
     }
     if (!isset($request['-table'])) {
         die("Could not complete request.  No table specified.");
     }
     $fieldname = $request['-field'];
     $tablename = $request['-table'];
     $table =& Dataface_Table::loadTable($tablename);
     $keys = array_keys($table->keys());
     $lastTableUpdate = $table->getUpdateTime();
     $lastTableUpdate = strtotime($lastTableUpdate);
     $app =& Dataface_Application::getInstance();
     $query =& $app->getQuery();
     $rec =& df_get_record($table->tablename, $query);
     if (!$rec) {
         throw new Exception("No record found to match the request.", E_USER_ERROR);
     }
     $field =& $table->getField($fieldname);
     if (PEAR::isError($field)) {
         header('HTTP/1.0 500 Internal Server Error');
         echo '<h1>Internal Server Error</h1>';
         error_log($field->getMessage());
         exit;
     }
     if (!$rec->checkPermission('view', array('field' => $fieldname))) {
         header('HTTP/1.1 403 Forbidden');
         echo '<h1>Access Forbidden</h1>';
         exit;
     }
     if ($table->isContainer($fieldname)) {
         $savepath = $field['savepath'];
         if (!$rec->val($fieldname)) {
             header('HTTP/1.0 404 Not Found');
             echo '<h1>404 File Not Found</h1>';
             exit;
         }
         header('Content-type: ' . $rec->getMimetype($fieldname));
         header('Content-disposition: attachment; filename="' . basename($rec->val($fieldname)) . '"');
         echo file_get_contents($savepath . '/' . basename($rec->val($fieldname)));
         exit;
     }
     if (!$table->isBlob($fieldname)) {
         die("blob.php can only be used to load BLOB or Binary columns.  The requested field '{$fieldname}' is not a blob");
     }
     //$field =& $table->getField($fieldname);
     if (isset($request['-index'])) {
         $index = $request['-index'];
     } else {
         $index = 0;
     }
     $cachePath = $app->_conf['cache_dir'] . '/' . basename($app->_conf['_database']['name']) . '-' . basename($tablename) . '-' . basename($fieldname) . '-' . basename($index) . '?';
     foreach ($keys as $key) {
         $cachePath .= urlencode($key) . '=' . urlencode($_REQUEST[$key]) . '&';
     }
     $queryTool =& Dataface_QueryTool::loadResult($tablename, null, $request);
     // No mimetype was recorded.  Use the PECL Fileinto extension if it is available.
     $files = glob($cachePath . '-*');
     $found = false;
     if (is_array($files)) {
         foreach ($files as $file) {
             $matches = array();
             if (preg_match('/.*-([^\\-]+)$/', $file, $matches)) {
                 $time = $matches[1];
                 if (intval($time) > $lastTableUpdate) {
                     $found = $file;
                     break;
                 } else {
                     @unlink($file);
                 }
             }
         }
     }
     if ($found !== false) {
         $contents = file_get_contents($found);
     } else {
         $columns = array($fieldname);
         if (isset($field['mimetype']) and $field['mimetype']) {
             $columns[] = $field['mimetype'];
         }
         if (isset($field['filename']) and $field['filename']) {
             $columns[] = $field['filename'];
         }
         $record =& $queryTool->loadCurrent($columns, true, true);
         $record->loadBlobs = true;
         $contents = $record->getValue($fieldname, $index);
         $found = $cachePath . '-' . time();
         $found = str_replace("?", "-", $found);
         if ($fh = fopen($found, "w")) {
             fwrite($fh, $contents);
             fclose($fh);
         } else {
             $found = false;
         }
     }
     if (!isset($record)) {
         $columns = array();
         if (isset($field['mimetype']) and $field['mimetype']) {
             $columns[] = $field['mimetype'];
         }
         if (isset($field['filename']) and $field['filename']) {
             $columns[] = $field['filename'];
         }
         $record =& $queryTool->loadCurrent($columns);
     }
     if (isset($field['mimetype']) and $field['mimetype']) {
         $mimetype = $record->getValue($field['mimetype'], $index);
     }
     if (isset($field['filename']) and $field['filename']) {
         $filename = $record->getValue($field['filename'], $index);
     }
     //$mimetype = $record->getValue($field['mimetype'], $index);
     //echo $mimetype; exit;
     if ((!isset($mimetype) or !$mimetype) and $found !== false) {
         if (extension_loaded('fileinfo')) {
             $res = finfo_open(FILEINFO_MIME_TYPE);
             /* return mime type ala mimetype extension */
             $mimetype = finfo_file($res, $found);
         } else {
             if (function_exists('mime_content_type')) {
                 $mimetype = mime_content_type($found);
             } else {
                 throw new Exception("Could not find mimetype for field '{$fieldname}'", E_USER_ERROR);
             }
         }
     }
     if (!isset($filename)) {
         $filename = $request['-table'] . '_' . $request['-field'] . '_' . date('Y_m_d_H_i_s');
     }
     //echo "here";
     //echo "here: $mimetype";
     //echo $contents;
     //echo $mimetype; exit;
     header('Content-type: ' . $mimetype);
     header('Content-disposition: attachment; filename="' . $filename . '"');
     echo $contents;
     exit;
 }
Example #11
0
}
require_once 'Dataface/Application.php';
$app =& Dataface_Application::getInstance();
$fieldname = $_REQUEST['-field'];
$tablename = $_REQUEST['-table'];
$table =& Dataface_Table::loadTable($tablename);
if (!$table->isBlob($fieldname)) {
    die("blob.php can only be used to load BLOB or Binary columns.  The requested field '{$fieldname}' is not a blob");
}
$field =& $table->getField($fieldname);
print_r($field);
exit;
if (isset($_REQUEST['-index'])) {
    $index = $_REQUEST['-index'];
} else {
    $index = 0;
}
$queryTool =& Dataface_QueryTool::loadResult($tablename, null, $_REQUEST);
$mimetype = $field['mimetype'];
$columns = array($fieldname, $mimetype);
$queryTool->loadCurrent($columns, true, true);
//echo $mimetype;
//echo $table->getValue($mimetype);
header("Content-type: " . $table->getValue($mimetype, $index));
//echo "Here";
echo $table->getValue($fieldname, $index);
//$res = mysql_query("select small_image from unit_plans limit 1", $app->_db);
//list($val2) = mysql_fetch_row($res);
//echo "Len 1: ".strlen($val);
//echo "Len 2: ".strlen($val2);
//if ( $val === $val2 ) echo "They are the same";
Example #12
0
 function &getResultSet()
 {
     if ($this->queryTool === null) {
         import('Dataface/QueryTool.php');
         $this->queryTool = Dataface_QueryTool::loadResult($this->_query['-table'], $this->db(), $this->_query);
     }
     return $this->queryTool;
 }
Example #13
0
 /**
  * Constructor.
  *
  * @param $tablename The name of the table that is to be displayed.
  * @param $db Database handle.
  * @param $currentAction The name of the current action being performed.  e.g., 'browse', 'list'
  * @param $query Array of query parameters.
  * @param $params Extra parameters.
  */
 function Dataface_TableView($tablename, $db = '', $currentAction = '', $query = array(), $params = array())
 {
     $this->_tablename = $tablename;
     $this->_table =& Dataface_Table::loadTable($this->_tablename, $db);
     $this->_currentAction = $currentAction;
     if (!isset($query['-limit'])) {
         $query['-limit'] = 30;
     }
     $this->_params = $params;
     $this->_query = $query;
     $this->_db = $db;
     $this->_skinTool =& Dataface_SkinTool::getInstance();
     $this->_baseUrl = DATAFACE_SITE_URL . '/index.php';
     $this->_vars = array();
     $this->_resultSet =& Dataface_QueryTool::loadResult($this->_tablename, $this->_db, $this->_query);
     if (!$this->_currentAction) {
         if (isset($query['-action'])) {
             $this->_currentAction = $query['-action'];
         } else {
             if ($this->_resultSet->found() > 1) {
                 $this->_currentAction = 'list';
             } else {
                 $this->_currentAction = 'browse';
             }
         }
     }
     // perform initialization for current action
     if (method_exists($this, "_" . $this->_currentAction . "_init")) {
         call_user_func(array(&$this, "_" . $this->_currentAction . "_init"));
     }
     if (session_id()) {
         if (isset($_SESSION['Dataface_Application_back'])) {
             $this->back = unserialize($_SESSION['Dataface_Application_back']);
         } else {
             $this->back = '';
         }
         $current = array('table' => $this->_table->tablename, 'link' => $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
         $_SESSION['Dataface_Application_back'] = serialize($current);
         if (isset($_SESSION['Dataface_Application_history'])) {
             $this->_history = unserialize($_SESSION['Dataface_Application_history']);
         } else {
             $this->_history = array();
         }
         if ($this->_currentAction == 'browse' and !isset($this->_params['new'])) {
             $record =& $this->_resultSet->loadCurrent();
             if ($record) {
                 $current = array('table' => $this->_table->tablename, 'recordTitle' => $record->getTitle(), 'link' => $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
                 array_push($this->_history, $current);
             }
         }
         if (count($this->_history) > 10) {
             array_shift($this->_history);
         }
         $titles = array();
         foreach (array_keys($this->_history) as $key) {
             if (isset($titles[$this->_history[$key]['recordTitle']])) {
                 unset($this->_history[$key]);
             } else {
                 $titles[$this->_history[$key]['recordTitle']] = true;
             }
         }
         //$this->_history = array_unique($this->_history);
         $_SESSION['Dataface_Application_history'] = serialize($this->_history);
     }
 }
Example #14
0
 /**
  * @brief Loads the current result set.
  * @returns Dataface_QueryTool
  */
 function &getResultSet()
 {
     if ($this->queryTool === null) {
         $this->fireEvent('beforeLoadResultSet');
         $applicationDelegate = $this->getDelegate();
         if (isset($applicationDelegate) and method_exists($applicationDelegate, 'beforeLoadResultSet')) {
             // Do whatever we need to do before the request is handled.
             $applicationDelegate->beforeLoadResultSet();
         }
         import('Dataface/QueryTool.php');
         $this->queryTool = Dataface_QueryTool::loadResult($this->_query['-table'], $this->db(), $this->_query);
     }
     return $this->queryTool;
 }
Example #15
0
 public function loadRecords(xatacard_layout_Schema $schema, $query)
 {
     $tablename = $schema->getProperty('table');
     if (!$tablename) {
         throw new Exception(sprintf("MySQL datasource cannot load a records from schema '%s' because the schema does not specify a table", $schema->getLabel()));
     }
     $queryTool = new Dataface_QueryTool($tablename, df_db(), $query);
     $res = $queryTool->loadSet('', true, true, false);
     // preview should be disabled... we need full records
     if (PEAR::isError($res)) {
         throw new Exception(sprintf("MySQL datasource failed to load records: %s", $res->getMessage()));
     }
     $out = new xatacard_layout_RecordSet();
     $out->setSchema($schema);
     $out->setDatasource($this);
     $out->setFound($queryTool->found());
     $out->setCardinality($queryTool->cardinality());
     $out->setStart($queryTool->start());
     $out->setEnd($queryTool->end());
     $out->setLimit($queryTool->limit());
     $records = $queryTool->getRecordsArray();
     if (PEAR::isError($records)) {
         throw new Exception(sprintf("MySQL datasource cannot load records from schema '%s' because an error occurred in the query: %s", $schema->getLabel(), $records->getMessage()));
     }
     foreach ($records as $rec) {
         $out->addRecord($this->buildRecord($schema, $rec));
     }
     return $out;
 }