Working with the my_model & my_controller for the first time this month within CodeIgniter, I think I almost have this working.
I got an insert function working properly, now I'm trying to add an update to it if there is an ID.
Here's my code:
function upsert_client($client_id = 0)
{
load_model('client_model');
$this->insertMethodJS();
$this->fields['client'] = $this->_prototype_client();
$user_id = get_user_id();
$company_id = get_company_id();
if ($client_id)
{
$this->data['client'] = $this->client_model->get_record($client_id);
}
if (!$this->ion_auth->in_group(GROUP_NAME_MANAGER, $user_id))
{
redirect('members/dashboard');
}
if ($_POST)
{
$this->load->helper('string');
if ($this->_validate_client())
{
$fields = $this->input->post(null , TRUE);
$fields['user_id'] = $user_id;
$fields['company_id'] = $company_id;
$fields['active'] = 1;
if ($client_id)
{
$fields['id'] = $this->client_model->get_record($client_id);
unset($fields['billing']);
$this->client_model->update($client_id, $fields);
}
else
{
unset($fields['billing']);
$this->client_model->insert($fields);
redirect('members/clients/manage_clients');
}
}
}
$this->template->write_view('content',$this->base_path.'/'.build_view_path(__METHOD__), $this->data);
$this->template->render();
}
function _prototype_client()
{
$fields = array();
$fields['id'] = 0;
$fields['name'] = '';
return $fields;
}
And from my client_model:
class Client_model extends MY_Model {
function get_record($client_id)
{
$query = $this->db->select('id')
->where(array('id'=>$client_id))
->get('clients');
return $query->row_array();
}
}
Everytime I try to edit a "client", it just inserts a new one... All I'm currently trying to edit is the "name" field.
My edit button:
<td><button class="btn btn-inverse" style="float: right;" type="button">Edit</button></td>
Any help is appreciated, thanks! And let me know if you need any additional details...
I never worked with ion auth in particular but from what I see you have a couple of functions that are not referenced correctly.
load_model('client_model');
//Should be
$this->load->model('client_model');
also a few other functions should be referenced as
$this->function_name();
//Instead of just
function_name();
//Unless they are in another library
$this->lib_name->function_name();
I'm not sure if this well solve your problems but just a few things I noticed.
Your hyperlink points to 'add_client' whereas the function you are showing is called 'upsert' Are you calling the correct URL?
Related
Seems get_instance() is no longer working in Codeigniter 4. But I need to get some value from BaseController to my custom_helper. Here is my code snippet:
BaseController.php
<?php namespace App\Controllers;
class BaseController extends Controller
{
protected $helpers = ['custom'];
public function initController(...) {
parent::initController(...);
$myconfig = config('MyConfig');
$this->languages = $myconfig->languages;
$this->selected_lang = $myconfig->site_lang;
$lang_segment = $this->request->uri->getSegment(1);
foreach ($this->languages as $lang) {
if ($lang_segment == $lang->short_form) {
$this->selected_lang = $lang;
$this->lang_base_url = base_url() . $lang->short_form . "/";
}
}
}
}
// Here I need to pass ($this->selected_lang) value to my custom_helper.
custom_helper.php
<?php
if (!function_exists('trans')) {
function trans($string)
{
$language_translations = get_translation_array($this->selected_lang->id);
// --> Here I want to get ($this->selected_lang->id) value from BaseController.
if (!empty($language_translations[$string])) {
return $language_translations[$string];
}
return "";
}
}
function get_translation_array($land_id)
{
.......
}
I'm not sure is it possible or not! I'm newbie in CI4. Please suggest if is there any solutions. Thanks in advance.
Instead of calling the BaseController you should do everything within the helper that is
if (!function_exists('trans')) {
function trans($string)
{
$myconfig = config('MyConfig');
$languages = $myconfig->languages;
$selected_lang = $myconfig->site_lang;
$language_translations = get_translation_array($selected_lang->id);
if (!empty($language_translations[$string])) {
return $language_translations[$string];
}
return "";
}
}
Although I don't know what you are trying to do but I hope this will help you if not call my attentions again
I am working in Codeigniter and I want to hide ID from URL.
my current URL is:
www.localhost/CI/services/1/ac_repair
but need this type of URL in codeigniter:
www.localhost/CI/services/ac_repair
View Page Code:
<?=anchor('services/' . $ser->s_id . '/' . url_title($ser->s_title,'_') ,'View Service');?>
Controller Code:
public function services()
{
$this->load->model('ServicesModel', 'ser_model');
$s_id = $this->uri->segment(2, 0);
if($s_id){
$get_service = $this->ser_model->get_ser($s_id);
return $this->load->view('public/detail', compact('get_service') );
}
else
{
// $services = $this->articles->articles_list( $config['per_page'], $this->uri->segment(3) );
$get_services['result'] = $this->ser_model->all_services_list();
// $this->load->view('public/services', ['services'=>$services]);
$this->load->view('public/services', $get_services);
}
}
Model Code here:
public function get_ser($id)
{
// $q = $this->db
$q = $this->db->select('*')
->from('services')
->where( ['s_id' => $id] )
->get();
if ( $q->num_rows() )
return $q->row();
return false;
}
but need this type of URL in codeigniter:
www.localhost/CI/services/ac_repair
If you want this functionality you have to be able to use your title ac_repair in place of the id. This means the title needs to be marked as unique and therefore not contain any duplicates.
The following pseudo-code should give you an idea:
function services($url_title = null) {
if (!is_null($url_title)) {
// get ser would use title instead of $id
$this->db->where('title', $url_title);
} else {
// all rows
}
}
Other methods would be "hacky" and I cannot think of any off the top of my head that I would consider usable.
Side note: you should never be returning in a view
I have 2 functions in my controller that I'm sending a session using the first one to the second function. What I am talking about is:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result/');
}
and the second function:
function search_result()
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
Everything is fine but, the problem is that, I want to prevent direct access to search_result() function. I mean, I want to unset search_term session when the user calls search_result() directly. What should I do?!
You can use flashdata: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages
And this is the code that do what are you looking for:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_flashdata('search_term', $search_term);
redirect('search_result/');
}
function search_result()
{
$search_term = $this->session->flashdata('search_term') ? $this->session->flashdata('search_term') : '';
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
There are many ways you can do it. Here are some
append a get parameter if it exists then search else redirect back
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result?status=1');
}
public function search_result()
{
$status = $this->input->get('status');
if(status == 1){
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}else{
$this->session->unset_userdata('search_term');
redirect('search');
}
}
Protect you seach_result function and dont let user direct call it.
Using _ will do it for you.
Here is the link you can read.
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
$this->_search_result($search_term);
}
function _search_result($keyword)
{
if(strlen($keyword)>0){
$data['search_term'] = $keyword;
$this->load->view('includes/template', $data);
}else{
redirect('search');
}
}
in the search function
redirect('search_result/'.$this->session->userdata('search_term'));
in the search_result function
function search_result()
{
if($this->uri->segment(3))
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
else
{
redirect('search_result/','refresh');
}
}
hope it will help you.please let me know if you face any problem.
Here is a scenario with Magento CE 1.7.0.2 If you are on catalog search page and list mode is on.
Url: http://127.0.0.1/magento/catalogsearch/result/index/?mode=list&q=the
And redirect to current page after adding product to cart is Active in admin panel.
If you try to add simple product to cart, product added to cart successful but redirection URL is not decoded properly
All ‘&’ replaced by ‘&’ and result in breaking search result...
Result URL: http://127.0.0.1/magento/catalogsearch/result/index/?mode=list&q=the
I think this bug may be already attended but I don’t find any topic on it....
Kindly help in this
Thanks in advance
Thanks Mufaddal for giving reference.
I got two easy ways to fix this:
Way 1:-
You can just override/rewrite this helper app\code\core\Mage\Core\Helper\Abstract.php
With modification of below function :
public function escapeUrl($data)
{
return htmlspecialchars($data);
}
to
public function escapeUrl($data)
{
return $data;
}
Way 2 (the best way) :
rewrite/override controller /app/code/core/Mage/Core/Controller/Varien/Action.php
with below modified method :
protected function _getRefererUrl()
{
$refererUrl = $this->getRequest()->getServer(’HTTP_REFERER’);
if ($url = $this->getRequest()->getParam(self::PARAM_NAME_REFERER_URL)) {
$refererUrl = $url;
}
if ($url = $this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
$refererUrl = Mage::helper(’core’)->urlDecode($url);
}
if ($url = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
$refererUrl = Mage::helper(’core’)->urlDecode($url);
}
$refererUrl = Mage::helper(’core’)->escapeUrl($refererUrl);
if (!$this->_isUrlInternal($refererUrl)) {
$refererUrl = Mage::app()->getStore()->getBaseUrl();
}
return $refererUrl;
}
To
protected function _getRefererUrl()
{
$refererUrl = $this->getRequest()->getServer(’HTTP_REFERER’);
if ($url = $this->getRequest()->getParam(self::PARAM_NAME_REFERER_URL)) {
$refererUrl = $url;
}
if ($url = $this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
$refererUrl = Mage::helper(’core’)->urlDecode($url);
}
if ($url = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
$refererUrl = Mage::helper(’core’)->urlDecode($url);
}
//$refererUrl = Mage::helper(’core’)->escapeUrl($refererUrl);
if (!$this->_isUrlInternal($refererUrl)) {
$refererUrl = Mage::app()->getStore()->getBaseUrl();
}
return $refererUrl;
}
In this fle app\code\core\Mage\Core\Helper\Abstract.php public function escapeUrl($data)
this function is responsible for weird url.
It use htmlspecialchars function of php whic replace & with '&' see here http://php.net/manual/en/function.htmlspecialchars.php
So what I do is comment this line $this->getResponse()->setRedirect($returnUrl); in my CartController _goBack method insrtead of I used my custom redirect method.
I created a "callback" function to check if the username exists in the DB.
I have multiple rules for the "username" field, but the only thing that work is my callback function. It refuses to check against the other rules. I tried leaving the field empty, and the "required" rule never kicked in.
Controller:
account.php
function register() {
$this->load->library('validation');
$fields['username'] = "trim|required|callback_username_check";
etc ...
etc ...
$this->validation->set_rules($fields);
if ($this->validation->run()) {
$records = array();
$records['username'] = $this->validation->username;
etc ...
etc ...
$data = $this->account_model->registerNewAccount($records);
}
$this->load->view('register_view');
}
function username_check($username) {
$m = new Mongo();
$collection = $m->selectDB( DBNAME )->selectCollection( TABLE );
$data = $collection->count(array("username" => $username) );
if($data == 1) {
$this->validation->set_message('username_check', '%s is already taken!');
return false;
} else {
return true;
}
}
Try using the new form_validation class here:
http://ellislab.com/codeigniter/user_guide/libraries/form_validation.html
I believe there was a bug about it.