Full Rest WebService(API)

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    require APPPATH.'libraries/REST_Controller.php';

    class Webservice extends REST_Controller
    {
       
        function __construct(){
           
            parent::__construct();
            //error_reporting(E_ALL & ~E_NOTICE);
            error_reporting(0);

        }   
       
        /*%%%%%%%%%%User register%%%%%%%%%%%%%%%*/
       
        //http://test.com/webservice/userRegister?name=test1&contact=7876543211&email=user5@just1call.com&password=123456&registered_by=form&referred_by=7076543211
       
        // ********facebook user login*************   
        function userFblogin_get() {
            $email = (trim($this->get('email')));
            $where_email = array(
                'email' => $email,
                'registered_by'=>'facebook'
            );
            $user_FB = ($this->user_model->get_joins('j1c_users', $where_email));
            if (!empty($user_FB)){
       
                $this->response(array(
                    'status' => 'true',
                    'message' => $user_FB
                ));   
            }
            else
            {
            $this->response(array(
                'status' => 'false',
                'message' => 'User not registered with facebook'
            ));
            }

        }
       
        function userRegister_get() {
           
            $name = ucwords(trim($this->get('name')));
            $email = (trim($this->get('email')));
            $password = ((md5($this->get('password'))));
            $contact_no = (trim($this->get('contact')));
            $registered_by = (trim($this->get('registered_by')));
            $referred_by = (trim($this->get('referred_by')));

            $this->form_validation->set_data(array(
                'email' => $email,
                'password' => $password
            ));
            if ($this->form_validation->run('userRegister_get') !== false){
               
                $where_email = array(
                    'email' => $email
                );
                $user_email = ($this->user_model->get_joins('j1c_users', $where_email));
               
                $where_referred_by= array(
                    'contact_no' => $referred_by
                );
               
                $referred_by = ($this->user_model->get_joins('j1c_users', $where_referred_by));
                $referred_id = ($referred_by[0]['id']);
                if($referred_id==''){
                        $referred_by_userid='';
                }else{
                        $referred_by_userid=$referred_id;
                     }
                if (empty($user_email)) {
                    $where_contact = array(
                        'contact_no' => $contact_no
                    );
                    $user_contact = ($this->user_model->get_joins('j1c_users', $where_contact));
                    if (empty($user_contact))
                        {
                    $insertdata = array(
                        'email' => $email,
                        'password' => $password,
                        'name' => $name,
                        'contact_no' => $contact_no,
                        'registered_by' => $registered_by,
                        'role_id' => '2',
                        'referred_by' => $referred_by_userid,
                        'registered_date' => date('Y-m-d h:i:s')
                    );
                        $is_insert = $this->user_model->INSERTDATA('j1c_users', $insertdata);
                        if ($is_insert){
                           
                            $where_user = array(
                                        'email' => $email,
                                        'contact_no' => $contact_no
                            );   
                           
                            $fields = array(
                                        'id',
                            );   
                           
                            $data = ($this->user_model->get_joins('j1c_users',$where_user,'',$fields));       
                            $this->response(array(
                                'status' => 'true',
                                'result' => $data,
                                'message'=> 'Thank you for registration'       
                            ));
                           
                            }
                          else
                            {
                            $this->response(array(
                                'status' => 'false',
                                'message' => 'Error Occured'
                            ));
                            }
                        }
                      else
                        {
                        $data['userid'] = ($user_contact[0]['id']);
                        $this->response(array(
                            'status' => 'false',
                            'result' => $data,
                            'message' => 'Mobile no. already exists'
                        ));
                        }
                    }
                  else
                    {   
                    $data['userid'] = ($user_email[0]['id']);
                    $this->response(array(
                        'status' => 'false',
                        'result' => $data,
                        'message' => 'Email already exists'
                    ));
                    }
                }
              else{
                $this->response(array(
                    'status' => 'false',
                    'message' => $this->form_validation->get_errors_as_array()
                ) , REST_Controller::HTTP_BAD_REQUEST);
                }
            }
           
            /*user wise referred count*/
            function user_referred_by_get(){
               
                $userid = $this->get('userid');
                        $where_userid = array(
                        'referred_by' => $userid
                        );
               
                $data = $this->user_model->get_joins('j1c_users',$where_userid);       
                $referredcount= count($data);
                if($referredcount){
                    $this->response(array(
                        'status' => 'true',
                        'result' => $referredcount
                    ));
                    }
                  else{
                    $this->response(array(
                        'status' => 'false',
                        'message' => 'Error Occured'
                    ));
                    }
             }
           
       
        /*%%%%%%%%%%User address update%%%%%%%%%%%%%%%*/
        //http://test.com/webservice/updateUser?id=11&address=lig%20indore&landmark=Press complex&state=M.P.&city=indore&pin=452010
        function updateUser_get() {
           
            $id = trim($this->get('id'));
            $address  = ucwords(trim($this->get('address')));
            $landmark  = ucwords(trim($this->get('landmark')));
            $state  = ucwords(trim($this->get('state')));
            $city   = ucwords(trim($this->get('city')));
            $pin    = ucwords(trim($this->get('pin')));

            $this->form_validation->set_data(array(
                'address' => $address,
                'state' => $state,
                'city' => $city,
                'pin' => $pin
            ));
           
            if ($this->form_validation->run('userupdate_get') !== false){
               
                $updatedata = array(
                    'address' => $address,
                    'landmark' => $landmark,
                    'state' => $state,
                    'city' => $city,
                    'pin' => $pin,
                    'is_address_updated ' => 'Yes',
                );
               
                $add_flag = array(
                    'id' => $id,
                    'is_address_updated' => 'Yes'
                );
               
                $address_flag = ($this->user_model->get_joins('j1c_users', $add_flag));
                if (empty($address_flag)){

                $where = array(
                    'id' => $id
                );
                $is_update = $this->user_model->UPDATEDATA('j1c_users',$where, $updatedata);
                        if ($is_update){
                            $this->response(array(
                                'status' => 'true',
                                'message' => 'Thank you. Update info successfully'
                            ));
                            }
                          else
                            {
                            $this->response(array(
                                'status' => 'false',
                                'message' => 'Error Occured'
                            ));
                            }
                }else{
                    $this->response(array(
                                'status' => 'false',
                                'message' => 'Address Can not update. Please contact to support'
                            ));
                    }   
           
          }else{
                $this->response(array(
                    'status' => 'false',
                    'message' => $this->form_validation->get_errors_as_array()
                ) , REST_Controller::HTTP_BAD_REQUEST);
                }
    }
   
   
            /*%%%%%%%%%% User profile image other info update %%%%%%%%%%%%%%%*/
            //test.com/webservice/profile
            function profile_post() {
               
            $userid = $this->post('userid');
            $image=$_POST['image'];
            $decodeImage=base64_decode("$image");
           
                $rand_no =  microtime();
                $rand_no = str_replace(' ', '', $rand_no);
                $foldername="assets/images/userprofile/";
                if (!is_dir($foldername))
                {
                    mkdir($foldername, 0777, true);
                }
           
            $isfile=file_put_contents("$foldername".$rand_no.".jpg",$decodeImage);
           
            $imagename=$rand_no.".jpg";
           
            $this->form_validation->set_data(array(
                                        'id'=>$userid,
                                        ));
            if($this->form_validation->run('profilepicupdate')!==false)
            {   
                        $updatedata = array(
                            'profile_pic' => $imagename
                        );
                       
                        $where = array(
                                'id' => $userid
                        );
                       
                    $is_update = $this->user_model->UPDATEDATA('j1c_users',$where, $updatedata);
                    $imagedata=array(
                                    'firstname'=>$userid,
                                    'image'=>$imagename,
                                    'base64image'=>$image,
                                );
                    $is_imageinsert=$this->user_model->INSERTDATA('visaform_images',$imagedata);
                    if ($is_update){
                        $condition = array(
                                'id' => $userid
                        );
                        $urlimg = base_url().'assets/images/userprofile/';
                        $fields=array("REPLACE(profile_pic, profile_pic, concat('".$urlimg."', profile_pic)) AS profileimage");
                        $data = $this->user_model->get_joins('j1c_users',$condition,'',$fields);
                        $this->response(array(
                        'status' => 'true',
                        'result' => $data,
                        'message' => 'Profile update successfully'
                    ));
                    }
                    else
                    {
                        $this->response(array(
                        'status' => 'false',
                        'message' => 'Error Occured'
                    ));
                    }       
                   
            }
            else
            {
                $this->response(array('status'=>'false','message'=>$this->form_validation->get_errors_as_array()),REST_Controller::HTTP_BAD_REQUEST);
            }
    }
       
    /*function profile_post(){
       
        $userid = $this->post('userid');
        //$userid= "1111111";
        //$userid= 2222;
        $dir_path = 'assets/images/userprofile';
       
            if (!is_dir($dir_path)) {
                mkdir($dir_path, 0777, true);
            }

        $config['upload_path']   = $dir_path;
        $config['allowed_types'] = 'gif|jpg|png|jpeg|';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
       
        if ($this->upload->do_upload('image')) {
            $uploadimg = $this->upload->data();
           
            $pic = $uploadimg['file_name'];
                    $updatedata = array(
                        'profile_pic' => $pic
                    );
                    $where = array(
                            'id' => $userid
                    );
                   
                $is_update = $this->user_model->UPDATEDATA('j1c_users',$where, $updatedata);
                if ($is_update){
                    $condition = array(
                            'id' => $userid
                    );
                    $urlimg = base_url().'assets/images/userprofile/';
                    $fields=array("REPLACE(profile_pic, profile_pic, concat('".$urlimg."', profile_pic)) AS profileimage");
                    $data = $this->user_model->get_joins('j1c_users',$condition,'',$fields);
                    $this->response(array(
                    'status' => 'true',
                    'result' => "Ok It is innnnnnn".$data,
                    'message' => 'Profile update successfully'
                ));
                }
                else
                {
                    $this->response(array(
                    'status' => 'false',
                    'message' => 'Error Occured'
                ));
                }
        }else{
            $this->response(array(
                    'status' => 'false',
                    'message' => 'IT is out now ppppp'.json_encode($_POST["image"])
                ));
           
        }
    }*/
           
        /*%%%%%%%%%%User login%%%%%%%%%%%%%%%*/

        //http://test.com/webservice/login?username=user55@just1call.com&password=123456
        //http://test.com/webservice/login?username=9876543211&password=123456
        function login_get() {
           
        $username = trim($this->get('username'));
        $password = trim($this->get('password'));
        $this->form_validation->set_data(array(
            'username' => $username,
            'password' => $password
        ));
        if ($this->form_validation->run('login_get') !== false)
            {
            if (preg_match("/^[0-9]*$/", $username))
                {
                $where = array(
                    'contact_no' => $username,
                    'password' => md5($password) ,
                    'is_active' => '1'
                );
                }
              else
                {
                $where = array(
                    'j1c_users.email' => $username,
                    'j1c_users.password' => md5($password) ,
                    'j1c_users.is_active' => '1'
                );
                }
               
            $urlimg = base_url().'assets/images/userprofile/';
           
           
            $mjoin=array(
                    array('table'=>'j1c_membership_user','condition'=>'j1c_membership_user.id=j1c_users.mpe_id','jointype'=>'left'),
                    array('table'=>'j1c_membership','condition'=>'j1c_membership.id=j1c_membership_user.membership_id','jointype'=>'left')
            );   
           
            $fields=array(
                    "j1c_membership_user.active_date",
                    "j1c_membership_user.expiry_date",
                    "j1c_membership.membership_name",
                    "j1c_users.*",
                    "REPLACE(profile_pic, profile_pic, concat('".$urlimg."', profile_pic)) AS profileimage"
            );
            $user_info = array_shift($this->user_model->get_joins('j1c_users', $where,$mjoin,$fields));
            if (!empty($user_info)){
                $this->session->set_userdata($data);
                if ($user_info['role_id'] == 2)
                    {
                    $this->response(array(
                        'status' => 'true',
                        'message' => $user_info
                    ));
                    }
                  else
                    {
                    $this->response(array(
                        'status' => 'false',
                        'message' => 'User type not found'
                    ));
                    }
                }
              else
                {
                $this->response(array(
                    'status' => 'false',
                    'message' => 'Incorrect username or password'
                ));
                }
            }
          else
            {
            $this->response(array(
                'status' => 'false',
                'message' => $this->form_validation->get_errors_as_array()
            ) , REST_Controller::HTTP_BAD_REQUEST);
            }
        }
       
        /*%%%%%%%%%Category select%%%%%%%%%*/
        //http://test.com/webservice/category
       
        function category_get(){   
                $where = array(
                            'status' => '1',
                            'parentid' => '0'
                );
                $urlimg = base_url().'assets/images/ServiceMaster/';
                $fields=array("j1c_category.*","REPLACE(cat_image, cat_image, concat('".$urlimg."', cat_image)) AS image");
                $category = $this->user_model->get_joins('j1c_category',$where,'',$fields,'','','short ASC');
                   
                    if (!empty($category)) {
                           // $category['image']=$category->cat_image;
                            $this->response(array('result'=>$category));
                    }else {
                            $this->response(array('status'=>'false','message'=>'Category not found'));
                     }
        }
        /*%%%%%%%%%Subcategory Select%%%%%%%%%*/

        //http://test.com/webservice/subcategory?userid=3&id=40
        function subcategory_get(){   
           
                $userid = trim($this->get('userid'));
                $id = trim($this->get('id'));
                $this->form_validation->set_data(array(
                    'id' => $id,
                ));
            if ($this->form_validation->run('subcategory_get') !== false){
               
               
                $where = array(
                            'status' => '1',
                            'parentid' => $id
                );
                $whereuser = array(
                            'id' => $userid
                );
                $subcategory['subcategory'] = ($this->user_model->get_joins('j1c_category',$where,''));
                $subcategory['user'] = ($this->user_model->get_joins('j1c_users',$whereuser,''));
               
                    if (!empty($subcategory)) {
                            $this->response(array('result'=>$subcategory));
                    }else {
                            $this->response(array('status'=>'false','message'=>'Subcategory not found'));
                     }
                 }else{
                $this->response(array(
                    'status' => 'false',
                    'message' => $this->form_validation->get_errors_as_array()
                ) , REST_Controller::HTTP_BAD_REQUEST);
                }
        }
       
        /*%%%%%%%%%All membership list select%%%%%%%%%*/
        //http://test.com/webservice/membership
       
        function memberShip_get(){   
                $where = array('is_active' => '1'
                );
               
                $membership['membership'] = ($this->user_model->get_joins('j1c_membership',$where));
                   
                    if (!empty($membership['membership'])) {
                            $this->response(array('result'=>$membership));
                    }else {
                            $this->response(array('status'=>'false','message'=>'Member ship not found'));
                     }
        }
       
        /*%%%%%%%%%%%%%%%%%%User membership%%%%%%%%%%%%%%%%*/
        //http://test.com/webservice/userMemberShip?userid=12&membershipid=3
        function userMemberShip_get(){   
           
            $membership_id = trim($this->get('membershipid'));
            $userid = (trim($this->get('userid')));
           
           
                $whereexp = array(
                        'id' => $membership_id,
                    );
                $is_membership = $this->user_model->get_joins('j1c_membership',$whereexp);
                 $totalday = $is_membership[0]['duration'];
                 $expirydate=date('d-m-Y', strtotime(date('Y-m-d'). ' + '.$totalday.' days'));


                $insertdata = array(
                    'membership_id' => $membership_id,
                    'userid' => $userid,
                    'expiry_date' => $expirydate,
                    'active_date' => date('Y-m-d h:i:s')
                );
               
                $updatedata = array(
                    'userid' => $userid,
                );
               
                $fieldsupdate = array(
                    'status' => 'Inactive',
                );
               
                $is_update = $this->user_model->UPDATEDATA('j1c_membership_user',$updatedata, $fieldsupdate);
                $is_insert = $this->user_model->INSERTDATA('j1c_membership_user', $insertdata);
                $where = array(
                        'status' => 'Active',
                        'userid'=> $userid
                );
                /*update j1c_users "mpe_id" active membership id*/   
                $select = ($this->user_model->get_joins('j1c_membership_user',$where));
               
                $usermpe_id = array(
                    'mpe_id' => $select[0]['id']
                );
                $whereid= array(
                    'id' => $userid
                );
                $usertable = $this->user_model->UPDATEDATA('j1c_users',$whereid, $usermpe_id);
                /*%%%%%%%%% End %%%%%%%%%%*/
                    if ($is_insert){
                        $this->response(array(
                                'status' => 'true',
                                'message' => 'Thank you. Membership subscription successfully.'
                            ));   
                    }else{
                       
                        $this->response(array('status'=>'false','message'=>'Please try again. Ticket Not apply'),REST_Controller::HTTP_BAD_REQUEST);

                    }
        }
       
        /*%%%%%%%%%%%%%%%%% User wise data get memberShip get %%%%%%%%%%%%%%%%%%*/
       
        //test.com/webservice/viewMemberShip?userid=17
        function viewMemberShip_get() {
           
                $userid = (trim($this->get('userid')));
               
                $field=array(
                'j1c_membership.membership_name',
                'j1c_membership_user.*',
                );
                $join=array(
                    array('table'=>'j1c_membership','condition'=>'j1c_membership.id=j1c_membership_user.membership_id','jointype'=>'left')
                );   
               
                $where = array(
                'j1c_membership_user.userid' => $userid,
                'j1c_membership_user.status' => 'Active'
                );
               
                $is_select = $this->query_model->get_joins('j1c_membership_user',$where,$join,$field,'','');
                if ($is_select){
                        $this->response(array(
                                'status' => 'true',
                                'result' => $is_select
                            ));   
                    }else{
                       
                        $this->response(array('status'=>'false','message'=>'Membership not found'),REST_Controller::HTTP_BAD_REQUEST);

                    }
        }
       
        /*%%%%%%%%%%%%%%%%%%% User tickets apply %%%%%%%%%%%%%%%%%%%%%%%*/

        //http://test.com/webservice/tickets?raisedby=3&servicetypeid=1&issue=all%20issue&day_available=12-07-2017&time_available=12:30PM&preferred_mobile=8765432456789
        function tickets_get() {
            $raisedby = (trim($this->get('raisedby')));
            $issue = ucwords(trim($this->get('issue')));
            $service_type = (trim($this->get('servicetypeid')));
            $day_available  = (trim($this->get('day_available')));
            $time_available   = (trim($this->get('time_available')));
            $preferred_mobile  = (trim($this->get('preferred_mobile')));
            $this->form_validation->set_data(array(
                'email' => $email,
                'password' => $password
            ));
            /*if ($this->form_validation->run('userRegister_get') !== false){*/
                $insertdata = array(
                    'raisedby' => $raisedby,
                    'service_type' => $service_type,
                    'issue' => $issue,
                    'day_available' => $day_available,
                    'time_available' => $time_available,
                    'preferred_mobile' => $preferred_mobile,
                    'apply_datetime' => date('Y-m-d h:i:s')
                );
                $is_insert = $this->user_model->INSERTDATA('j1c_tickets', $insertdata);
                    if ($is_insert){
                        $this->response(array(
                                'status' => 'true',
                                'message' => 'Thank you. Ticket apply successfully.'
                            ));   
                    }else{
                       
                        $this->response(array('status'=>'false','message'=>'Please try again. Ticket Not apply'),REST_Controller::HTTP_BAD_REQUEST);

                    }
       
            //}
        }
       
        /********User feedback on ticket**********/
        //http://test.com/webservice/feedback?ticketid=1&feedback_by=12&feedback=nice%20service&rating=3
       
        function feedback_get(){
               
                $ticketid = (trim($this->get('ticketid')));
                $feedback_by = (trim($this->get('feedback_by')));
                $feedback = ucwords(trim($this->get('feedback')));
                $rating = (trim($this->get('rating')));
               
                $insertdata = array(
                    'ticketid' => $ticketid,
                    'feedback_by' => $feedback_by,
                    'feedback' => $feedback,
                    'rating' => $rating,
                    'date' => date('Y-m-d h:i:s')
                );
               
                $whereupdate = array(
                    'id' => $ticketid
                );
               
                $fields = array(
                    'status' => 'Close',
                );
               
                $is_update = $this->user_model->UPDATEDATA('j1c_tickets',$whereupdate, $fields);
                $is_insert = $this->user_model->INSERTDATA('j1c_feedback', $insertdata);
                    if ($is_insert){
                        $this->response(array(
                                'status' => 'true',
                                'message' => 'Thank you. For feedback'
                            ));   
                    }else{
                       
                        $this->response(array('status'=>'false','message'=>'Please try again.'),REST_Controller::HTTP_BAD_REQUEST);

                    }
           
        }
       
        /********User wise tickets**********/
        //http://test.com/webservice/userticket?userid=3&status=open
        //http://test.com/webservice/userticket?userid=2&status=close       
        function userticket_get() {
               
                $userid = (trim($this->get('userid')));
                $status = ucwords(trim($this->get('status')));
               
                                   
                $field=array(
                                'j1c_services_provider.name as serviceprovidername',
                                'j1c_services_provider.contact_no as mobile',
                                'category.category',
                                'j1c_category.category as servicetypename',
                                'j1c_tickets.*',
                                'j1c_feedback.feedback',
                                'j1c_feedback.rating'
                );
                $join=array(
                            array('table'=>'j1c_tickets','condition'=>'j1c_tickets.raisedby=j1c_users.id','jointype'=>'inner'),
                            array('table'=>'j1c_category','condition'=>'j1c_category.id=j1c_tickets.service_type','jointype'=>'inner'),
                            array('table'=>'`j1c_services_provider','condition'=>'`j1c_services_provider.id=j1c_tickets.assign_to_service_pro_id','jointype'=>'left'),
                            array('table'=>'j1c_category as category','condition'=>'category.id=j1c_category.parentid','jointype'=>'inner'),
                            array('table' => 'j1c_feedback','condition' => 'j1c_tickets.id=j1c_feedback.ticketid','jointype' => 'left')

                );   
                $condition = array(
                                'j1c_users.id' => $userid,
                                'j1c_tickets.status' => $status,
                        );           
                $userticket['userticket'] = ($this->user_model->get_joins('j1c_users',$condition,$join,$field,'','','j1c_tickets.id DESC'));
                if (!empty($userticket['userticket'])) {
                            $this->response(array('result'=>$userticket));
                    }else {
                            $this->response(array('status'=>'false','message'=>'Ticket not found'));
                     }   
        }
       
        /*%%%%%%%%%%%%%%%Forget password %%%%%%%%%%%%%%%%%*/
        function forgetpassword_get() {
                    $email = (trim($this->get('email')));   
                    $this->form_validation->set_data(array(
                    'email' => $email
                    ));
                    if ($this->form_validation->run('forgetpass') !== false)
                    {
                        $condition = array(
                                'email' => $email
                            );
                        $user_email = array_shift($this->user_model->get_joins('j1c_users', $condition));
                        if (!empty($user_email))
                        {
                            $baseemail=base64_encode($email);           
                            $id=base64_encode($user_email['id']);           
                            $name=base64_encode($user_email['name']);           
           
            $link='http://test.com/login/forgetpassword?id='.$baseemail.'&uid='.$id;
           
            $html='<table>
                        <tr>
                        <td><b>Hello '.$name.',</b></td>
                        </tr>
                        <tr>
                        <td>We received a request to reset your password. Click the link below to choose a new one:</td>
                        </tr>
                        <tr>
                            <td><a href="'.$link.'">Reset your password</a></td>
                        </tr>                   
                    </table>';
                                       
                    // Always set content-type when sending HTML email
                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                    // More headers
                    $emails='just1call@just1call.com';
                    $headers .= 'From: <'.$emails.'>' . "\r\n";
                    //$headers .= 'Cc: myboss@example.com' . "\r\n";

                    mail($email,'Reset password',$html,$headers);
                    $this->response(array('status'=>'true','message'=>'Please check your email for reset password'));   
                           
                        }else {
                                $this->response(array('status'=>'false','message'=>'Please enter valid email address'));
                         }   
                    } else{
                            $this->response(array(
                                'status' => 'false',
                                'message' => $this->form_validation->get_errors_as_array()
                            ) , REST_Controller::HTTP_BAD_REQUEST);
                        }
           
        }
   
}
?>
Latest
Previous
Next Post »