remithomas/rt-extends

There is no license information available for the latest version (1.0.1) of this package.

ZF2 useful tools

1.0.1 2013-05-22 07:58 UTC

This package is not auto-updated.

Last update: 2024-12-16 11:57:10 UTC


README

A list of ZF2 useful tools. To provide some utilities to generate list of languages, Sql query (on duplicate key update), flashmessenger, countries states and more

Features / Goals

  • Sql : Db\Sql\DuplicateInsert ON DUPLICATE KEY UPDATE option
  • Validators : Date is later, is Earlier
  • Uri
    • Thumbnail : Get Thumbnail from URI
    • Type : Get the type of the media
    • Meta : Get metadata of the media
  • Useful
    • Location : List of countries, states list, zipcode search
    • I18n : continents list, languages list, Timezones list
    • Data : Fake data Lorem Ipsum generator
    • File : create Zip Archive, unzip archive, get Favicon
    • PHP : sprintf with dynamic variables
  • View\Helper : extended Flash messenger (sub-message and messages are translated)
    • Date : CountDown
    • BodyClasses : manage your body CSS classes from controller
  • Snippets : create basic CSRF quickly

Ask for contributions

Some ideas to implement to this useful code ? Or ups some errors appear

Requirements

Installation

How to install ?

Using composer.json

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "minimum-stability": "dev",
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "dev-master",
        "remithomas/rt-extends": "dev-master"
    }
}

Activate the module :

application.config.php

<?php
return array(
    'modules' => array(
        'Application',
        'RtExtends',
    )
);
?>

Examples

Db\Sql

Sql class including DuplicateInsert.php

$value = array(
    'user_id' => 2,
    'value' => 'myvalue'
);

$sql = new \RtExtends\Db\Sql($adapter, 'mytable');
        
$DuplicateInsert = $sql->duplicateInsert();
$DuplicateInsert->values($values);
        
$sqlString = $sql->getSqlStringForSqlObject($DuplicateInsert);
$adapter->query($sqlString, Adapter::QUERY_MODE_EXECUTE);

Db\Sql\DuplicateInsert

<?php
$value = array(
    'user_id' => 2,
    'value' => 'myvalue'
);
            
$DuplicateInsert = new RtExtends\Db\Sql\DuplicateInsert("user");
$DuplicateInsert->values($value);

$statment = $this->dbAdapter->createStatement(); 
$DuplicateInsert->prepareStatement($this->dbAdapter, $statment); 
$statment->execute(); 
?>

the above code generates this query

INSERT INTO `user` (`user_id`, `value`) 
VALUES (2, 'myvalue')
  ON DUPLICATE KEY UPDATE `user_id`=VALUES(`user_id`), `value`=VALUES(`value`);

Validator\Date\IsLater

public function getInputFilterSpecification()
{
    return array(
        'datestart' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => "RtExtends\Validator\Date\IsLater",
                    'options' => array(
                        'min' => date ("d F Y - H:i", mktime()),
                        'format' => 'd F Y - H:i',
                        'timezone' => 'Europe/London'
                    )
                )
            ),
        ),
    );
}

Validator\Date\IsEarlier

public function getInputFilterSpecification()
{
    return array(
        'dateend' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => "RtExtends\Validator\Date\IsEarlier",
                    'options' => array(
                        'max' => date ("d F Y - H:i", mktime()),
                        'format' => 'd F Y - H:i',
                        'timezone' => 'Europe/London'
                    )
                )
            ),
        ),
    );
}

Uri

Uri\Thumb

Get list of thumbnail of an URI

$uri = "http://www.dailymotion.com/video/x9003r_pac-man-remi-gaillard_fun";
$uri = "http://www.youtube.com/watch?v=1VVkIOxRcX0"; // youtube
$thumbUri = new \RtExtends\Uri\Thumb();
var_dump($thumbUri->getThumbs($uri));

// limit the number of thumbnails
var_dump($thumbUri->getThumbs($uri, 2));

Get one thumbnail of an URI

$uri = "http://framework.zend.com/blog/"; 
$thumbUri = new \RtExtends\Uri\Thumb();
var_dump($thumbUri->getThumb($uri));

Uri\Type

Get the type of the media

$typeUri = new \RtExtends\Uri\Type();
$typeUri->getType("http://www.youtube.com/watch?v=1VVkIOxRcX0"); // return youtube

Uri\MetaData

Get the meta of the media. All URI gets title and description

$metadataUri = new \RtExtends\Uri\MetaData();
$metadataUri->getMetaData("http://www.youtube.com/watch?v=1VVkIOxRcX0"); 
/*
return array(
    'title' => 'media title',
    'description' => 'media description',
    // ...
)
*/

Useful\I18n\Languages

List of languages

var_dump(RtExtends\Useful\I18n\Languages::getSimpleCodeLanguages());
// array("fr"=>"Français","en"=>"English",'pt'=>'Português',....)
var_dump(RtExtends\Useful\I18n\Languages::getLanguages());
// array('lv_LV'=>'Latvija - Latviešu','en_LB'=>'Lebanon - English','lt_LT'=>'Lietuva - Lietuvių','fr_LU'=>'Luxembourg - Français',,....)

Useful\Location

List of countries (array returned)

\RtExtends\Useful\Location\Countries::getCountries();

Zipcode

\RtExtends\Useful\Location\Ziptastic::dataLocation("US", "33330");
/*
Return a stdClass object
object(stdClass)#748 (3) {
  ["city"] => string(15) "Fort Lauderdale"
  ["state"] => string(7) "Florida"
  ["country"] => string(2) "US"
}
*/

States of countries (DE,FR,US are available)

\RtExtends\Useful\Location\Country\Us::states();
\RtExtends\Useful\Location\Country\Us::statesFIPS(); // FIPS codes
\RtExtends\Useful\Location\Country\Fr::states();
\RtExtends\Useful\Location\Country\De::states();

Counties of countries (FR,US are available)

\RtExtends\Useful\Location\Country\Fr::counties();
\RtExtends\Useful\Location\Country\Fr::countiesStructured(); // by states
\RtExtends\Useful\Location\Country\Us::counties();
\RtExtends\Useful\Location\Country\Us::countiesStructured(); // by states

Useful\File\Zip

Create zip file

<?php
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
/**
 *
 * @param array $files
 * @param string $destination
 * @param bool $overwrite
 * @return boolean 
 */
RtExtends\Useful\File\Zip::createZip($files, 'path/destination/myzipfile.zip', true); 
?> 

Unzip archive

<?php
RtExtends\Useful\File\Zip::unzip('path/to/archive', 'path/destination'); 
?> 

Useful\Data\Fake

Lorem Ipsum : Paragraph

Generate paragraphs

<?php 
// simple use : generate only one paragraph (element p)
echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(); 

// generate 3 paragraphs (element <p>) with class 'lead'
echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(3,"p",array("class"=>"lead")); 

// generate 2 divs (element <div>) with class 'alert alert-info'
echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(2,"div",array("class"=>"alert alert-info")); 
?>

If you don't need to get a list of random paragraphs, just do like that

echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(2,"div",array("class"=>"alert alert-info"),false); 
?>

Lorem Ipsum : Words

// simple use : 10 words
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10);

// include in tag (ex: <p class='lead>>)
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10,"p",array("class"=>"lead"),"!");

// no random option
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10,"p",array("class"=>"lead"),"!", false);

// no random option but a special line
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10,"p",array("class"=>"lead"),"!", 3);

Useful\Php\String

$data = array("this", "cool");
echo RtExtends\Useful\Php\String::sprintfArray("%s is %s", $data);

$data = array(
    "otherway" => "Or",
    "second" => "like that"
);
echo RtExtends\Useful\Php\String::sprintfArray("%(otherway)s maybe %(second)s !", $data);

View\Helper\ExtendedFlashMessenger

In your layout

echo $this->extendedFlashMessenger(true); // true is to get also current messages

In your controller

// Success !
$flashMessage = new FlashMessage();
$flashMessage->setTitle("bravo");
$flashMessage->setMessages("Yes you did");

$this->flashmessenger()->addSuccessMessage($flashMessage);

// Error !
$flashMessage = new FlashMessage();
$flashMessage->setTitle("Ups");
$flashMessage->setMessages("This is wrong");

$this->flashmessenger()->addErrorMessage($flashMessage);

With variable

    
// Ups error ! (with sub messages
$subMessage = new FlashMessageSub();
$subMessage->setMessage("Ups %(name)s"); // first message
$subMessage->setVariables(array(
    'name' => "John Doe"
));

$subMessageSecond = new FlashMessageSub();
$subMessageSecond->setMessage("Please  check here %(url)s"); // first message
$subMessageSecond->setVariables(array(
    'url' => "http://php.net"
));

$flashMessage = new FlashMessage();
$flashMessage->setTitle("Sorry");
$flashMessage->setMessages(array($subMessage,$subMessageSecond));
                
$this->flashmessenger()->addErrorMessage($flashMessage);

Date view helper : countdown

date_default_timezone_set('America/Montreal');
$date = new \DateTime('NOW');

// countdown result in ARRAY
$countDown = $this->rtCountDown($date->getTimestamp()+167890, $date->getTimestamp(), true);

// string (is default param)
$countDown = $this->rtCountDown($date->getTimestamp()+167890, $date->getTimestamp(), false);
<<<<<<< HEAD

BodyClasses view helper

Inside your controller

$this->BodyClasses()->addClass("my-css-class");
$this->BodyClasses()->addClass(array("my-css-class", "blue-bck"));

Inside your view script

?><body class="<?php echo $this->rtBodyClasses()->render(); ?>">
=======
>>>>>>> FETCH_HEAD

Snippets

Some quick snippets

Snippets\Form\Element

$form = new Form('my-form');
$form->add(\RtExtends\Snippets\Form\Element\Csrf::getCreateElementArray("crsf", 60*60));
/* generate
$form->add(array(
    'type' => 'Zend\Form\Element\Csrf',
    'name' => 'crsf',
    'options' => array(
        'csrf_options' => array(
            'timeout' => '3600'
        )
    )
));*/

Snippets\Js

Jquery

<?php 
echo \RtExtends\Snippets\Js\Jquery::getUrl(); 
echo "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js";

echo \RtExtends\Snippets\Js\Jquery::getUrl("2.0.0"); 
echo "http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js";
?>

Jquery UI

<?php 
echo \RtExtends\Snippets\Js\Jqueryui::getUrl(); 
echo "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js";
?>

Thanks

Todo

  • many other validators
  • Fake data (more tool)
  • some good helpers
  • Currency
  • More countries