Esempio n. 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;
 }
Esempio n. 2
0
 public function querySql($sql, $messageError)
 {
     $result = \Meta\Core\Db::query(Builder::replaceSQL($sql))->fetchColumn();
     if (!$result) {
         $this->error = $messageError;
     }
     return $result;
 }
Esempio n. 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;
 }
Esempio n. 4
0
 protected function fetchFromSql($sql)
 {
     if (isset($_GET['id'])) {
         $row = Db::query(Builder::replaceSQL($sql))->fetch(\PDO::FETCH_ASSOC);
         if ($this->context instanceof \Meta\Builder\Object\Form) {
             $this->context->setData($row);
         }
     }
     return true;
 }
Esempio n. 5
0
 public static function listGroups()
 {
     return \Meta\Core\Db::query("SELECT id, name FROM groups ORDER BY name")->fetchAll(\PDO::FETCH_KEY_PAIR);
 }
Esempio n. 6
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;
});
Esempio n. 7
0
 public static function loadRecord($table, $pk_value, $pk_name = null)
 {
     if (!$pk_name) {
         $pk_name = 'id';
     }
     return \Meta\Core\Db::query("SELECT * FROM {$table} WHERE " . \Meta\Core\Db::where(array($pk_name => $pk_value)));
 }
Esempio n. 8
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));
 }
Esempio n. 9
0
 public function getRows()
 {
     return \Meta\Core\Db::query($this->sql)->fetchAll();
 }
Esempio n. 10
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;
 }