function csv2pg($options = array()) { /* * get command line args here, replacing default values * much like pg2gviz * in other words, defaults get used unless over-ridden by command line args * the difference is they all get stuffed into an options array, makes for much cleaner code */ /* * get the debugging arg */ if (array_key_exists(DEBUGGING, $options)) { $debugging = $options[DEBUGGING]; } else { $debugging = false; } if ($debugging) { echo "debugging default: {$debugging} \n"; } $debugging_arg = getargs("debugging", $debugging); if ($debugging) { echo "debugging_arg: {$debugging_arg} \n"; } if (strlen(trim($debugging_arg))) { $debugging = strtobool($debugging_arg); $options[DEBUGGING] = $debugging; } if ($debugging) { echo "debugging final: {$debugging} \n"; } /* * get the logging arg */ if (array_key_exists(LOGGING, $options)) { $logging = $options[LOGGING]; } else { $logging = false; } if ($debugging) { echo "logging default: {$logging} \n"; } $logging_arg = getargs("logging", $logging); if ($debugging) { echo "logging_arg: {$logging_arg} \n"; } if (strlen(trim($logging_arg))) { $logging = strtobool($logging_arg); $options[LOGGING] = $logging; } if ($debugging) { echo "logging final: {$logging} \n"; } /* * get the filename arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(FILENAME, $options)) { $file_name = $options[FILENAME]; } else { // we can NOT set a default for this so the arg better have something! $file_name = ""; } if ($debugging) { echo "file_name default: {$file_name} \n"; } $file_name_arg = getargs("filename", $file_name); if ($debugging) { echo "file_name_arg: {$file_name_arg} \n"; } if (strlen(trim($file_name_arg))) { $file_name = trim($file_name_arg); $options[FILENAME] = $file_name; if ($debugging) { echo "file_name final: {$file_name} \n"; } } else { if (strlen(trim($file_name))) { if ($debugging) { echo "file_name final: {$file_name} \n"; } } else { // we can NOT proceed without a file name!! if ($logging) { echo "Error: csv2pg: Missing file name. \n"; } if ($debugging) { print_r($options); } return false; } } /* * get the delimiter arg */ if (array_key_exists(DELIMITER, $options)) { $delimiter = $options[DELIMITER]; } else { $delimiter = ","; } if ($debugging) { echo "delimiter default: {$delimiter} \n"; } $delimiter_arg = getargs("delimiter", $delimiter); if ($debugging) { echo "delimiter_arg: {$delimiter_arg} \n"; } $fixed_width = 0; $fixed_width_array = array(); if (strlen(trim($delimiter_arg))) { switch ($delimiter_arg) { case "tab": $delimiter = "\t"; break; case "space": $delimiter = " "; break; case "fixed_" . substr($delimiter_arg, 6): // emulate fixed_* $fixed_width_array = explode("_", substr($delimiter_arg, 6)); $fixed_width = intval($fixed_width_array[0]); if ($logging) { echo "switched to fixed width fields of size: {$fixed_width} \n"; } break; case "comma": default: $delimiter = ","; break; } $options[DELIMITER] = $delimiter; } if ($debugging) { echo "delimiter final: {$delimiter} \n"; } /* * get the method arg */ if (array_key_exists(METHOD, $options)) { $method = $options[METHOD]; } else { // we can set a default for this $method = 1; } if ($debugging) { echo "method default: {$method} \n"; } $method_arg = getargs("method", $method); if ($debugging) { echo "method_arg: {$method_arg} \n"; } if (strlen(trim($method_arg))) { $method = intval($method_arg); $options[METHOD] = $method; } if ($debugging) { echo "logging final: {$logging} \n"; } /* * get the skiplines arg */ if (array_key_exists(SKIPLINES, $options)) { $skiplines = $options[SKIPLINES]; } else { // we can set a default for this $skiplines = 0; } if ($debugging) { echo "skiplines default: {$skiplines} \n"; } $skiplines_arg = getargs("skiplines", $skiplines); if ($debugging) { echo "skiplines_arg: {$skiplines_arg} \n"; } if (strlen(trim($skiplines_arg))) { $skiplines = intval($skiplines_arg); $options[SKIPLINES] = $skiplines; } if ($debugging) { echo "skiplines final: {$skiplines} \n"; } /* * get the fieldcount arg */ if (array_key_exists(FIELDCOUNT, $options)) { $fieldcount = $options[FIELDCOUNT]; } else { // we can set a default for this, 0 means unknown $fieldcount = 0; } if ($debugging) { echo "fieldcount default: {$fieldcount} \n"; } $fieldcount_arg = getargs("fieldcount", $fieldcount); if ($debugging) { echo "fieldcount_arg: {$fieldcount_arg} \n"; } if (strlen(trim($fieldcount_arg))) { $fieldcount = intval($fieldcount_arg); $options[FIELDCOUNT] = $fieldcount; } if ($debugging) { echo "fieldcount final: {$fieldcount} \n"; } /* * get the linenumbers arg */ if (array_key_exists(LINENUMBERS, $options)) { $linenumbers = $options[LINENUMBERS]; } else { $linenumbers = false; } if ($debugging) { echo "linenumbers default: {$linenumbers} \n"; } $linenumbers_arg = getargs("linenumbers", $linenumbers); if ($debugging) { echo "linenumbers_arg: {$linenumbers_arg} \n"; } if (strlen(trim($linenumbers_arg))) { $linenumbers = strtobool($linenumbers_arg); $options[LINENUMBERS] = $linenumbers; } if ($debugging) { echo "linenumbers final: {$linenumbers} \n"; } /* * get the modflow well file arg - 0 is false, xx > is true, read the data for year xx from the file, <0 is true, read the WHOLE file */ if (array_key_exists(MODFLOWWELLFILE, $options)) { $modflowwellfile = $options[MODFLOWWELLFILE]; } else { $modflowwellfile = 0; } if ($debugging) { echo "modflowwellfile default: {$modflowwellfile} \n"; } $modflowwellfile_arg = getargs("modflowwellfile", $modflowwellfile); if ($debugging) { echo "modflowwellfile_arg: {$modflowwellfile_arg} \n"; } if (strlen(trim($modflowwellfile_arg))) { $modflowwellfile = intval($modflowwellfile_arg); $options[MODFLOWWELLFILE] = $modflowwellfile; } if ($debugging) { echo "modflowwellfile final: {$modflowwellfile} \n"; } /* * get the pguser arg */ if (array_key_exists(PGUSER, $options)) { $pguser = $options[PGUSER]; } else { $pguser = ""; } if ($debugging) { echo "pguser default: {$pguser} \n"; } $pguser_arg = getargs("pguser", $pguser); if ($debugging) { echo "pguser_arg: {$pguser_arg} \n"; } if (strlen(trim($pguser_arg))) { $pguser = trim($pguser_arg); $options[PGUSER] = $pguser; if ($debugging) { echo "pguser final: {$pguser} \n"; } } else { if ($debugging) { echo "pguser final: {$pguser} \n"; } } /* * get the pgpassword arg */ if (array_key_exists(PGPASSWORD, $options)) { $pgpassword = $options[PGPASSWORD]; } else { $pgpassword = ""; } if ($debugging) { echo "pgpassword default: {$pgpassword} \n"; } $pgpassword_arg = getargs("pgpassword", $pgpassword); if ($debugging) { echo "pgpassword_arg: {$pgpassword_arg} \n"; } if (strlen(trim($pgpassword_arg))) { $pgpassword = trim($pgpassword_arg); $options[PGPASSWORD] = $pgpassword; if ($debugging) { echo "pgpassword final: {$pgpassword} \n"; } } else { if ($debugging) { echo "pgpassword final: {$pgpassword} \n"; } } /* * get the pgtable arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGTABLE, $options)) { $pgtable = $options[PGTABLE]; } else { // we can NOT set a default for this so the arg better have something! $pgtable = ""; } if ($debugging) { echo "pgtable default: {$pgtable} \n"; } $pgtable_arg = getargs("pgtable", $pgtable); if ($debugging) { echo "pgtable_arg: {$pgtable_arg} \n"; } if (strlen(trim($pgtable_arg))) { $pgtable = trim($pgtable_arg); $options[PGTABLE] = $pgtable; if ($debugging) { echo "pgtable final: {$pgtable} \n"; } } else { if (strlen(trim($pgtable))) { if ($debugging) { echo "pgtable final: {$pgtable} \n"; } } else { // we can NOT proceed without a pgtable!! if ($logging) { echo "Error: csv2pg: Missing pgtable. \n"; } if ($debugging) { print_r($options); } return false; } } /* * get the pgdb arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGDB, $options)) { $pgdb = $options[PGDB]; } else { // we can NOT set a default for this so the arg better have something! $pgdb = ""; } if ($debugging) { echo "pgdb default: {$pgdb} \n"; } $pgdb_arg = getargs("pgdb", $pgdb); if ($debugging) { echo "pgdb_arg: {$pgdb_arg} \n"; } if (strlen(trim($pgdb_arg))) { $pgdb = trim($pgdb_arg); $options[PGDB] = $pgdb; if ($debugging) { echo "pgdb final: {$pgdb} \n"; } } else { if (strlen(trim($pgdb))) { if ($debugging) { echo "pgdb final: {$pgdb} \n"; } } else { // we can NOT proceed without a pgdb!! if ($logging) { echo "Error: csv2pg: Missing pgdb. \n"; } if ($debugging) { print_r($options); } return false; } } /* * get the pghost arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGHOST, $options)) { $pghost = $options[PGHOST]; } else { // we can set a default for this $pghost = "localhost"; } if ($debugging) { echo "pghost default: {$pghost} \n"; } $pghost_arg = getargs("pghost", $pghost); if ($debugging) { echo "pghost_arg: {$pghost_arg} \n"; } if (strlen(trim($pghost_arg))) { $pghost = trim($pghost_arg); $options[PGHOST] = $pghost; if ($debugging) { echo "pghost final: {$pghost} \n"; } } else { if ($debugging) { echo "pghost final: {$pghost} \n"; } } /* * get the pgport arg * this is required, so bail if it is not set from either the default above or the cli arg */ if (array_key_exists(PGPORT, $options)) { $pgport = $options[PGPORT]; } else { // we can set a default for this $pgport = 5432; } if ($debugging) { echo "pgport default: {$pgport} \n"; } $pgport_arg = getargs("pgport", $pgport); if ($debugging) { echo "pgport_arg: {$pgport_arg} \n"; } if (strlen(trim($pgport_arg))) { $pgport = intval($pgport_arg); $options[PGPORT] = $pgport; if ($debugging) { echo "pgport final: {$pgport} \n"; } } else { if ($debugging) { echo "pgport final: {$pgport} \n"; } } /* * now start the file processing * convert file into array of file records */ if ($file_records = file($file_name, FILE_IGNORE_NEW_LINES)) { if ($debugging) { print_r($file_records); } /* * convert array of file records into an array of file field arrays */ if ($fixed_width) { // read the records with fixed width fields of size $fixed_width $file_fields = fixedwidth2array($file_records, $fixed_width_array, $options); } else { //else assume it is a delimited record if ($modflowwellfile) { //hopefully the user set things up for the right file format - such as free format and space delimited which is my case! $file_fields = mfwf2array($file_records, $modflowwellfile, $options); } else { $file_fields = csv2array($file_records, $options); } } if ($file_fields) { if ($debugging) { print_r($file_fields); } /* * append field values to pg table */ $pgconnectionstring = "dbname={$pgdb} host={$pghost} port={$pgport}"; if (strlen($pguser)) { $pgconnectionstring .= " user={$pguser}"; } if (strlen($pgpassword)) { $pgconnectionstring .= " password={$pgpassword}"; } $pgconnection = pg_connect($pgconnectionstring); if (!$pgconnection) { if ($logging) { echo "Error: could not make database connection: " . pg_last_error($pgconnection); } return false; } $results = pg_query($pgconnection, "SELECT * FROM {$pgtable} LIMIT 1"); $pgtable_fieldcount = pg_num_fields($results); if (!$pgtable_fieldcount) { if ($logging) { echo "Error: could not get target table {$pgtable} field count \n"; } return false; } $file_recordcount = count($file_records); $parsed_recordcount = count($file_fields); if ($logging) { echo "\$pgtable_fieldcount: {$pgtable_fieldcount}\n"; } if ($logging) { echo "\$fieldcount (expected): {$fieldcount}\n"; } if ($fieldcount < 0) { if ($logging) { print "Warning: field count is <0 ({$fieldcount}), therefore saving each field to a relational record in table {$pgtable} \n"; } } if ($logging) { echo "\$file_recordcount: {$file_recordcount}\n"; } if ($logging) { echo "\$parsed_recordcount: {$parsed_recordcount}\n"; } if ($linenumbers) { if ($logging) { echo "Warning: using first field in table {$pgtable} as a line number field \n"; } } $arraytocopy = array(); //loop over the file records //for($recordnumber=0;$recordnumber<$file_recordcount;$recordnumber++) { //loop over the parsed file records for ($recordnumber = 0; $recordnumber < $parsed_recordcount; $recordnumber++) { if ($recordnumber >= $skiplines) { $file_fieldcount = count($file_fields[$recordnumber]); if ($debugging) { echo "\$recordnumber: " . ($recordnumber + 1) . " \$file_fieldcount: {$file_fieldcount}\n"; } /* * if $fieldcount >=0 then copy the record fields to database table fields, 1 to 1 * but if $fieldcount <0 then it means to create relational records in the target table for every field. like: row#, column#, value */ if ($fieldcount >= 0) { if ($fieldcount && $file_fieldcount != $fieldcount) { if ($logging) { echo "Warning: record " . ($recordnumber + 1) . " in file {$file_name} has {$file_fieldcount} fields, expected {$fieldcount} \n"; } } if ($file_fieldcount > $pgtable_fieldcount) { if ($logging) { echo "Warning: record " . ($recordnumber + 1) . " in file {$file_name} has {$file_fieldcount} fields, pg table {$pgtable} only has {$pgtable_fieldcount} \n"; } } /* * create the tab delimited string to use for the "row" */ $row = ""; $fieldcounttocopy = $pgtable_fieldcount; $fieldcountcopied = 0; /* * use the first field in the output table for file line numbers */ if ($linenumbers) { $row .= $recordnumber + 1; $fieldcounttocopy = $pgtable_fieldcount - 1; $fieldcountcopied = 1; } /* * now fill out the tab delimited string to paste in each row */ for ($fieldnumber = 0; $fieldnumber < $fieldcounttocopy; $fieldnumber++) { if ($fieldcountcopied) { $row .= "\t"; } if ($fieldnumber < $file_fieldcount) { $row .= $file_fields[$recordnumber][$fieldnumber]; } else { $row .= "\\NULL"; } $fieldcountcopied++; } $row .= "\n"; $arraytocopy[] = $row; } else { /* * make sure the target table is the right size, should be three fields */ if ($pgtable_fieldcount != 3) { if ($logging) { echo "Error, relational table must have three fields. The pg table {$pgtable} has {$pgtable_fieldcount} \n"; } return false; } /* * create the tab delimited string to use for the "row" */ $fieldcounttocopy = $file_fieldcount; $fieldcountcopied = 0; /* * loop over all the fields, creating a record out of each */ for ($fieldnumber = 0; $fieldnumber < $fieldcounttocopy; $fieldnumber++) { /* * now fill out the tab delimited string to paste in each row */ $zero_base = false; if ($zero_base) { $r = $recordnumber - $skiplines; $c = $fieldnumber; } else { $r = $recordnumber - $skiplines + 1; $c = $fieldnumber + 1; } $val = $file_fields[$recordnumber][$fieldnumber]; $row = ""; $row .= $r; $row .= "\t"; $row .= $c; $row .= "\t"; $row .= $val; $row .= "\n"; $arraytocopy[] = $row; $fieldcountcopied++; } } } else { if ($logging) { print "Warning: skipping line " . ($recordnumber + 1) . " of file {$file_name} \n"; } } } return pg_copy_from($pgconnection, $pgtable, $arraytocopy, "\t", "\\NULL"); } else { if ($logging) { echo "Error: csv2pg: could not convert file record array into file field arrays\n"; } return false; } } else { if ($logging) { echo "Error: csv2pg: could not read from file: {$file_name} \n"; } return false; } }
require $relative_path . "common.php"; //get countries $parliaments = json_decode(file_get_contents(APP_PATH . "inc/parliaments.json")); $selected_countries = selected_countries($parliaments); //get categories $categories = json_decode(file_get_contents(APP_PATH . "inc/categories.json")); $categories_sorted = sort_categories($categories); //read jumbo.md include 'Parsedown.php'; $mdurl = TEXT_URL . lang($page) . "/front-page/jumbo.md"; $contents = file_get_contents($mdurl); $Parsedown = new Parsedown(); //include texts //categories $handle = fopen(TEXT_PATH . $lang . DIRECTORY_SEPARATOR . 'meta' . DIRECTORY_SEPARATOR . 'categories.csv', "r"); $categories_texts = csv2array($handle); // print_r($categories_texts);die(); $smarty->assign('countries', json_encode($selected_countries)); $smarty->assign('categories', $categories_sorted); $smarty->assign('jumbo_text', ltrim($Parsedown->text($contents), '<p>')); $smarty->assign('categories_texts', $categories_texts); $smarty->assign('relative_path', $relative_path); $smarty->display($page . '.tpl'); function sort_categories($categories) { $out = []; foreach ($categories as $c) { $out[] = $c; } usort($out, 'compare_weights'); return $out;
// match path-esque strings (containing '/' or '\') trailed by an // EXIF-capable image extension, then verify this file actually exists if (preg_match('#[\\/]+.+\\.(jpe?g|tiff?)$#', $raw) && is_file($raw)) { $nodes = $exif = exif_read_data($raw, 0, true); $len = $exif['COMPUTED']['Width'] . 'x' . $exif['COMPUTED']['Height']; $type->types = ['image']; $type->nodes = ['EXIF' => $nodes['EXIF']]; $type->length = $len; return $type; } }); // csv records Type::hook('_String', function ($raw, Type $type, $path) { if (preg_match('#[\\/]+.+\\.csv$#', $raw) && is_file($raw)) { $type->types = ['csv']; $type->nodes = csv2array($raw); $type->length = count($type->nodes); return $type; } }); function csv2array($file) { $csv = []; $rows = array_map('str_getcsv', file($file)); $header = array_shift($rows); foreach ($rows as $row) { $csv[] = array_combine($header, $row); } return $csv; } // prevent arrays keyed under 'c' from dumping sub-nodes
public function handle_requests() { if (!$this->is_plugin_page()) { return; } if (isset($_GET['action2']) && $_GET['action2'] != -1 && $_GET['action'] == -1) { $_GET['action'] = $_GET['action2']; } if ($_GET['action'] == 'add' && isset($_POST['wpsm-create-table'])) { if (!isset($_POST['table_respon'])) { $_POST['table_respon'] = ''; } $result = $this->db->add($_POST['table_name'], $_POST['table_rows'], $_POST['table_cols'], $_POST['table_subs'], $_POST['table_color'], $_POST['table_respon'], $_POST['table_values']); if ($result) { $sendback = add_query_arg(array('page' => $_GET['page'], 'action' => 'edit', 'table' => $result, 'added' => true), ''); wp_redirect($sendback); } } if ($_GET['action'] == 'edit' && isset($_POST['wpsm-save-changes']) && isset($_GET['table'])) { if (!isset($_POST['table_respon'])) { $_POST['table_respon'] = ''; } $result = $this->db->update($_GET['table'], $_POST['table_name'], $_POST['table_rows'], $_POST['table_cols'], $_POST['table_subs'], $_POST['table_color'], $_POST['table_respon'], $_POST['table_values']); $sendback = add_query_arg(array('page' => $_GET['page'], 'action' => 'edit', 'table' => $_GET['table'], 'updated' => $result), ''); wp_redirect($sendback); } if ($_GET['action'] == 'edit' && isset($_POST['wpsm-create-table'])) { if (!isset($_POST['table_respon'])) { $_POST['table_respon'] = ''; } $result = $this->db->add($_POST['table_name'], $_POST['table_rows'], $_POST['table_cols'], $_POST['table_subs'], $_POST['table_color'], $_POST['table_respon'], $_POST['table_values']); if ($result) { $sendback = add_query_arg(array('page' => $_GET['page'], 'action' => 'edit', 'table' => $result, 'added' => true), ''); wp_redirect($sendback); } } if ($_GET['action'] == 'delete' && isset($_GET['table'])) { if (is_array($_GET['table']) || is_numeric($_GET['table'])) { $result = $this->db->delete($_GET['table']); $sendback = add_query_arg(array('page' => $_GET['page'], 'deleted' => $result), ''); wp_redirect($sendback); } } if (isset($_POST['wpsm-import-table'])) { if (is_uploaded_file($_FILES['upload_file']['tmp_name']) && $_FILES['upload_file']['type'] == 'text/xml') { $xml = simplexml_load_file($_FILES['upload_file']['tmp_name']); $array = xml2array($xml); } else { exit('Can\'t open file: ' . $_FILES['userfile']['name'] . '. Error: ' . $_FILES['upload_file']['error'] . '.'); } $result = $this->db->add($array['name'], $array['rows'], $array['cols'], $array['subs'], $array['color'], $array['responsive'], $array['tvalues']); if ($result) { $sendback = add_query_arg(array('page' => $_GET['page'], 'action' => 'edit', 'table' => $result, 'added' => true), ''); wp_redirect($sendback); } } if (isset($_POST['wpsm-import-csv'])) { if (is_uploaded_file($_FILES['upload_file']['tmp_name']) && $_FILES['upload_file']['type'] == 'text/csv' && isset($_POST['csv_delimiter'])) { if (($handle = fopen($_FILES['upload_file']['tmp_name'], "r")) !== FALSE) { $array = csv2array($handle, $_POST['csv_delimiter']); fclose($handle); } } else { exit('Can\'t open file: ' . $_FILES['userfile']['name'] . '. Error: ' . $_FILES['upload_file']['error'] . '.'); } $array['subs'] = ''; $result = $this->db->add(__('Noname Table', 'wpsm-tableplugin'), $array['rows'], $array['cols'], $array['subs'], 'default', '0', $array['tvalues']); if ($result) { $sendback = add_query_arg(array('page' => $_GET['page'], 'action' => 'edit', 'table' => $result, 'added' => true), ''); wp_redirect($sendback); } } }
<?php /** * common functions */ //error_reporting(E_ALL); error_reporting(0); // load settings $settings = json_decode(file_get_contents($relative_path . 'settings.json')); //get language $lang = lang($settings); // include texts $handle = fopen($relative_path . 'texts_' . $lang . '.csv', "r"); $t = csv2array($handle); // put full path to Smarty.class.php require '/usr/local/lib/php/Smarty/libs/Smarty.class.php'; $smarty = new Smarty(); $smarty->setTemplateDir($relative_path . '../../smarty/templates/' . $settings->template); $smarty->setCompileDir($relative_path . '../../smarty/templates_c'); $smarty->assign('lang', $lang); $smarty->assign('t', $t); $smarty->assign('settings', $settings); /** * get language */ function lang($settings) { if (isset($_GET['lang']) and in_array($_GET['lang'], $settings->languages)) { $_SESSION['lang'] = $_GET['lang']; return $_GET['lang']; } else {
/** * FUNCTIONS FOR BEST PRACTICES */ function best_practices($page) { //read categories $lang = lang($page); $handle = fopen(TEXT_PATH . $lang . DIRECTORY_SEPARATOR . 'meta' . DIRECTORY_SEPARATOR . 'categories.csv', "r"); $categories = csv2array($handle); //read and sort list of examples $handle = fopen(TEXT_PATH . $lang . DIRECTORY_SEPARATOR . $page . DIRECTORY_SEPARATOR . 'examples.csv', "r"); $examples = csv2array($handle); foreach ($examples as $key => $row) { $s[$key] = (double) $row['weight']; } array_multisort($s, SORT_ASC, $examples); //read examples, parse categories include 'Parsedown.php'; $Parsedown = new Parsedown(); foreach ($examples as $key => $example) { $url = TEXT_PATH . $lang . DIRECTORY_SEPARATOR . $page . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $example['code'] . '.md'; $file_headers = @get_headers($url); if (strpos($file_headers[0], '404')) { $exists = false; } else { $exists = true; } if ($exists) { $contents = file_get_contents($url); $parsed = parse_text($Parsedown->text($contents)); $examples[$key]['text'] = $parsed['text']; $examples[$key]['header'] = $parsed['header']; $examples[$key]['teaser'] = $parsed['teaser']; $examples[$key]['rest'] = $parsed['rest']; $examples[$key]['body'] = $parsed['body']; $examples[$key]['categories'] = parse_categories($examples[$key]['categories'], $categories); } else { unset($examples[$key]); } } //filter category $filter = false; $get_category = false; if (!isset($_GET['category'])) { if (isset($_GET['c'])) { $get_category = $_GET['c']; } } else { $get_category = [$_GET['category']]; } if ($get_category) { foreach ($examples as $key => $example) { $in = false; foreach ($example['categories'] as $cat) { if (in_array($cat, $get_category)) { $in = true; } } if (!$in) { unset($examples[$key]); } } foreach ($get_category as $gc) { $filter[] = $categories[$gc]; } } $out = ['categories' => $categories, 'examples' => $examples, 'filter' => $filter]; return $out; }
<?php include "../" . $path2root . "settings.php"; //set up Smarty require SMARTY_PATH; $smarty = new Smarty(); $smarty->setTemplateDir(APP_PATH . 'smarty/templates'); $smarty->setCompileDir(APP_PATH . 'smarty/templates_c'); //get language $lang = lang($path2root); $smarty->assign('lang', $lang); //include texts //page specific $handle = fopen($path2root . 'texts_' . $lang . '.csv', "r"); $texts = csv2array($handle); $smarty->assign('t', $texts); $smarty->assign('app_url', APP_URL); /** * set language */ function lang($path2root) { if (isset($_GET['lang']) and is_readable($path2root . 'texts_' . $_GET['lang'] . '.csv')) { $_SESSION["lang"] = $_GET['lang']; return $_GET['lang']; } else { if (isset($_SESSION['lang'])) { return $_SESSION['lang']; } else { //default language return 'cs';
require $relative_path . 'settings.php'; include $relative_path . "cache.php"; $page = 'explore'; require $relative_path . "common.php"; //read data $questions = json_decode(file_get_contents(APP_PATH . "inc/questions.json")); $categories = json_decode(file_get_contents(APP_PATH . "inc/categories.json")); $data = json_decode(file_get_contents(APP_PATH . "inc/data.json")); $parliaments = json_decode(file_get_contents(APP_PATH . "inc/parliaments.json")); //echo (microtime(true) - $start . "<br>");die(); //read data translations $td = []; //translated data if ($lang != 'en') { $handle = fopen(TEXT_PATH . $lang . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'texts.csv', "r"); $tdraw = csv2array($handle); foreach ($tdraw as $key => $item) { $td[$item['code']] = $item['text']; } } // prepare parliaments for dialog $parliaments_ar = parliaments4dialog($parliaments); $regions = array_keys($parliaments_ar); // prepare questions/categories for dialog $questions_ordered = prepare_questions($questions); $categories_used_codes = array_keys($questions_ordered); $categories_used = prepare_categories($categories_used_codes, $categories); // ensure that GET parameters are arrays $pars = ['p', 'q', 'c', 'cc']; foreach ($pars as $par) { if (!isset($_GET[$par])) {
include "../" . $path2root . "settings.php"; //set up Smarty require SMARTY_PATH; $smarty = new Smarty(); $smarty->setTemplateDir(APP_PATH . 'smarty/templates'); $smarty->setCompileDir(APP_PATH . 'smarty/templates_c'); //get language $lang = lang($path2root); $smarty->assign('lang', $lang); //include texts $handle = fopen($path2root . 'texts_' . $lang . '.csv', "r"); $texts = csv2array($handle); $smarty->assign('t', $texts); //include questions $handle = fopen($path2root . 'questions_' . $lang . '.csv', "r"); $questions = csv2array($handle, true); foreach ($questions as $key => $question) { $questions[$key]['sources_links_ar'] = str_getcsv($question['sources_links']); $questions[$key]['sources_names_ar'] = str_getcsv($question['sources_names']); } $smarty->assign('questions', $questions); $smarty->assign('questions_json', json_encode($questions)); $smarty->assign('questions_count', count($questions)); #print_r($questions);die(); $smarty->assign('app_url', APP_URL); $smarty->assign('cdn_url', CDN_URL); $smarty->assign('session_id', session_id()); if (isset($_SERVER['HTTP_REFERER'])) { $smarty->assign('http_referer', $_SERVER['HTTP_REFERER']); } else { $smarty->assign('http_referer', "");
/** * Accepts a CSV string of tags for a content and adds it to tha database. * TODO: Optimize the function to execute one SQL query. * @param string $tags CSV tags for a content. */ public function addTags($tags) { $tagarray = csv2array(pg_escape_string($tags)); for($i=0;$i<count($tagarray);$i++) { $sql= "Insert into content_tags(ct_contentid, ct_tagid) values('".$this->cid."','".createTag($tagarray[$i])."')"; dbquery($sql); } }