Friday, February 26, 2016

Yii2, Gauge and also Google Chart

I needed to put a gauge to a dashboard in my application so I started looking for widgets that could do the job.
I found two:
yii2-justgage which is based on this component canv-gauge. Which has nice graphics.
And Google Charts which are very easy to use and a little bit more clean design which was more suitable to my case.

yii2-google-chart

The good part of this widget is that it allows you to use not only the gauge but all google chart apis, even maps.
As you can see in docs, to use it is as simple as this:

            echo GoogleChart::widget( array('visualization' => 'Gauge', 'packages' => 'gauge',
echo GoogleChart::widget( array('visualization' => 'Gauge', 'packages' => 'gauge', 'data' => array( array('Label', 'Value'), array('Memory', 80), array('CPU', 55), array('Network', 68), ), 'options' => array( 'width' => 400, 'height' => 120, 'redFrom' => 90, 'redTo' => 100, 'yellowFrom' => 75, 'yellowTo' => 90, 'minorTicks' => 5 ) ));

Monday, February 22, 2016

Integrating yii2 with Paypal


When using paypal to receive payments in your website you can decide develop the integration from scratch using Paypal's API or you can use a pre-existing class.
In my case I used this solution PayPal extension for the Yii2 which is very easy to use and has a reasonable documentation.
This StackOverflow post was also valuable when trying to understand how to use the API provided by Paypal.

Thursday, February 18, 2016

Hide buttons in gridview depending on variable

If in Yii2 you want to customize ActionColumn on Gridview widget depending on variable, e.g. status = 1, then you could use this code:

// Comment
     [  
        'class' => 'yii\grid\ActionColumn',
        'contentOptions' => ['style' => 'width:260px;'],
        'header'=>'Actions',
        'template' => '{view} {delete}',
        'buttons' => [

            //view button
            'view' => function ($url, $model) {
                return Html::a('View', $url, [
                            'title' => Yii::t('app', 'View'),
                            'class'=>'btn btn-primary btn-xs',                                  
                ]);
            },
        ],

        'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {
                $url ='/jobs/view?id='.$model->jobid;
                return $url;
            }
        }

       ],
Source and discussion

Another sample:

// Comment
['class' => 'yii\grid\ActionColumn',
    'template' => '{update} {delete}',
    'buttons' => [
        'delete' => function ($url, $model, $key) {
            if ($model->fk_status_id == 1) {
                return Html::a('<span class=\'glyphicon glyphicon-trash\'></span>', $url, ['data-pjax' => 0, 'data-method' => 'post', 'data-confirm' => 'Are you sure you want to delete this item?', 'aria-label' => 'Delete', 'title' => 'Delete']);
            }
         },
    ],
],

Hello world

This blog was made in order to help myself concentrate in one place some commands, widgets, tips and tricks I learn while programming in Yii2 and also PHP stuff.
It is mainly done for personal use, the better place to find help is through documentation, but if it helps you somehow than I am grateful for your visit.