function copy($record, $vals = array(), $force = true) { foreach ($vals as $k => $v) { if (strpos($v, '=') === 0) { $vals[$k] = $this->evaluate($v, $k, $record); } } $del = $record->_table->getDelegate(); if (isset($del) and method_exists($del, 'beforeCopy')) { $res = $del->beforeCopy($record, $vals); if (PEAR::isError($res)) { return $res; } } $this->warnings = array(); // Step 1: Load the record - it has been passed // Step 2: build sql query to copy the record $query = $this->buildCopyQuery($record, $vals, $force); if (PEAR::isError($query)) { return $query; } $res = df_query($query); if (!$res) { return PEAR::raiseError("Failed to copy record '" . $record->getTitle() . "' due to an SQL error:" . xf_db_error()); } if (PEAR::isError($res)) { return $res; } $ret = null; if ($auto_field_id = $record->_table->getAutoIncrementField()) { $insert_id = df_insert_id(); $copied =& df_get_record($record->_table->tablename, array($auto_field_id => $insert_id)); $ret = $copied; } else { $ret = new Dataface_Record($record->_table->tablename, array_merge($record->vals(), $vals)); } if (isset($del) and method_exists($del, 'afterCopy')) { $res = $del->afterCopy($record, $ret); if (PEAR::isError($res)) { return $res; } } return $ret; }
function _loadValuelistsIniFile() { if (!isset($this->_valuelists)) { $this->_valuelists = array(); } $valuelists =& $this->_valuelists; if ($this->_hasValuelistsIniFile()) { $conf = parse_ini_file($this->_valuelistsIniFilePath(), true); foreach ($conf as $vlname => $vllist) { $valuelists[$vlname] = array(); if (is_array($vllist)) { foreach ($vllist as $key => $value) { if ($key == '__sql__') { // we perform the sql query specified to produce our valuelist. // the sql query should return two columns only. If more are // returned, only the first two will be used. If one is returned // it will be used as both the key and value. $res = df_query($value, null, true, true); if (is_array($res)) { //while ($row = mysql_fetch_row($res) ){ foreach ($res as $row) { $valuekey = $row[0]; $valuevalue = count($row) > 1 ? $row[1] : $row[0]; $valuelists[$vlname][$valuekey] = $valuevalue; if (count($row) > 2) { $valuelists[$vlname . '__meta'][$valuekey] = $row[2]; } } } else { trigger_error('Valuelist sql query failed: ' . $value . ': ' . mysql_error() . Dataface_Error::printStackTrace(), E_USER_NOTICE); } } else { $valuelists[$vlname][$key] = $value; } } } } } }
/** * Loads the valuelists from the ini file. */ function _loadValuelistsIniFile() { if (!isset($this->_valuelists)) { $this->_valuelists = array(); } $valuelists =& $this->_valuelists; import('Dataface/ConfigTool.php'); $configTool =& Dataface_ConfigTool::getInstance(); $conf =& $configTool->loadConfig('valuelists', $this->tablename); foreach ($conf as $vlname => $vllist) { $valuelists[$vlname] = array(); if (is_array($vllist)) { foreach ($vllist as $key => $value) { if ($key == '__import__') { // we import the values from another value list. The value of this // entry should be in the form tablename.valuelistname list($ext_table, $ext_valuelist) = explode('.', $value); if (isset($ext_table) && isset($ext_valuelist)) { $ext_table =& self::loadTable($ext_table, $this->db); } else { if (isset($ext_table)) { $ext_valuelist = $ext_table; $ext_table =& $this; } } if (isset($ext_table)) { $ext_valuelist = $ext_table->getValuelist($ext_valuelist); foreach ($ext_valuelist as $ext_key => $ext_value) { $valuelists[$vlname][$ext_key] = $ext_value; } } // clean up temp variables so they don't confuse us // in the next iteration. unset($ext_table); unset($ext_table); unset($ext_valuelist); } else { if ($key == '__sql__') { // we perform the sql query specified to produce our valuelist. // the sql query should return two columns only. If more are // returned, only the first two will be used. If one is returned // it will be used as both the key and value. $res = df_query($value, null, true, true); if (is_array($res)) { //while ($row = mysql_fetch_row($res) ){ foreach ($res as $row) { $valuekey = $row[0]; $valuevalue = count($row) > 1 ? $row[1] : $row[0]; $valuelists[$vlname][$valuekey] = $valuevalue; if (count($row) > 2) { $valuelists[$vlname . '__meta'][$valuekey] = $row[2]; } } //mysql_free_result($res); } else { trigger_error("Valuelist query '" . $value . "' failed. " . Dataface_Error::printStackTrace(), E_USER_NOTICE); } } else { $valuelists[$vlname][$key] = $value; } } } } } }
function getResultFilters() { ob_start(); $app =& Dataface_Application::getInstance(); $query =& $app->getQuery(); echo '<div class="resultlist-filters"> <h3>' . df_translate('scripts.Dataface_ResultList.MESSAGE_FILTER_RESULTS', 'Filter Results') . ':</h3> <script language="javascript"><!-- function resultlist__updateFilters(col,select){ var currentURL = "' . $app->url('') . '"; var currentParts = currentURL.split("?"); var currentQuery = "?"+currentParts[1]; var value = select.options[select.selectedIndex].value; var regex = new RegExp(\'([?&])\'+col+\'={1,2}[^&]*\'); if ( currentQuery.match(regex) ){ if ( value ){ prefix = "="; } else { prefix = ""; } currentQuery = currentQuery.replace(regex, \'$1\'+col+\'=\'+prefix+encodeURIComponent(value)); } else { currentQuery += \'&\'+col+\'==\'+encodeURIComponent(value); } window.location=currentParts[0]+currentQuery; } //--></script> <ul>'; $qb = new Dataface_QueryBuilder($this->_table->tablename, $query); foreach ($this->_filterCols as $col) { $field =& $this->_table->getField($col); unset($vocab); if (isset($field['vocabulary'])) { $vocab =& $this->_table->getValuelist($field['vocabulary']); } else { $vocab = null; } echo '<li> ' . htmlspecialchars($field['widget']['label']) . ' <select onchange="resultlist__updateFilters(\'' . addslashes($col) . '\', this);"><option value="">' . df_translate('scripts.GLOBAL.LABEL_ALL', 'All') . '</option>'; $res = df_query("select `{$col}`, count(*) as `num` " . $qb->_from() . " " . $qb->_secure($qb->_where(array($col => null))) . " group by `{$col}`", null, true); if (!$res and !is_array($res)) { trigger_error(mysql_error(df_db()), E_USER_ERROR); } if (@$query[$col] and $query[$col][0] == '=') { $queryColVal = substr($query[$col], 1); } else { $queryColVal = @$query[$col]; } //while ( $row = mysql_fetch_assoc($res) ){ foreach ($res as $row) { if (isset($vocab) and isset($vocab[$row[$col]])) { $val = $vocab[$row[$col]]; } else { $val = $row[$col]; } if ($queryColVal == $row[$col]) { $selected = ' selected'; } else { $selected = ''; } echo '<option value="' . htmlspecialchars($row[$col]) . '"' . $selected . '>' . htmlspecialchars($val) . ' (' . $row['num'] . ')</option>'; } //@mysql_free_result($res); echo '</select></li>'; } echo '</ul></div>'; $out = ob_get_contents(); ob_end_clean(); return $out; }
/** * * @param array $columns * @return type * @throws Exception@brief Load the totals for the current found set using MySQL's aggregate * operators. This method will always query the database directly (no caching). * * <p>This method was developed to make it easier to create "totals" rows in the * list view.</p> * * <h3>Example</h3> * * @code * $resultSet = Dataface_Application::getInstance()->getResultSet(); * $results = $resultSet->loadTotals(array( * 'age#avg', * 'age#std', * 'income#sum' * )); * print_r($results); * @endcode * * <p>Output:</p> * * @code * array( * 'age_AVG' => 74.4, * 'age_STD' => 13.2, * 'income_SUM' => 100567 * ); * @endcode * * @param array $columns A list of strings of the form {fieldname}#{operator} where * where {fieldname} is the name of a field and {operator} is the name of an operator * to apply to the field. * * <h3>Available Operators</h3> * <table> * <tr> * <th>Operator</th><th>Description</th> * </tr> * <tr> * <td>sum</td><td>The sum of field values for result set.</td> * </tr> * <tr> * <td>avg</td><td>The average value of field in result set.</td> * <tr> * <td>max</td><td>The max value of field in result set.</td> * </tr> * <tr> * <td>min</td><td>The minimum value of field result set.</td> * </tr> * <tr> * <td>bit_and</td><td>Result of bitwise AND on field for all found records.</td> * </tr> * <tr> * <td>bit_or</td><td>Result of bitwise OR on field for all found records.</td> * </tr> * <tr> * <td>bit_xor</td><td>Result of bitwise XOR on field for all found records.</td> * </tr> * <tr> * <td>group_concat</td><td>The concatenation of all rows in found set for field. Joined with a comma.</td> * </tr> * <tr> * <td>std</td><td>Population Standard deviation of field values for result set.</td> * </tr> * <tr> * <td>stddev_samp</td><td>Sample Standard deviation of field values for result set.</td> * </tr> * <tr> * <td>stddev_pop</td><td>Same as stddev</td> * </tr> * <tr> * <td>var_pop<td><td>Population standard variance for field values for result set.</td> * </tr> * <tr> * <td>var_samp</td><td>Sample standard deviation for field values for result set.</td> * </tr> * <tr> * <td>variance</td><td>Same as var_pop</td> * </tr> * </table> * * * @return Associative array where the keys have the form {fieldname}_{operator|uppercase} and the values * are the corresponding result of the operator on that field. E.g. If your input was array('field1#sum', 'field2#avg') * the output woult be array('field1_SUM' => {some number}, 'field2_AVG' => {some_number}) * * @since 2.0.4 * @see Dataface_QueryBuilder::select_totals() */ function loadTotals($columns = array()) { $builder = new Dataface_QueryBuilder($this->_tablename, $this->_query); $sql = $builder->select_totals(array(), null, $columns); $res = df_query($sql); if (!$res) { throw new Exception(xf_db_error(df_db())); } else { $out = xf_db_fetch_assoc($res); @xf_db_free_result($res); return $out; } }