How to get the users detail info in api call in cs-cart? - cs-cart

I am trying to retrieve user information through api in CS-cart. But it returns only limited information. How can we modify the code to get all the user info for ex- user profiles, address, gst and all.

you can create your own API.
/var/www/html/app/addons/addon_name/Tygh/Api/Entities/Egg.php
<?php
namespace Tygh\Api\Entities;
use Tygh\Api\AEntity;
use Tygh\Api\Response;
class Egg extends AEntity
{
public function index($id = '', $params = array())
{
if(empty($id))
{
$dd=db_get_array("SELECT * FROM ?:table_name");
//result all rows
}
else
{
// for filtering purpose
$where=array("id"=>$id);
$dd=db_get_array("SELECT * FROM ?:table_name where ?w",$where);
//result-> specific one row
}
return array(
'status' => Response::STATUS_OK,
'data' => $dd
);
}
public function create($params)
{
return array(
'status' => Response::STATUS_OK,
'data' => array()
);
}
public function update($id, $params)
{
return array(
'status' => Response::STATUS_OK,
'data' => array()
);
}
public function delete($id)
{
return array(
'status' => Response::STATUS_NO_CONTENT,
);
}
public function privileges()
{
return array(
'index' => true,
'create' => 'create_things',
'update' => 'edit_things',
'delete' => 'delete_things',
'index' => 'view_things'
);
}
public function privilegesCustomer()
{
return array(
'index' => true
);
}
}
?>
Remarks:
file name,
class name,
file path

Or you can edit the user API entity from this location.
app/Tygh/Api/Entities/Users.php
Any doubts , then kick me in...

Related

I can't call a static function from another controller in codeigniter. Why?

I have started to create a controller called User in codeigniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array( 'encrypt', 'form_validation' ));
}
public function index()
{
}
/*
* Create loginform
* Parameters: void
* Return: html of loginform;
*/
public static function loginform() {
// Login form setup
$loginform_data = array();
$loginform_data['attributes'] = array('id' => 'loginform');
$loginform_data['username'] = array(
'name' => 'username',
'id' => 'username',
'value' => '',
'maxlength' => '100'
);
$loginform_data['pass'] = array(
'name' => 'pass',
'id' => 'pass',
'value' => '',
'maxlength' => '100'
);
$contentdata = array();
$contentdata['loginform'] = $this->load->view('partials/forms/login', $loginform_data, true);
return $contentdata;
}
/*
* Check login username, password from form
* Parameters: void
* Return: void;
*/
public function login() {
$name = $this->input->post('username');
$pass = $this->input->post('pass');
$this->form_validation->set_rules('username', 'Användarnamn', 'required');
$this->form_validation->set_rules('pass', 'Lösenord', 'required');
if ($this->form_validation->run() == false)
{
$this->load->view('home');
}
else
{
$this->load->view('formsuccess');
}
}
}
I can call the user/login - function through the url. But I can't call User::loginform() from another controller. Shouldn't I be able to do that?
Here's what I'm trying: (from my Home-class)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//Create login and register-forms
$this->load->helper('form');
//Registration form setup
$registerform_data = array();
$registerform_data['attributes'] = array('id' => 'registerform');
$registerform_data['company'] = array(
'name' => 'name-company',
'id' => 'name-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['orgnr'] = array(
'name' => 'orgnr-company',
'id' => 'orgnr-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['contact'] = array(
'name' => 'contact-company',
'id' => 'contact-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['phonecompany'] = array(
'name' => 'phone-company',
'id' => 'phone-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['emailcompany'] = array(
'name' => 'email-company',
'id' => 'email-company',
'value' => '',
'maxlength' => '100'
);
//What content to pass to view
$contentdata = array();
$contentdata['loginform'] = User::loginform();
$contentdata['registerform'] = $this->load->view('partials/forms/registration', $registerform_data, true);
$this->load->view('home', $contentdata);
}
}
$contentdata['loginform'] = User::loginform(); gives me error:Fatal error: Class 'User' not found in C:\Program...
What am I missing?
While you are extending Home class ,extend to User controller like
require_once(APPPATH.'controllers/user.php');
class Home extends User {
Because you need to extract the function from User class and then it will Inherit the parent class User functions And since login_form is your public function in User controller,you can call this in Home controller now.And no need to use Static here I think so.
There is also an way to do this.Just write the login_form function in an helper and call it on both the controllers then your problem may solve.
Edit: As #IJas said,we need to include the controller file that you are extending.

How to disable direct access to callback functions?

<? if ( ! defined('BASEPATH')) exit();
class Registration extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('registration_model');
}
public function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email|callback_email_available');
if($this->form_validation->run() == FALSE) {
$this->load->view('registration');
} else {
$this->registration_model->add_user();
}
}
# Check E-mail
public function email_available($email) {
$this->db->select('email');
$this->db->where('email', $email);
$query = $this->db->get('users');
$result = $query->row();
if(!empty($result)) {
$this->form_validation->set_message('email_available', 'This e-mail belongs to another user.');
return FALSE;
} else {
return TRUE;
}
}
}
?>
I have a registration form with Form Validation.
And I have a callback function to validate email uniqueness.
All code works fine, but I can directly access to callback function with errors
examle.com/registration/email_available
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Registration::email_available()
Filename: controllers/registration.php
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: email
Filename: controllers/registration.php
How can I deny direct access to callback function?
You can prefix the method name with an _ to deny access through HTTP request.
My suggestion is to tuck your validation rules into a separate file. CodeIgniter supports this by allowing you to save validation configurations in config/form_validation.php. Take a look at the Form Validation Documentation, specifically the section labelled Saving Sets of Validation Rules to a Config File.
Your controller's index:
public function index() {
$this->load->library('form_validation');
if($this->form_validation->run('submit_registration') == FALSE) {
$this->load->view('registration');
}
else{
$this->registration_model->add_user();
}
}
config/form_validation.php
$config = array
(
'submit_registration' => array
(
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|email_available'
),
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required|alpha_numeric|etc'
)
),
'some_other_submission' => array(
array(
'field' => 'somefield',
'label' => 'SomeField',
'rules' => 'some|rules'
),
array(
'field' => 'getit',
'label' => 'Get The Point?',
'rules' => 'trim'
)
)
);
libraries/MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array()){
parent::__construct($config);
}
function email_available($email){
$CI =& get_instance();
//check your database with the $CI variable...
if(email_exists) return TRUE;
else return FALSE;
}
}

ORM/Extra Validate iby two field

Kohana 3.2
I want to check my parent_id but i need the second value of type_id.
Code:
public function rules()
{
return array(
'type_id' => array(
array('not_empty'),
array('digit'),
),
'parent_id' => array(
array('digit'),
array(array($this,'check_category'),array(':value', ':field','type_id'))
),
);
}
public function check_category($parent_id,$field,$type_id)
{
die($type_id);
}
How to sent two values of my field to my function ??
After i make that in my controller :
if(isset($_POST['submit']))
{
$data = Arr::extract($_POST, array('type_id', 'parent_id', 'name', 'comment'));
$category = ORM::factory('kindle_category');
$category->values($data);
try {
$extra_rules = Validation::factory($_POST)
->rule('parent_id','Kindle::check_category',array($data['type_id'],$data['parent_id'],'parent_id',':validation'));
$category->save($extra_rules);
$this->request->redirect('kindle/category');
}
catch (ORM_Validation_Exception $e) {
$errors = $e->errors('validation');
}
}
if($parent->type_id!=$type_id)
{
$validation->error($field, 'Dog name, not cat!');
return false;
}
How to see my error "Dog name,not cat!' in my View ?
Array errors doesnot have this value.
public function rules()
{
return array(
'type_id' => array(
array('not_empty'),
array('digit'),
),
'parent_id' => array(
array('digit'),
array(array($this,'check_category'),array(':validation'))
),
);
}
public function check_category($validation)
{
$type_id = $validation['type_id'];
...
}
http://kohanaframework.org/3.2/guide/orm/examples/validation

codeigniter page redirection issue

I am new to codeigniter frame work and i am trying to design a member registration form using that framework. I have used the following code to insert the member.
<?php
Class data extends CI_Model
{
function add_form()
{
$this->load->database();
$val = array(
'name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address'),
'phoneno' => $this->input->post('phone'),
);
$insert=$this->db->insert('tbl_member',$val);
}
}
?>
If the member details inserted into the database successfully i have to redirect it to the view member details page with success message. In that page i have to display the member details fetched from the database. I don't know how to redirect and check whether the insert is true. How can i do that?
You can check if the insert was successful with:
$this->db->affected_rows()
to return the number of records inserted. You can then redirect the user to the appropriate page using the URL helper's redirect function:
redirect('path/to/redirect/to');
I hope it will work for you
<?php
Class data extends CI_Model
{
function add_form()
{
$this->load->database();
$val = array(
'name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address'),
'phoneno' => $this->input->post('phone'),
);
if( $this->db->insert('tbl_member',$val) ) {
$insert= $this->db->insert_id();
return $this->insert_success($insert);
} else {
return false;
}
}
}
?>
And your insert_success method would be:
function insert_success($userId)
{
$data = $this->db->query('query-to-fetch-details-with-user-id');
return $data->result_array();
}

Understanding when Model's $validate rules work

I'm trying to activate a user with CakePHP 2 by activation url, but I get a validation error used on user registration page.
I've created a registration action and view, where I've defined this UserModel validation rules:
<?php
class User extends AppModel {
public $name = 'User';
public $validate = array (
'username' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Username cannot be empty'
)
),
'password' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Password cannot be empty'
),
'between_chars' => array (
'rule' => array ('between', 5, 40),
'message' => 'Password must be between 5 and 40 chars'
),
'match_password' => array (
'rule' => 'matchPasswords',
'message' => 'Password is different from Password confirm'
)
),
'password_confirm' => array (
'not_empty' => array (
'rule' => 'notEmpty',
'message' => 'Password confirm cannot be empty'
),
'between_chars' => array (
'rule' => array ('between', 5, 40),
'message' => 'Password confirm must be between 5 and 40 chars'
)
),
'email' => array (
'invalid_email' => array (
'rule' => 'email',
'message' => 'Invalid email, try again'
),
'existing_email' => array (
'rule' => 'isUnique',
'message' => 'This email is already registered'
)
),
'activation_key' => array (
'alphanumeric_key' => array(
'allowEmpty' => true,
'rule' => 'alphaNumeric',
'message' => 'Invalid activation key',
'last' => true
)
)
);
public function matchPasswords ($data) {
debug($this->data); // Undefined index: password_confirm [APP/Model/User.php, line 59] points here
if ($data['password'] == $this->data['User']['password_confirm']) {
return true;
}
$this->invalidate('password_confirm', 'Password confirm must be equal to Password field');
return false;
}
public function beforeSave () {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
Everything works perfect until I reach the activate action of the UsersController with the activation code, here I get an Undefined index: password_confirm [APP/Model/User.php, line 59] which points to matchPasswords of the User Model validation rule.
<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController {
public $name = 'Users';
public function index () {
$this->User->recursive = 0;
$this->set('users', $this->User->find('all'));
}
public function view ($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid user');
$this->redirect(array('action'=>'index'));
}
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException('Invalid user');
}
$this->set('user', $this->User->read());
}
public function edit () {
}
public function delete () {
}
public function register () {
// code for user registration
}
public function registration ($sub_action = null) {
}
public function password ($sub_action = null, $code = null) {
if ($sub_action == 'reset' && !empty ($code)) {
$this->render('password_reset_code');
} else if ($sub_action == 'reset' && empty ($code)) {
$this->render('password_reset_mail');
}
}
public function login () {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect ($this->Auth->redirect());
} else {
$this->Session->setFlash('Errore di accesso, email/password sbagliati, ricontrolla i dati inseriti');
}
}
}
public function logout () {
$this->redirect($this->Auth->logout());
}
public function activate ($code = null) {
if (!empty ($code)) {
$user = $this->User->find('first', array('conditions' => array('User.activation_key' => $code)));
if (!empty($user)) {
$user['User']['activation_key'] = null;
$user['User']['active'] = 1;
if ($this->User->save($user)) { // here is where Invalid index error starts
$this->render('activation_successful');
} else {
$this->set('status', 'Salvataggio fallito');
$this->render('activation_fail');
}
// debug($this->User->invalidFields());
// debug($this->User->data);
} else {
$this->set('status', 'Nessun account collegato alla chiave');
$this->render('activation_fail');
}
} else {
$this->set('status', 'Nessuna chiave');
$this->render('activation_fail');
}
}
private function registrationEmail ($account_email, $username, $code) {
// code for email registration
}
}
?>
Why I get the error?
Why matchPasswords is executed in the activate action?
Are these validation rules executed in every view of the controller?
Try setting the User model's id before the save in the activate() function:
$this->User->id = $user['User']['id'];
or something similar.
When you don't set the id prior to the save() function, it tries to make a new table row. That's why it's validating everything.
Try changing
if ($data['password'] == $this->data['User']['password_confirm']) {
to
if (isset($this->data['User']['password_confirm']) && $data['password'] == $this->data['User']['password_confirm']) {
in your matchPasswords function in the User.php
In the activate action you are getting all of the fields for the user and then saving them so within the data you are saving will be the password field. As the password field is there CakePHP is trying to validate it against the password_confirm field which does not exist.
You should only validate that the password and password confirm fields match when they are both present, i.e when you have the password_confirm in your form.

Resources