Thursday, 28 January 2016

Confirm Message when refresh or close or forward or backward the page via Javascript and AngularJs

To prevent accidental closing of page on the browser, you can set the Confirm Message when refresh or close or forward or backward the page via Javascript and AngularJs.

On Plain javascript, however you can also add the below code into AngularJs Controller
// Event Trigger when refreshing/Closing the page.
        var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compatable

        myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
            var confirmationMessage = 'Submission form not complete yet, Are you sure you want to leave this page?';  // a space
            (e || window.event).returnValue = confirmationMessage;
            return confirmationMessage;
        });        

On AngularJs State Changes, put the below code inside the particular controller.
// Event Trigger when clicking browser Back or Forward buttons
        $scope.$on('$locationChangeStart', function( event ) {
            $scope.pagePreventAlert();
            event.preventDefault();
        });

Sample AlertService 
I have created the sample AlertService AngularJs Service to prompt the alert, Please add the below factory into your code.
app.factory('AlertService', ['$rootScope', '$mdDialog', '$mdMedia',
    function ($rootScope, $mdDialog, $mdMedia) {

        var AlertService = {};

        return {

            onBeforeUnloadAlert: function(aData) {

                var data = aData;

                // Event Trigger when refreshing/Closing the page.
                var myEvent = window.attachEvent || window.addEventListener;
                var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compatable

                myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
                    var confirmationMessage = data.msgContent; // or a space
                    (e || window.event).returnValue = confirmationMessage;
                    return confirmationMessage;
                });

                // Event Trigger when clicking browser Back or Forward buttons
                $rootScope.$on('$locationChangeStart', function(e) {

                    // Appending dialog to document.body to cover sidenav in docs app
                    // Modal dialogs should fully cover application
                    // to prevent interaction outside of dialog
                    $mdDialog.show(
                        $mdDialog.alert()
                            //.parent(angular.element(document.querySelector('#popupContainer')))
                            .clickOutsideToClose(true)
                            .title(data.msgTitle)
                            .textContent(data.msgContent)
                            //.ariaLabel('Alert Dialog Demo')
                            .ok('Ok')
                            .targetEvent(e)
                    );

                    e.preventDefault();
                });
            }

        };

        return AlertService;
    }
])

And, in Your_Angular_Controller, please add "AlertService" on the controller parameter and include the below function it on each controller,
        // Page/Stage changes alert
        AlertService.onBeforeUnloadAlert({
            msgTitle: 'Form Submission Alert',
            msgContent: 'The Submission form not complete, please continue to submit the form or you may lose the information.'
        });

Wednesday, 27 January 2016

Connect to PHP with Oracle database via configuring Oracle Instant Client for Linux and Windows

PS: Create the Databases on Oracle after installing the Oracle Express Database XE 11.2, then follow the below step to enable OCI to connect to PHP.

1. Open the php.ini and uncomment the below line.
extension=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client

2. Download the php_oci8_11g.dll files from the link below based on your version, find the "Thread Safe (TS) x86" version.

https://pecl.php.net/package/oci8/2.0.8/windows

Then, unzip the files and copy to the php/ext/ folder.

3. Next, download the "Instant Client Package - Basic" for Windows from the Oracle Instant Client page.
Because PHP is 32 bit, download the 32 bit version of Instant Client from here.

Package Name
Instant Client Package - Basic: All files required to run OCI, OCCI, and JDBC-OCI applications 
Download instantclient-basic-nt-11.2.0.4.0.zip (51,477,933 bytes)

Once downloaded, unzip the Instant Client files to C:\instantclient_11_2

4. Then, edit the Windows PATH environment setting and add C:\instantclient_11_2.
For example, on Windows XP, follow Start -> Control Panel -> System -> Advanced -> Environment Variables and edit PATH in the System Variables list.

Normally, you need to reboot Windows so the new environment is correctly set.

5. If does not work for some reason, please check the files path and location once again.

6. Also, if still does not work, you may try download this package "Microsoft Visual C++ 2012 SP1 Redistributable Package (x64)" and install it.
 (I am not sure when we would require to install it, try without it first as it may not require on your case).

Find more information for Linux and other stuff here...

Thursday, 7 January 2016

To preview the executing or last Query Log in Controllers or Models in Laravel 5

To view/preview the executing query or last executed Query Log in Laravel 5 (including Laravel 4.2, Laravel 5.0, Laravel 5.2), please add the below code and run it.
 
// This is the Sample Controller
class SampleController extends Controller
{
 public function sampleControllerFunction()
 {
  $this->owner_id = 5;
  
  // Enable the Laravel Database Query Log
  DB::enableQueryLog(); 
  
  // Run your Query here
  $item = SampleModel::where('owner_id', $this->owner_id)->first();
  SampleModel::where('owner_id', $owner_id)->forceDelete();

  // Fetch and Print the Last Database Query Log
  print_r( DB::getQueryLog() ); 
 }
}

// This is the Sample Model
class SampleModel extends Model
{
 /**
     * The database table used by the model (mysql).
     *
     * @var string
     */
 protected $table = 'owner_list';

    /**
     * The attributes that are mass assignable..
     *
     * @var string
     */
    protected $fillable = ['id', 'owner_id'];
 
 public function getItemsList()
 {
  // Enable the Laravel Database Query Log
  DB::enableQueryLog(); 

  // Run your Query here
  $item = static::where('owner_id', $this->owner_id)->first();

  // Fetch and Print the Last Database Query Log
  print_r( DB::getQueryLog() ); 
 }
 
}


Please comment me if you have any queries


Monday, 4 January 2016

Adding Foreign Key on the Existing Table in Laravel 5.2 using php artisan command

To add a foreign key to the existing Table in Laravel 5.2 using php artisan command

Please run the below command:

  
// user_id: This is the name of your column on this format
// property_list: This is the name of reference table (e.g. to table, not from table)

$ php artisan make:migration add_foreign_key_for_user_id_column_to_property_list_table --table=property_list

Find below the sample class of the foreign key migration table
 
class AddForeignKeyForUserIdColumnToPropertyListTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('property_list', function (Blueprint $table) {
            //
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('NO ACTION')->onDelete('NO ACTION');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('property_list', function (Blueprint $table) {
            //
            $table->dropForeign('property_list_user_id_foreign');
        });
    }
}


Wednesday, 30 December 2015

To copy the local files to Remote Linux Server (Amazon EC2 Instance)

To copy the local files to Remote Linux Server (Amazon EC2 Instance) :

First, generate and dowmnload the ec2-instance-key-pair.pem file form AWS EC2 to your computer.
To do that follow the instruction here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html

Then run the below command by locating the .pem file and name of file to copy and location of directory into and AWS server:

scp -i /c/KeyFolderPath/ec2-instance-key-pair.pem /c/Users/YourFolderPath/instantclient-basic-linux.x64-11.2.0.4.0.zip ec2-username@ec2-198-51-100-1.compute-1.amazonaws.com:/home/destination_folder_path

To find more information here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Thursday, 26 November 2015

Bash (npm install) - Couldn't install optional dependency: Unsupported

If you are tyring to install any NPM packages, but stuck in the middle facing below issue:
"Couldn't install optional dependency: Unsupported"

npm WARN install Couldn't install optional dependency: Unsupported

> bufferutil@1.2.1 install C:\xampp\htdocs\myProject\node_modules\bufferutil
> node-gyp rebuild

C:\xampp\htdocs\wandle\node_modules\bufferutil>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node  rebuild )
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:116:14)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:71:11
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:82:15)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\xampp\htdocs\myProject\node_modules\bufferutil
gyp ERR! node -v v5.1.0
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok
npm WARN install:bufferutil@1.2.1 bufferutil@1.2.1 install: `node-gyp rebuild`
npm WARN install:bufferutil@1.2.1 Exit status 1

> utf-8-validate@1.2.1 install C:\xampp\htdocs\myProject\node_modules\utf-8-validate
> node-gyp rebuild

Please run the below command first and try to install again, it should be fine afterwards:
$ git config --global url."https://".insteadOf git://

Tuesday, 24 November 2015

Building the Development Environment for Laravel

* Download and install the latest Composer
                https://getcomposer.org/Composer-Setup.exe

 Once installed, check the version by typing:
                $ composer -V

* Download and install the latest Laravel
$ composer global require "laravel/installer=~1.1"

* Install the NodeJS (globally)
                Go to the NodeJS website and download the stable version.
                https://nodejs.org/en

                OR

                Direct Link: https://nodejs.org/dist/v5.1.0/node-v5.1.0-x64.msi
                Then, Run the app with all default setting.
                And, Restart the machine.

                To check the NodeJs version:
                $ npm -v

* Download and install the latest Gulp (globally)
$ npm install -g gulp

Now, let’s install the Gulp into project dependency.

Create a package.json (if it is not already exist):
$ vi package.json 

* To install Bower
Bower is a command line utility. Install it with npm.
Bower requires node, npm and git.

$ npm install -g bower

To install Angular Material

                $ bower install  angular-material --save

(--save) will save the package on vendor directory.