Friday 27 May 2016

Updating Column Types with Laravel's Schema Builder

Hi Everybody,

Today we will discuss how to update column type with Laravel 5.1 Schema Builder.

Laravel makes database migrations very easy with its Schema Builder. It has a lot of built-in methods to add tables, columns, indexes, etc. However, it's easy to forget that you can do so much more with just plain old SQL queries.

I needed to change the type of a column from VARCHAR to TEXT. Here's what I ran in a migration file:


public function up()
{
    DB::statement('ALTER TABLE flavours MODIFY COLUMN description TEXT');
}

public function down()
{
    DB::statement('ALTER TABLE flavours MODIFY COLUMN description VARCHAR(255)');
}   

I think this will make you little help.

Thanks.

Friday 20 May 2016

Advertisement script in Jquery

Hi Everybody,


Today we will discuss about advertisement script in Jquery.

All we know in today timing all product required advertisement, so in web application also advertisement display on third party website.When we click on these advertisement, main website of product open either in browser new tab or in any pop up or iframe etc.

For this we require two things

1. Script that is hosted on product website server(inside your project_dir/js_dir/adscript.js )


Code inside your project_dir/js_dir/adscript.js

var siteAddress =  '//yourdomain.com/';
(function() {
    include_jQueryFilesToPage();
})();

function afterloadScript() {
    var adddiv = $('<div class="yourproduct-ad"><div class="yourproduct yourproduct_1279518" id="yourproduct_1279518" data-serve="CV7DPKT"><a rel="nofollow" target="_blank" id="yourproduct_5568645" title="Product Title" class="ad0 odd " href="'+siteAddress+'/route_of_page_you_want_to_open/'" data-title="Product Title" data-width="700" data-height="500"><img width="200" height="200" alt="" src="'+siteAddress+'/images/adscript.png"></a></div></div>');
    $("body").append(adddiv);
    adjustAd();
    var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" id="yourproduct_adscreen" allowfullscreen></iframe>');
    dialog = $("<div id='oopenDiv' style='padding:0'></div>").append(iframe).appendTo("body").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        width: "auto",
        height: "auto",
        close: function () {
            iframe.attr("src", siteAddress+"route_of_page_you_want_to_open");
        }
    });

    $("#yourproduct_5568645").on("click", function (e) {
        e.preventDefault();
        var src = $(this).attr("href");
        var title = $(this).attr("data-title");
        var width = $(this).attr("data-width");
        var height = $(this).attr("data-height");
        iframe.attr({
            width: +width,
            height: +height,
            src: src
        });
        dialog.dialog("option", "title", title).dialog("open");
    });
    setTimeout($('body').addClass('add_script_wrapper'), 2000);
}


function adjustAd(){
    var iframeHtml = $("body").find(".yourproduct-ad").html();
    $(".adsbyproduct").parent().append('<div class="product-ad">'+iframeHtml+'</div>');
    $("body").find(".yourproduct-ad").remove();
}


function include_jQueryFilesToPage() {
    var jqCSSFilePath = siteAddress + 'css/code.jquery.com-ui-1.10.3-themes-smoothness-jquery-ui.css';
    var jqStyleCSSFilePath = siteAddress + 'css/style.css';
    var jqCoreFilePath = siteAddress + 'js/code.jquery.com-jquery.js';
    var jqUIFilePath = siteAddress + 'js/code.jquery.com-ui-1.10.3-jquery-ui.js';
    var head = document.getElementsByTagName('head')[0];
    // jQuery CSS jnclude
    var jqCSS = 'cssIDJQ';  // you could encode the css path itself to generate id.
    if (!document.getElementById(jqCSS)) {
        var link = document.createElement('link');
        link.id = jqCSS;
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = jqCSSFilePath;
        link.media = 'all';
        head.appendChild(link);
    }

    // style CSS jnclude
    var jqStyleCSS = 'styleCssIDJQ';  // you could encode the css path itself to generate id.
    if (!document.getElementById(jqStyleCSS)) {
        var link = document.createElement('link');
        link.id = jqStyleCSS;
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = jqStyleCSSFilePath;
        link.media = 'all';
        head.appendChild(link);
    }

    // Core jQuery include
    var jqc = "coreFileRefIDJQ";
    if (!document.getElementById(jqc)) {
        var coreJs = document.createElement('script');
        coreJs.onload = function () {
            //alert("Script loaded and ready");
        };
        coreJs.src = jqCoreFilePath;
        head.appendChild(coreJs);
    }
    // jQueryUI include
    var jqUI = "uiFileRefIDJQ";
    if (!document.getElementById(jqUI)) {
        var coreJsUi = document.createElement('script');
        coreJsUi.onload = function () {
            afterloadScript();
        }
        coreJsUi.src = jqUIFilePath;
        head.appendChild(coreJsUi);
    }
}


2. One line advertisement script that is used by third party website, where you want to display your product advertisement.


Now finally the one line advertisement script(given below) copy and paste the code from below, in every page of the website(where you want to display your ad), just before the closing body tag(inside a div).

<script type="text/javascript" async src= "yourdomain/js/adscript.js"></script><ins class="adsbyproduct"></ins>


Thanks

Friday 13 May 2016

IE Blocking iFrame Cookies

Hi Everybody,

Today we will discuss about "IE Blocking iFrame Cookies".

I have faced a problem in my applications not running correctly from inside an iFrame. I tried it out and it looked like everything worked great in Safari and Firefox but not IE6 or IE7. It took me a few failed attempts to fix it before I decided it must be a session problem. After firing up a packet sniffer it became obvious the cookie with the session ID was not being passed.

The problem lies with a W3C standard called Platform for Privacy Preferences or P3P for short. You can read all about the boring stuff via the link or else just install the P3P Compact Policy header below. This will allow Internet Explorer to accept your third-party cookie. You will need to send the header on every page that sets a cookie.


PHP:

header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');

Friday 6 May 2016

How to send email in Laravel 5.1 using SendGrid

Hi Everybody,


Laravel comes with an email sending library built in, so we just need to set it to use SendGrid over SMTP. Check out the docs for Laravel’s mailer for details.

In app/config/mail.php you need to configure these settings:


<?php

return array(
  'driver' => 'smtp',
  'host' => 'smtp.sendgrid.net',
  'port' => 587,
  'from' => array('address' => 'from@example.com', 'name' => 'John Smith'),
  'encryption' => 'tls',
  'username' => 'sendgrid_username',
  'password' => 'sendgrid_password',
);

?>

You can use Laravel’s Mail class just like you normally would, but all email will be sent through SendGrid!


<?php

Mail::send('emails.demo', $data, function($message)
{
    $message->to('jane@example.com', 'Jane Doe')->subject('This is a demo!');
});

?>

Adding a category or custom field


Categories in sendgrid allow you to split your statistics into categories. An example would be if you have a white labeled service you can split your statistics by the user login.

Another useful feature of sendgrid is the notifications. If you want to complete the feedback loop for your product you can pass identifiers as a header which relate to a record in your database which you can then parse the notifications against that record to track deliveries/opens/clicks/bounces.

   

<?php

   Mail::send('emails.view', $data, function ($message)
    {
        $data['category']                  = 'category';
        $data['unique_args']['variable_1'] = 'abc';

        $header = $this->asString($data);

        $message->getSwiftMessage()->getHeaders()->addTextHeader('X-SMTPAPI', $header);

        $message->to('jane@example.com', 'Jane Doe')->subject('This is a demo!');
    });


    private function asJSON($data) // set as private with the expectation of being a class method
    {
        $json = json_encode($data);

        $json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);

        return $json;
    }

    private function asString($data) // set as private with the expectation of being a class method
    {
        $json = $this->asJSON($data);
        $str  = wordwrap($json, 76, "\n   ");

        return $str;
    }
?>

Thanks.