/** * Currently the type of `graph_type` in the `graphs` database table * is limited to varchar(32); we check that all defined graph types, that * are not categories or subcategories, are within this limit. */ function testAllGraphsHaveShortNames() { // we need to login because private graphs require summary currencies $_SESSION["user_id"] = 101; $_SESSION["user_name"] = "Testing user"; $_SESSION["user_key"] = "testing-key"; global $global_user_logged_in; $global_user_logged_in = true; $graphs = graph_types(); foreach ($graphs as $key => $type) { if (isset($type['category']) && $type['category']) { // ignore } else { if (isset($type['subcategory']) && $type['subcategory']) { // ignore } else { $this->assertLessThan(32, strlen($key), "Graph key '{$key}' should be less than 32 characters long"); } } } }
$user = get_user(user_id()); require_user($user); // check premium account limits (unless we're editing a graph) if (!$graph_id) { $q = db()->prepare("SELECT COUNT(*) AS c FROM graphs WHERE page_id=? AND is_removed=0 AND graph_type <> 'linebreak'"); $q->execute(array($page_id)); $count = $q->fetch(); $count = $count['c']; if ($count >= get_premium_value($user, 'graphs_per_page')) { $errors[] = t("Cannot add graph: too many existing graphs on this page.") . ($user['is_premium'] ? "" : " " . t("To add more graphs on this page, upgrade to a :premium_account.", array(':premium_account' => link_to(url_for('premium'), t('premium account'))))); set_temporary_errors($errors); redirect(url_for('profile', array('page' => $page_id))); } } // only permit valid values $graph_types = graph_types(); $permitted_days = array(); foreach (get_permitted_days() as $key => $data) { $permitted_days[] = $data['days']; } $permitted_deltas = get_permitted_deltas(); if (!isset($graph_types[$graph_type])) { throw new Exception("Invalid graph type '" . htmlspecialchars($graph_type) . "'"); } else { if (!is_numeric($width) || $width < 1 || $width > 16) { throw new Exception("Invalid width '" . htmlspecialchars($width) . "'"); } else { if (!is_numeric($height) || $height < 1 || $height > 16) { throw new Exception("Invalid height '" . htmlspecialchars($height) . "'"); } else { if ($days && !in_array($days, $permitted_days)) {
// url_for() references need to be relative to the base path, not the js/ directory that this script is within require __DIR__ . "/../../inc/content_type/js.php"; // to allow for appropriate headers etc require __DIR__ . "/../../inc/global.php"; require __DIR__ . "/../../layout/graphs.php"; require_login(); // note that the contents of this file will change based on user, selected currencies etc; // these parameters need to be encoded into a ?hash parameter, so that while this file can // be cached, it is correctly reloaded when necessary. allow_cache(); ?> function graph_types() { return [ <?php foreach (graph_types() as $id => $graph) { if (!(isset($graph['hide']) && $graph['hide'])) { // we don't want to display graph types that we aren't interested in $arg0 = isset($graph['arg0']) && $graph['arg0'] ? $graph['arg0'] : false; $arg0_values = $arg0 ? $arg0(isset($graph['param0']) ? $graph['param0'] : false, isset($graph['param1']) ? $graph['param1'] : false) : false; if ($arg0_values) { // need to convert from array of (id => value) to a list of {id, value}, because JS // sorts by id whereas PHP sorts by insertion order $result = array(); foreach ($arg0_values as $key => $value) { $result[] = array($key, $value); } $arg0_values = $result; } if (!($arg0 && !$arg0_values)) { // we also don't want to display graph types that need arguments, but there aren't any
require __DIR__ . "/../layout/graphs.php"; require_login(); require_admin(); $page_id = require_post("page"); $messages = array(); $errors = array(); // check that we own this page $q = db()->prepare("SELECT * FROM graph_pages WHERE id=? AND user_id=?"); $q->execute(array($page_id, user_id())); if (!$q->fetch()) { throw new Exception("You do not own that graph page."); } // delete all old graphs $q = db()->prepare("DELETE FROM graphs WHERE page_id=?"); $q->execute(array($page_id)); // now go through all graphs $count = 0; foreach (graph_types() as $key => $graph_type) { if (isset($graph_type['category']) && $graph_type['category'] || isset($graph_type['subcategory']) && $graph_type['subcategory']) { // add a new heading $graph = array('page_id' => $page_id, 'graph_type' => 'heading', 'arg0' => 0, 'width' => 1, 'height' => 1, 'page_order' => $count, 'days' => 45, 'string0' => "Category: " . $graph_type['title']); } else { $graph = array('page_id' => $page_id, 'graph_type' => $key, 'arg0' => 0, 'width' => isset($graph_type['default_width']) ? $graph_type['default_width'] : get_site_config('default_user_graph_width'), 'height' => isset($graph_type['default_height']) ? $graph_type['default_height'] : get_site_config('default_user_graph_height'), 'page_order' => $count, 'days' => 45, 'string0' => ''); } $q = db()->prepare("INSERT INTO graphs SET page_id=:page_id, graph_type=:graph_type, arg0=:arg0, width=:width, height=:height, page_order=:page_order, days=:days, string0=:string0"); $q->execute($graph); $count++; } // redirect to this page $messages[] = t("Reset graph page with :graphs.", array(':graphs' => plural("example graph", $count))); redirect(url_for('profile', array('page' => $page_id)));