Ignited Datatables Where Not In - codeigniter

I using Ignited Datatables library but it dose not support WhereNotIn functionality, how can I add this feature to this library? Thanks in advance.

Patch the Datatables class like this:
class Datatables
{
//...
private $where_not_in = array();
//...
public function where_not_in($key_condition, $val = NULL, $backtick_protect = TRUE)
{
$this->where_not_in[] = array($key_condition, $val, $backtick_protect);
$this->ci->db->where_not_in($key_condition, $val, $backtick_protect);
return $this;
}
private function get_total_results($filtering = FALSE)
{
//...
foreach($this->where_not_in as $val)
$this->ci->db->where_not_in($val[0], $val[1], $val[2]);
//...
}
}

Related

Codeigniter 4: Get value from BaseController to custom helper

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

Post with xml file in laravel

I am using method post to create new data in xml file but The function c_element cannot be used in the function store
$DeTai = c_element('DeTai', $root);
This is my current code:
public function c_element($e_name, $parent)
{
global $xml;
$node = $xml->createElement($e_name);
$parent->appendChild($node);
return $node;
}
public function c_value($value, $parent)
{
global $xml;
$value = $xml->createTextNode($value);
$parent->appendChild($value);
return $value;
}
public function store(Request $request)
{
$xml = new DOMDocument("1.0","UTF-8");
$xml->load('../xml/QuanLyDoAnTotNghiep.xml');
if ($request->isMethod('post'))
{
$madt= $request->madt;
$noidungdetai = $request->noidungdetai;
$root=$xml->getElementsByTagName("QuanLyDoAnTotNghiep")->item(0);
$DeTai = c_element("DeTai", $root); //error in here
$s_madt = c_element('MaDT', $DeTai);
c_value("$madt", $s_madt);
$s_noidungdetai = c_element('NoiDungDeTai', $DeTai);
c_value("$noidungdetai", $s_noidungdetai);
$xml->formatOutput=true;
$xml->save('../xml/QuanLyDoAnTotNghiep.xml');
echo "Thêm mới thành công!!!";
}
}
use this keyword to call one method in different method of same class
$DeTai = $this->c_element('DeTai', $root);
to know more about it please visit this
Thanks..

Override CustomerFormatter.php for birthday form

I would like to put the birthday field mandatory, so I override the CustomerFormatter but it does not work at all. That's how I did:
<?php
class CustomerFormatter extends CustomerFormatterCore
{
public function getFormat()
{
$customerForm = parent::getFormat();
if ($customerForm->ask_for_birthdate) {
$format['birthday'] = (new FormField)
->setName('birthday')
->setType('text')
->setLabel(
$customerForm->translator->trans(
'Birthdate', [], 'Shop.Forms.Labels'
)
)
->addAvailableValue('placeholder', Tools::getDateFormat())
->addAvailableValue(
'comment',
$customerForm->translator->trans('(E.g.: %date_format%)', array('%date_format%' => Tools::formatDateStr('31 May 1970')), 'Shop.Forms.Help')
)
->setRequired(true)
;
}
}
}
I am under Prestashop 1.7.3.3
Do you have an idea of ​​the problem ? Thank you for your help
Probably to late for you but it might help others, this is unfortunately impossible with 1.7.
"Yes, overrides work as usual on all classes that have no namespace (so you can still override Product, Address, etc.)."
http://build.prestashop.com/news/prestashop-1-7-faq/#can-developers-still-use-overrides-in-17
You need to go to public_html/override/classes/form/ and create the file CustomerFormatter.php then type this code:
<?php
/**
* #Override CustomerFormatter
*/
use Symfony\Component\Translation\TranslatorInterface;
class CustomerFormatter extends CustomerFormatterCore
{
private $translator;
private $language;
private $ask_for_birthdate = true;
private $ask_for_password = true;
private $password_is_required = true;
private $ask_for_new_password = false;
public function __construct(
TranslatorInterface $translator,
Language $language
) {
parent::__construct($translator, $language);
$this->translator = $translator;
$this->language = $language;
}
public function getFormat()
{
$format = parent::getFormat();
$format = [];
if ($this->ask_for_birthdate) {
$format['birthday'] = (new FormField())
->setName('birthday')
->setType('birthday')
->setLabel(
$this->translator->trans(
'Birthdate',
[],
'Shop.Forms.Labels'
)
)
->setRequired($this->password_is_required);
}
}
You need to use:
->setRequired($this->password_is_required)
Because there's an error on checkout for guests purchases. So like this, it would be an optional field.

How to cache model attributes in Laravel

In my current configuration, a user's email is stored on a remote server that I need to hit with a curl quest.
Luckily, I only need the email once a day when a certain process runs. However, when that process does run it will need to reference the email multiple times.
This is the current accessor I have set up for email. The problem is the curl request is being called every time I use $user->email. What's the best way to avoid this?
in UserModel:
public function getEmailAttribute(){
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
return $response->email;
}
private $cached_email = false;
public function getEmailAttribute(){
if ($this->cached_email){
// if set return cached value
return $this->cached_email;
}
// get the email
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
// cache the value
$this->cached_email = $response->email;
// and return
return $this->cached_email;
}
Depending on your use case make adjustments (ie. session, cache , static property...).
Extend a the Eloquent Model class
namespace App\Models\Utils;
use Illuminate\Database\Eloquent\Model as OldModel;
class MyModel extends OldModel
{
private $cachedAttributes = [];
public function getCachedAttribute(string $key, Callable $callable)
{
if (!array_key_exists($key, $this->cachedAttributes)) {
$this->setCachedAttribute($key, call_user_func($callable));
}
return $this->cachedAttributes[$key];
}
public function setCachedAttribute(string $key, $value)
{
return $this->cachedAttributes[$key] = $value;
}
public function refresh()
{
unset($this->cachedAttributes);
return parent::refresh();
}
}
make your class
class ElementWithEmail extends MyModel
{
const ATTRIBUTE_KEY_FOR_EMAIL = 'Email';
public function getEmailAttribute(){
$key = self::ATTRIBUTE_KEY_FOR_EMAIL;
$callable = [$this, 'getEmail'];
return $this->getCachedAttribute($key, $callable);
}
protected function getEmail()
{
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
return $response->email;
}
}
Call it from your code
$element = new ElementWithEmail();
echo $element->email;

CodeIgniter: My upsert function not working with my_model / my_controller

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?

Resources