/ Gists

Gists

On gists

Redirect from old domain to new

htaccess

htaccess #

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]

On gists

PHP DataContainer class

PHP

data-container.php #

<?php


class dataContainer_helper implements ArrayAccess {
    
    protected $data;
    
    public function __toString() {
        
        return '';
        
    }
    
    // array access
    public function offsetSet($offset, $value) {
        $this->__set($offset, $value);
    }
    
    public function offsetUnset($offset) {
        $this->__unset($offset);
    }
    
    public function offsetExists($offset) {
        
        return $this->__isset($offset);
    }
    
    public function offsetGet($offset) {
        
        //return $this->{$offset};
        return $this->__get($offset);
        
    }
    
    public function __get($name) {
        
        
        
        if(!isset($this->data[$name])) {   
            
            $this->data[$name] = new dataContainer_helper;
            
        }
        
        //var_dump($this->data[$name]);
        
        return $this->data[$name];
        
    }
    
    public function __set($name, $value) {
        
        
        if(is_array($value) && !is_int(key($value))) {
            
            if(!($this->data[$name] instanceof dataContainer_helper)) {
            
                $this->data[$name] = new dataContainer_helper;
            }
            
            foreach($value as $key => $v) {
                
                $this->data[$name]->$key = $v;
                
            }            
            
        } else {
            $this->data[$name] = $value;
        }

    }
    
    
    public function __unset($name) {
        
        unset($this->data[$name]);   
        
    }
    
    public function __isset($name) {
        
        if(isset($this->data[$name])) {
            
            if($this->data[$name] instanceof dataContainer_helper) {
                
                if($this->data[$name]->isNull()) {
                    
                    //var_dump($this->data[$name]);
                    
                    return false;
                    
                }
                
            }
            
            if($this->data[$name] !== null) {
            
                return true;
                
            }
            
        }
        
        return false;
        
    }
    
    public function getContainerData() {
        
        if(is_array($this->data)) {
            
            $data = array();
        
            foreach($this->data as $key => $value) {
                
                if($value instanceof dataContainer_helper) {
                    
                    $data[$key] = $value->getContainerData();
                    
                    if($data[$key] === false) {
                        
                        unset($data[$key]);
                        
                    }
                    
                } else {
                    
                    $data[$key] = $value;
                    
                }
                
            }
            
            return $data;
            
        }
        
        return false;
        
        
        
    }
    
    public function setContainerData($data) {
        
        if(is_array($data)) {
        
            foreach($data as $key => $value) {
                
                $this->$key = $value;
                
            }
            
        } else {
            
            return false;
            
        }
        
    }
    
    public function isNull() {
        
        if($this->data === null) {
            
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
    
    
}


$a = new dataContainer_helper;

echo $a->kkt->b = 'ku';

On gists

List of HTTP headers

PHP

List of HTTP headers.php #

 private function get_status_message(){
            $status = array(
                        100 => 'Continue',  
                        101 => 'Switching Protocols',  
                        200 => 'OK',
                        201 => 'Created',  
                        202 => 'Accepted',  
                        203 => 'Non-Authoritative Information',  
                        204 => 'No Content',  
                        205 => 'Reset Content',  
                        206 => 'Partial Content',  
                        300 => 'Multiple Choices',  
                        301 => 'Moved Permanently',  
                        302 => 'Found',  
                        303 => 'See Other',  
                        304 => 'Not Modified',  
                        305 => 'Use Proxy',  
                        306 => '(Unused)',  
                        307 => 'Temporary Redirect',  
                        400 => 'Bad Request',  
                        401 => 'Unauthorized',  
                        402 => 'Payment Required',  
                        403 => 'Forbidden',  
                        404 => 'Not Found',  
                        405 => 'Method Not Allowed',  
                        406 => 'Not Acceptable',  
                        407 => 'Proxy Authentication Required',  
                        408 => 'Request Timeout',  
                        409 => 'Conflict',  
                        410 => 'Gone',  
                        411 => 'Length Required',  
                        412 => 'Precondition Failed',  
                        413 => 'Request Entity Too Large',  
                        414 => 'Request-URI Too Long',  
                        415 => 'Unsupported Media Type',  
                        416 => 'Requested Range Not Satisfiable',  
                        417 => 'Expectation Failed',  
                        500 => 'Internal Server Error',  
                        501 => 'Not Implemented',  
                        502 => 'Bad Gateway',  
                        503 => 'Service Unavailable',  
                        504 => 'Gateway Timeout',  
                        505 => 'HTTP Version Not Supported');
            return ($status[$this->_code])?$status[$this->_code]:$status[500];
        }
                        
                      
                          private function set_headers(){
            header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
            header("Content-Type:".$this->_content_type);
        }

On gists

curl POST examples

PHP

server.js #

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.post('/data', function (req, res) {
  console.log(req.body);
  res.end();
});

app.listen(3000);

On gists

404 In presenter

Nette

SomePresenter.php #

			$this->getHttpResponse()->setCode(\Nette\Http\Response::S404_NOT_FOUND);
			$this->setView('missing');

On gists

Nette - conditions - retezení

Nette Nette-Forms

example.php #

<?php

1) 
$videoLength = $video->addText('videoLength', 'Délka videa');
$videoLength->addConditionOn($form['article-published'], Form::EQUAL, TRUE)
    ->setRequired();
$videoLength->addCondition(Form::FILLED)
    ->addRule(Form::PATTERN
    
    


2)
$video->addText('videoLength', 'Délka videa')
    ->addConditionOn($form['article-published'], Form::EQUAL, TRUE)
    ->setRequired()
    ->endCondition()
    ->addCondition(Form::FILLED)
    ->addRule(Form::PATTERN, 'Délka musí být zadána ve formátu minuty:sekundy', '[0-9]+:[0-9]{2}');

On gists

Nette - conditions -

Nette Nette-Forms

lambda.php #

<?php

->addCondition(function() use ($form) {
    return ...; //true nebo false
})

On gists

Nette - fileupload from URL

Nette Nette-Forms

fileupload.php #

<?php

$fileUpload = new \Nette\Http\FileUpload([
    'name' => basename($filePath),
    'size' => filesize($filePath),
    'tmp_name' => $filePath,
    'error' => UPLOAD_ERR_OK,
]);

On gists

JS Bin Rating panel via CSS 3 & jQuery // source http://jsbin.com/denukot

jQuery

jsbin.denukot.js #

$('#wrapper div').mouseover(function(){
	
	
	$(this).children('span').addClass('active');
	$(this).prevAll().children('span').addClass('active')
	
});

$('#wrapper div').mouseout(function(){
	
	
$(this).prevAll().children('span').removeClass('active')
		$(this).children('span').removeClass('active');
	
});

On gists

Triggers example

MySql

triggers-example.sql #


CREATE TRIGGER trRaiseTopicCountAll
AFTER INSERT ON forum_commentary
FOR EACH ROW 
BEGIN    
    UPDATE forum_topic 
    SET forum_commentary_count_all = @forum_commentary_count_all +1 
    WHERE forum_topic_id = NEW.forum_topic_id;
    IF (NEW.commentary_parent_id IS NULL) 
        THEN 
            UPDATE forum_topic 
            SET first_commentary_id = NEW.forum_commentary_id 
            WHERE forum_topic_id = NEW.forum_topic_id;
    END IF; 
END;
    
    
CREATE TRIGGER trRaiseTopicCountVisible
AFTER UPDATE ON forum_commentary
FOR EACH ROW 
BEGIN    
    IF (OLD.forum_commentary_status_id <> NEW.forum_commentary_status_id)
        THEN 
            IF (NEW.forum_commentary_status_id = 2) 
            THEN 
                UPDATE forum_topic 
                SET forum_commentary_count_visible = @forum_commentary_count_visible +1 
                WHERE forum_topic_id = NEW.forum_topic_id;
            ELSE 
                UPDATE forum_topic 
                SET forum_commentary_count_visible = @forum_commentary_count_visible -1 
                WHERE forum_topic_id = NEW.forum_topic_id;
            END IF;    
    END IF;
END;