Beispiel #1
0
 public function generateCsv(View $view)
 {
     $columns = $view->getExportableCols();
     $titles = array_map(function (Column $col) {
         return $col->label;
     }, $columns);
     $colNames = array_map(function (Column $col) {
         return $col->id;
     }, $columns);
     // get rows
     $sql = $view->prepareQuery();
     $rows = \Meta\Core\Db::query($sql)->fetchAll(\PDO::FETCH_ASSOC);
     header("Content-type: text/csv");
     header("Content-Disposition: attachment; filename=export.csv");
     header("Pragma: no-cache");
     header("Expires: 0");
     // write CSV to output
     $stdout = fopen('php://output', 'w');
     fputcsv($stdout, $titles);
     foreach ($rows as $row) {
         $newRow = array();
         foreach ($colNames as $name) {
             $newRow[$name] = $row[$name];
         }
         fputcsv($stdout, $newRow);
     }
     fflush($stdout);
     fclose($stdout);
     exit;
 }
Beispiel #2
0
 public function querySql($sql, $messageError)
 {
     $result = \Meta\Core\Db::query(Builder::replaceSQL($sql))->fetchColumn();
     if (!$result) {
         $this->error = $messageError;
     }
     return $result;
 }
Beispiel #3
0
 public static function authenticate($login, $pass)
 {
     $row = \Meta\Core\Db::query('select id from users where login = ? and password = ?', array($login, md5($pass)))->fetch();
     if (isset($row['id'])) {
         $_SESSION['user']['id'] = $row['id'];
         return true;
     }
     unset($_SESSION['user']['id']);
     return false;
 }
Beispiel #4
0
 public static function listGroups()
 {
     return \Meta\Core\Db::query("SELECT id, name FROM groups ORDER BY name")->fetchAll(\PDO::FETCH_KEY_PAIR);
 }
Beispiel #5
0
    return $page;
});
Page::beforeRender('frame-user-groups', function ($page) {
    // modify $page array here
    // prevent unauthorized actions
    if (is_demo() && isset($_REQUEST['action']) && isset($_REQUEST['id'])) {
        if (is_post() || $_REQUEST['action'] == 'delete') {
            $user_id = \Meta\Core\Db::query('SELECT user_id FROM user_groups WHERE id = ?', array($_REQUEST['id']))->fetchColumn();
            if ($user_id == 1) {
                \Meta\Core\Flash::error(t('Sorry, but you cannot modify the Admin user in demo mode'));
                header("Location: " . urldecode($_GET['url_from']));
                exit;
            }
        }
    }
    return $page;
});
Page::beforeRender('manage-files', function ($page) {
    // scan all files. if the number is different than the table, then re populate all entirely table
    $files = \Meta\Core\FileSystem::listAll();
    $dbCountFiles = \Meta\Core\Db::query('SELECT COUNT(*) FROM files')->fetchColumn();
    if (count($files) > 0 && count($files) != $dbCountFiles) {
        // delete all and insert files list
        \Meta\Core\Db::execute('DELETE FROM files');
        foreach ($files as $file) {
            \Meta\Core\Db::save('files', array('name' => basename($file), 'size' => filesize($file)));
        }
        \Meta\Core\Flash::info(t('The files base was been updated'));
    }
    return $page;
});
Beispiel #6
0
        \Meta\Builder::write('pages', $pages);
    }
    echo json_encode(array('errors' => $errors, 'obj' => $object));
});
// new
route_add('builder/editobject/*/*', function ($path, $oid) {
    $pages = \Meta\Builder::read('pages');
    $page = new \Meta\Builder\Page($path, $pages[$path]);
    $object = $page->objects[$oid];
    echo json_encode($object);
});
route_add('builder/analizesql/*/*', function ($pname, $oid) {
    $errors = array();
    $cols = array();
    $sql = \Meta\Builder::replaceSQL(\Meta\Core\Db::sqlNormalize($_REQUEST['query']));
    $statement = \Meta\Core\Db::getPDO()->prepare($sql);
    if ($statement->execute()) {
        for ($i = 0; $i < $statement->columnCount(); $i++) {
            $meta = $statement->getColumnMeta($i);
            //            $cols[$meta['name']] = $meta['native_type'];
            $cols[$meta['name']] = $meta['name'];
        }
    } else {
        $errors = $statement->errorInfo();
    }
    // ataualiza colunas do SQL na base (array)
    $pages = \Meta\Builder::read('pages');
    $pages[$pname]['objects'][$oid]['sqlCols'] = $cols;
    \Meta\Builder::write('pages', $pages);
    echo json_encode(array('errors' => $errors, 'sql_cols' => $cols));
});
Beispiel #7
0
 public static function colNames($table)
 {
     $q = \Meta\Core\Db::getPDO()->prepare("DESCRIBE {$table}");
     $q->execute();
     return $q->fetchAll(\PDO::FETCH_COLUMN);
 }
Beispiel #8
0
<?php

use Meta\Core\Db;
try {
    Db::execute('DROP TABLE group_permissions');
} catch (Exception $e) {
}
try {
    Db::execute('ALTER TABLE sample ADD fld_file VARCHAR(255);');
    Db::execute('CREATE TABLE files(id serial primary key auto_increment, name varchar(255) not null, size bigint, type varchar(255));');
} catch (Exception $e) {
}
Beispiel #9
0
 public static function checkCmds($cmds = array())
 {
     $cmdInstance = new self();
     foreach ($cmds as $cmd) {
         $cmd instanceof \Meta\Builder\Command;
         if (!method_exists($cmdInstance, $cmd->command)) {
             throw new \Exception(t('The command defined "' . $cmd->command . '" not exists in class Command'));
         }
         switch ($cmd->command) {
             case 'executePhpFunction':
                 $callback = $cmd->getArgument(0);
                 if (!function_exists($callback)) {
                     throw new \Exception(t('The defined public function "' . $callback . '" not found in system'));
                 }
                 break;
             case 'fetchFromSql':
                 $sql = $cmd->getArgument(0);
                 $this->errors[] = \Meta\Builder::sqlError($sql);
                 break;
             case 'fetchFromTable':
             case 'saveRecordInTable':
             case 'deleteRowFromTable':
                 $table = $cmd->getArgument(0);
                 if (!Db::tableExists($table)) {
                     throw new \Exception(t("The table '{$table}' was not found on database"));
                 }
                 break;
         }
     }
 }
Beispiel #10
0
 /**
  * Render a View object
  */
 public function renderContent()
 {
     $sql = $this->prepareQuery();
     // instancia o paginador
     $pager = $this->paginate ? new Pager($sql) : null;
     // obtem os registros da view
     if ($pager) {
         $rows = $pager->getRows();
     } else {
         $rows = Db::query($sql)->fetchAll();
     }
     // logica configuravel para quando não encontrar resultados
     if (count($rows) == 0) {
         switch ($this->whenEmpty) {
             // se configurado para esconder view, retorna objeto vazio
             case 'hideView':
                 return null;
                 break;
             case 'showMessage':
                 $emptyMsg = $this->emptyMessage;
                 break;
             case 'showDefaultMessage':
             default:
                 $emptyMsg = t('No records found...');
                 break;
         }
     }
     if ($this->nav->hasItems()) {
         $navbar = $this->nav->render();
     }
     if ($this->enableSearchBox) {
         $searchBox = render('form-search.php');
     }
     return render($this->templateFile, array('rows' => $rows, 'pager' => $pager, 'label' => $this->label, 'columns' => $this->getDisplayCols(), 'emptyMessage' => isset($emptyMsg) ? $emptyMsg : '', 'showLabel' => $this->showLabel, 'searchBox' => isset($searchBox) ? $searchBox : NULL, 'navbar' => isset($navbar) ? $navbar : NULL));
 }
Beispiel #11
0
 public function getRows()
 {
     return \Meta\Core\Db::query($this->sql)->fetchAll();
 }
Beispiel #12
0
 public function buildOptions()
 {
     switch ($this->source) {
         case 'sql':
             // executa sql
             $options = \Meta\Core\Db::query($this->optionsSql)->fetchAll(\PDO::FETCH_KEY_PAIR);
             break;
         case 'function':
             // executa callback
             $func = $this->optionsFunction;
             $options = $func();
             break;
         case 'raw':
             // gera lista
             $options = array();
             foreach (explode("\n", $this->optionsList) as $line) {
                 list($key, $val) = explode('|', $line);
                 $options[$key] = $val;
             }
             break;
         default:
             $options = array();
             break;
     }
     return $options;
 }