Skip to content

Commit

Permalink
close #19, close #20, close #21
Browse files Browse the repository at this point in the history
  • Loading branch information
127 committed May 10, 2019
1 parent 378ef50 commit 0491c40
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 132 deletions.
2 changes: 2 additions & 0 deletions app/controllers/index_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public function IndexAction() {
$this->view['model']= new SchemaMigrationModel();
$this->view['title'] = 'index action index controller';
}

public function AnotherAction() {}
}
?>
43 changes: 42 additions & 1 deletion app/controllers/sitemap_controller.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
<?php
class SitemapController extends ApplicationController {
private $_excl = [
'sitemap_index',
'sitemap_urls'
];
// Priorities by resource
private $_priorities = [
'defaults' => [
'actions' => [0.5, 'monthly']
],
'index' => [
// 'exclude' => [ 'index' ], //ecxlude action
'priority' => [0.5, 'monthly']
]
];
protected function init() {
header('Content-Type: application/xml; charset=utf-8');
$this->set_render_layout(false);
View::set_extension('.xml.php');
}

public function IndexAction() {

$_t = Router::get_routes_table();
$this->view['index'] = array_diff(array_keys($_t), $this->_excl);
}

public function ModuleAction() {
$this->view['quantity'] = $this->_h->_quantity;
$_c = str_replace('_sitemap.xml', '', trim($_SERVER['REQUEST_URI'], '/'));
$this->view['controller'] = $_c;
$_a = Router::get_controller_actions($_c);
if(isset($this->_priorities[$_c]['exclude'])){
$_ex = $this->_priorities[$_c]['exclude'];
$_a = array_filter($_a, function($el) use ($_ex){
if(!in_array($el, $_ex)){
return $el;
}
});
}
$this->view['actions'] = $_a;
if(isset($this->_priorities[$_c]['priority'])){
$this->view['importance'] = $this->_priorities[$_c]['priority'][0];
$this->view['frequency'] = $this->_priorities[$_c]['priority'][1];
} else {
$this->view['importance'] = $this->_priorities['defaults']['actions'][0];
$this->view['frequency'] = $this->_priorities['defaults']['actions'][1];
}
}

}
?>
2 changes: 2 additions & 0 deletions app/views/index/another.html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Another</h1>
<p>action</p>
15 changes: 15 additions & 0 deletions app/views/sitemap/index.xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?='<?xml version="1.0" encoding="UTF-8"?>'?>

<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$s='';
$lastmod = (new DateTime())->format('c');
foreach($_v['index'] as $resource){
$s .= ' <sitemap>
<loc>https://'.$_SERVER['HTTP_HOST'].'/'.$resource.'_sitemap.xml</loc>
<lastmod>'.$lastmod.'</lastmod>
</sitemap>'."\n";
}
echo $s;
?>
</sitemapindex>
19 changes: 19 additions & 0 deletions app/views/sitemap/module.xml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?='<?xml version="1.0" encoding="UTF-8"?>'?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<?php

$s='';
$lastmod = (new DateTime())->format('c');
foreach($_v['actions'] as $url){
$_a = $url=='index' ? '' : '/'.$url;
$s .= ' <url>
<loc>https://'.$_SERVER['HTTP_HOST'].'/'.$_v['controller'].$_a.'</loc>
<lastmod>'.$lastmod.'</lastmod>
<changefreq>'.$_v['frequency'].'</changefreq>
<priority>'.$_v['importance'].'</priority>
</url>'."\n";
}
echo $s;
?>
</urlset>
41 changes: 24 additions & 17 deletions bin/shpala
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,49 @@ if (version_compare(PHP_VERSION, '5.6.30', '<')) { die ('Minimal requirements: P
date_default_timezone_set('Europe/Moscow');
$GLOBALS['APP_ENV'] = (false === getenv('APP_ENV') ? 'development' : getenv('APP_ENV') );
$GLOBALS['APP_DIR'] = realpath(__DIR__.'/../');
$GLOBALS['TASKS_DIR'] = $GLOBALS['APP_DIR'].'/lib/tasks/';
$GLOBALS['TASKS_DIR'] = $GLOBALS['APP_DIR'].'/lib/tasks/';
$GLOBALS['SHPALA_DIR'] = $GLOBALS['APP_DIR'].'/lib/shpala/';
require_once $GLOBALS['APP_DIR'].'/lib/autoload.php';


if(isset($argv[1]))
if(isset($argv[1])){
$action = $argv[1];
}

function _shpala_help($helptask){
if(file_exists($helptask)){
require_once $helptask;
}
}

$h = $f = $GLOBALS['TASKS_DIR'].'task_help.php';
//default task is help
$help = $GLOBALS['TASKS_DIR'].'task_help.php';
if (!isset($action)){
if(file_exists($h))
require_once $h;
_shpala_help($help);
} else {
if(preg_match("/db:/", $action) == 1) {
$els = explode(':', $action);
$last_el = array_pop($els);
$f = $GLOBALS['TASKS_DIR'].implode('/', $els).'/task_'.$last_el.'.php';
$task = $GLOBALS['TASKS_DIR'].implode('/', $els).'/task_'.$last_el.'.php';


if(file_exists($f)){
if(file_exists($task)){
$config = (require_once($GLOBALS['APP_DIR'].'/config/database.php'))[$GLOBALS['APP_ENV']];
$db = new Database(false, false);
$db->set_connect($config);
$connect = $db->get_connect();
$dbname = ($db->get_config())['database'];
require_once $f;
require_once $task;
} else {
if(file_exists($h))
require_once $h;
_shpala_help($help);
}
} else {
$f = $GLOBALS['TASKS_DIR'].'task_'.$action.'.php';
if(file_exists($f))
require_once $f;
else
if(file_exists($h))
require_once $h;
$task = $GLOBALS['TASKS_DIR'].'task_'.$action.'.php';
if(file_exists($task)){
require_once $task;
} else {
_shpala_help($help);
}
}
}
exit(0);
?>
8 changes: 6 additions & 2 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
'root' => 'index#index',
'resources' => [
'index',
'sitemap' => [
'sitemap_index' => [
'path'=>'sitemap.xml',
'as' =>'sitemap#index'
]
],
'sitemap_urls' => [
'regexp'=>'/(.*)_sitemap.xml/',
'as' =>'sitemap#module'
],
]
];
?>
17 changes: 15 additions & 2 deletions lib/shpala/base_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class BaseController {
public $resource;
public $i18n;
private $_render_layout = true;
private $_render_action = true;

public function __construct(Resource &$resource) {
$this->resource = $resource;
Expand All @@ -15,8 +16,12 @@ public function __construct(Resource &$resource) {
$this->params = $resource->router->params;
$this->view = $resource->view;

if($resource->i18n) $this->i18n = $resource->i18n->strings;
if($resource->helpers) $this->helpers = $resource->helpers;
if($resource->i18n){
$this->i18n = $resource->i18n->strings;
}
if($resource->helpers){
$this->helpers = $resource->helpers;
}

$this->call_init();
return $this;
Expand All @@ -39,5 +44,13 @@ public function set_render_layout($flag=true){
public function get_render_layout(){
return $this->_render_layout;
}

public function set_render_action($flag=true){
$this->_render_action = $flag;
}

public function get_render_action(){
return $this->_render_action;
}
}
?>
57 changes: 36 additions & 21 deletions lib/shpala/base_record.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ public function update(array $params, bool $ignore=false){
}

public function find($params){
if(is_numeric($params))
if(is_numeric($params)){
$this->select()->where([$this->_pkey=>$params]);
}
if(is_array($params) && count($params)>0){
$this->select();
$s = [];
Expand Down Expand Up @@ -172,8 +173,11 @@ public function select($params=null){
return $this;
}

public function count(string $params){
$this->select('COUNT('.$params.')');
public function count(string $params, string $as=''){
if($as!=''){
$as = ' as '.$as;
}
$this->select('COUNT('.$params.')'.$as);
return $this;
}

Expand Down Expand Up @@ -233,15 +237,14 @@ public function not($params){
}
if(is_array($params)){
$_cnt = count($params);
if($_cnt==1)
if($_cnt==1){
$column = (array_keys($params))[0];

}
// User.where.not(name: %w(Ko1 Nobu))
// # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
if($_cnt==1 && is_array($params[$column])){
$_s=[];
foreach($params[$column] as $p){
print_r($p);
$k = $this->_get_alias();
$_s[]=$k;
$this->_sql_binds[$k] = $p;
Expand Down Expand Up @@ -283,14 +286,17 @@ public function or(){
}

public function where($params=null){
if(!isset($this->_sql_params['where']))
if(!isset($this->_sql_params['where'])){
$this->_sql_params['where'] = 'WHERE ';
else
$this->_sql_params['where'] .= ' AND ';
if(!isset($params))
} else {
$this->_sql_params['where'] .= ' AND ';
}
if(!isset($params)) {
return $this;
if (is_string($params))
}
if (is_string($params)){
$this->_sql_params['where'] .= $params;
}
if(is_array($params) && count($params)>0){
if($this->_is_assoc($params)){
$this->_sql_params['where'] .= implode(', ', array_map(function($c, $v){
Expand All @@ -314,8 +320,9 @@ public function where($params=null){
//PDO fails to pass symols on order
public function order($params){
$this->_sql_params['order'] = 'ORDER BY ';
if(is_string($params))
if(is_string($params)){
$this->_sql_params['order'] .= $params;
}
if(is_array($params) && count($params)>0) {
$this->_sql_params['order'] .= implode(', ', array_map(function($k, $v){
$key = is_numeric($k) ? $v : $k;
Expand All @@ -328,18 +335,21 @@ public function order($params){

public function group($params){
$this->_sql_params['group'] = 'GROUP BY ';
if(is_string($params))
if(is_string($params)){
$this->_sql_params['group'] .= $params;
if(is_array($params) && count($params)>0)
}
if(is_array($params) && count($params)>0){
$this->_sql_params['group'] .= implode(', ', $params);
}
return $this;
}

public function having($params){
// if(!isset($this->_sql_params['group']) check if order section isset
$this->_sql_params['having'] = 'HAVING ';
if(is_string($params))
if(is_string($params)){
$this->_sql_params['having'] .= $params;
}
if(is_array($params) && count($params)>0){
$tpl = array_shift($params);
foreach($params as $p){
Expand Down Expand Up @@ -367,10 +377,12 @@ public function offset(int $offset){
}

public function run(){
if(count(array_intersect($this->_proirity[0], array_keys($this->_sql_params)))==0)
if(count(array_intersect($this->_proirity[0], array_keys($this->_sql_params)))==0){
$this->select();
if(!isset($this->_sql_params['from']) && !isset($this->_sql_params['update']))
}
if(!isset($this->_sql_params['from']) && !isset($this->_sql_params['update'])){
$this->from();
}
foreach($this->_proirity as $part){
foreach($part as $v){
if(isset($this->_sql_params[$v])){
Expand All @@ -382,10 +394,11 @@ public function run(){
$stmnt = $this->_db->prepare($this->_sql_string);
$this->_bind_values($stmnt, $this->_sql_binds);
$stmnt->execute();
if(isset($this->_sql_params['update']) || isset($this->_sql_params['delete']))
if(isset($this->_sql_params['update']) || isset($this->_sql_params['delete'])){
$r = $stmnt->rowCount();
else
} else {
$r = $stmnt->fetchAll(PDO::FETCH_ASSOC);
}
$this->_cleanup();
return $r;
} catch(PDOException $e) {
Expand All @@ -396,8 +409,9 @@ public function run(){
}

private function _bind_values(PDOStatement &$stmnt, array $values){
if(count($values)==0)
if(count($values)==0){
return $stmnt;
}
foreach($values as $k=>$v){
switch( true ) {
case is_numeric($v):
Expand All @@ -422,8 +436,9 @@ private function _get_alias(){
}

private function _is_assoc(array $arr){
if(array()===$arr)
if(array()===$arr){
return false;
}
return array_keys($arr) !== range(0, count($arr) - 1);
}

Expand Down
Loading

0 comments on commit 0491c40

Please sign in to comment.