Wednesday, 10 August 2016

Convert multidimensional array to single dimensional using php


Below is the function to convert multidimensional array into single dimensional array.



function array_flatten($array, $x = NULL) {

        if (!is_array($array)) {
            return false;
        }
        $result = array();

        $x = ($x == '') ? 0 : $x;

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $result = array_merge($result, self::array_flatten($value, $x++));
            } else {
                $result[$x][$key] = $value;
            }
        }
        return $result;

    }



Yii2 href post – Post data using link tag



In Yii2, you can post data using link tag in a form with  data-method post, you can also pass data as params.


Example usage:
Html::a('<i class="fa fa fa-arrow-right"></i> Confirm Details', ['controller/action'], [
                        'class' => 'btn bg-blue btn-flat',
                        'data' => [
                            'method' => 'post',
                            'params' => [
                                'confirm-user' => '1',
                                'user_id' => '10',

                            ],
                        ]
                    ]);

Yii2 from events


In Yii2, below are the form events which we can make use it on front end / client side .

  • beforeValidate,
  • afterValidate,
  •  beforeValidateAttribute,
  • afterValidateAttribute,
  • beforeSubmit,
  •  ajaxBeforeSend,
  • ajaxComplete

Example usage:

$("#FORM-ID").on("afterValidate", function (event, messages) {

    // Now you can work with messages by accessing messages variable
 var attributes = $(this).data().attributes; // to get the list of attributes
    that has been passed in attributes property

  var settings = $(this).data().settings; // to get the settings

});

Yii2 model relation with multiple conditions

In AR Model Relations you can add multiple ON condition using andOnCondition method.
public function getInvoiceItem(){

return $this->hasOne(Invoice::className(), ['order_no' => 'order_no']) 

            ->andOnCondition(['item' => $this->order_item])           

            ->andOnCondition(['user_id' => $this->user_id]);
}


Friday, 25 September 2015

Prevent Form Submission When User Presses the Enter key / Move to next textbox when user presses enter key


 When we filling out the web forms, if we hit the Enter key accidentally, then the form will submit, it makes us frustrating every time.

To prevent this you can use the code below. Instead of submitting, it actually focus / moves to next input boxes when we press Enter key.

$('body').on('keydown', 'input, select, textarea', function(e) {
    var self = $(this)
            , form = self.parents('form:eq(0)')
            , focusable
            , next
            ;
    if (e.keyCode == 13) {
        focusable = form.find('input,a,select,textarea').filter(':visible');
        next = focusable.eq(focusable.index(this) + 1);
        //  console.log(next.attr('type'));
        if (next.length && (next.attr('type') != 'button')) {
            next.focus();
        } else {
            form.submit();
        }
        return false;
    }
});


Wednesday, 22 April 2015

Wordpress admin panel custom landing page after login

Redirect to different landing page after successful login on wordpress admin panel,

/*To make non-admin users redirect to specified landing page*/

function your_login_redirect()
{

if(!is_super_admin()){

            // pick where to redirect to, in the example: Posts page
            return admin_url( 'edit.php' );
        } else {
            return admin_url();
        }
   
}
add_filter( 'login_redirect', 'your_login_redirect');



Wordpress Disable Admin Panel Menus Based on Role

In wordpress admin panel, if you want to hide certain menu's for non-admins , then add the below code in your current theme's functions.php file.


function remove_menus () {
global $menu;

        $restricted = array(__('Dashboard'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'),  __('Plugins'));
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }
}
      /* To hide menus for non -admin users*/
if(!is_super_admin()){
add_action('admin_menu', 'remove_menus');   
}