I need to capitalize all user data in Prestashop 1.6 - I've managed to get it done for name, etc. but I don't know where I can do this for the address.
My guess is AddressController's processSubmitAddress() method, but I cannot find where does it take the input, so I can strotupper() that. Thanks for any guidance.
A little late, but here it is. Just change the ucfirst for whatever you need, in this case strotupper(). This is for the customer's address. In same file there should be other files for different customer data. This works also for PS 1.7.2 (tested)
Go to yourprojectfolder/classes/Address.php
Search for public function add($autodate = true, $null_values = false) on line 169.
Replace :
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return true;
}
With:
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return true;
}
Search for public function update($null_values = false) on line 181.
Replace :
public function update($null_values = false)
{
// Empty related caches
if (isset(self::$_idCountries[$this->id])) {
unset(self::$_idCountries[$this->id]);
}
if (isset(self::$_idZones[$this->id])) {
unset(self::$_idZones[$this->id]);
}
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return parent::update($null_values);
}
With:
public function update($null_values = false)
{
// Empty related caches
if (isset(self::$_idCountries[$this->id])) {
unset(self::$_idCountries[$this->id]);
}
if (isset(self::$_idZones[$this->id])) {
unset(self::$_idZones[$this->id]);
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return parent::update($null_values);
}
Instead of changing the PrestaShop core. You can also use the override option.
Create the file yourprojectfolder/override/classes/Address.php and insert this code and save the file:
<?php
/**
* Fix for capitalize and submit the first letters of the name and address input fields
*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* #author Peter Visser <info#mark-app.com>
*/
class Address extends AddressCore
{
public function update($null_values = false)
{
// Empty related caches
if (isset(self::$_idCountries[$this->id])) {
unset(self::$_idCountries[$this->id]);
}
if (isset(self::$_idZones[$this->id])) {
unset(self::$_idZones[$this->id]);
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return parent::update($null_values);
}
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return true;
}
}
After that reset your cache for overrides bij deleting yourprojectfolder/cache/class_index.php
Source: https://www.prestashop.com/forums/topic/333935-capitalize-first-letters-of-address-input-fields-and-submit-to-database
Lacking the help, I decided to do a little workaround - I've actually set a trigger on the database, which on insert or update transforms the specified data with SQL's UPPER() function.
Related
How to extend this function. The task is getLinkedPages must be created and return content depending on the parameter.
/**
* Get the contents as a sorted collection tree.
*/
public function getTree(bool $showDrafts = false, bool $renderPages = false, bool $renderLinkedPages = false): Collection
{
$pages = $this->getPages($showDrafts, $renderPages);
$chapters = Chapter::visible()->where('book_id', '=', $this->book->id)->get();
$all = collect()->concat($pages)->concat($chapters);
$chapterMap = $chapters->keyBy('id');
$lonePages = collect();
$pages->groupBy('chapter_id')->each(function ($pages, $chapter_id) use ($chapterMap, &$lonePages) {
$chapter = $chapterMap->get($chapter_id);
if ($chapter) {
$chapter->setAttribute('visible_pages', collect($pages)->sortBy($this->bookChildSortFunc()));[enter image description here][1]
} else {
$lonePages = $lonePages->concat($pages);
}
});
$chapters->whereNull('visible_pages')->each(function (Chapter $chapter) {
$chapter->setAttribute('visible_pages', collect([]));
});
$all->each(function (Entity $entity) use ($renderPages) {
$entity->setRelation('book', $this->book);
if ($renderPages && $entity instanceof Page) {
$entity->html = (new PageContent($entity))->render();
}
});
return collect($chapters)->concat($lonePages)->sortBy($this->bookChildSortFunc());
}
This code works but it is not sensitive to case/capitalization.
public function search(){
if ($search = \Request::get('q')) {
$patrons = Patron::where(function($query) use ($search){
$query->where('barcode','=',"$search")
})->paginate(20);
}else{
$patrons = Patron::latest()->paginate(5);
}
return $patrons;
}
Example
Searching for 'banana123' should NOT be equal to 'BaNaNa123' and should return 0.
How can we make the search exact match? Thank you.
You have to use BINARY
use DB;
...
public function search(){
if ($search = \Request::get('q')) {
$patrons = Patron::where(function($query) use ($search){
$query->whereRaw("BINARY `barcode` = '$search'");
// or
// $query->where(DB::raw("BINARY `barcode`), $search);
})->paginate(20);
}else{
$patrons = Patron::latest()->paginate(5);
}
return $patrons;
}
Also if you are using one where and not chaining it to other conditions you don't need to use function inside where. you can simply write:
use DB;
...
public function search(){
if ($search = \Request::get('q')) {
$patrons = Patron::where(DB::raw("BINARY `barcode`"), $search)->paginate(20);
}else{
$patrons = Patron::latest()->paginate(5);
}
return $patrons;
}
How to detect postProcess PrestaShop 1.6 EDIT/ADD/DELETE mode?
I have code, something like this (detect edit not working...):
detect add - is ok
detect delete - is ok
<?php
public function postProcess()
{
if (Tools::isSubmit('deletems_admin_delivery_manager') && Tools::getValue('id_ms_admin_delivery_manager') != '')
{
$this->errors[] = Tools::displayError('DETECT DELETE');
} elseif (Tools::isSubmit('submitAdminDeliveryManager')) {
if (!$id_ms_admin_delivery_manager = Tools::getValue('id_ms_admin_delivery_manager')) {
$this->errors[] = Tools::displayError('DETECT ADD');
// this not working...
} elseif($id_ms_admin_delivery_manager = Tools::getValue('id_ms_admin_delivery_manager')) {
$this->errors[] = Tools::displayError('DETECT EDIT - NOT WORKING');
}
}
}
Replace = by == in your last condition.
Of course, you don't really need the third condition:
$obj = $this->loadObject(true);
if (Tools::isSubmit('deletems_admin_delivery_manager') && $obj->id)
{
$this->errors[] = Tools::displayError('DETECT DELETE');
} elseif (Tools::isSubmit('submitAdminDeliveryManager')) {
if (#$obj->id) {
$this->errors[] = Tools::displayError('DETECT EDIT');
} else {
$this->errors[] = Tools::displayError('DETECT ADD');
}
}
UPDATE:
also you can use the Prestashop methods:
public function processDelete()
{
//Delete
}
public function processSave()
{
//Add or Update
}
public function processAdd()
{
//Add
}
public function processUpdate()
{
//Update
}
I don't know what this function will return, just modified record attributes or anything other?
protected function beforeSave()
{
if ($this->getIsNewRecord())
{
$this->created = Yii::app()->localtime->UTCNow;
}
$this->lastmodified = Yii::app()->localtime->UTCNow;
if ($this->dob == '') {
$this->setAttribute('dob', null);
} else {
$this->dob=date('Y-m-d', strtotime($this->dob));
}
if($this->image!="")
{
$this->imgfile_name = $this->image->name;
$this->imgfile_type = $this->image->type;
$this->imgfile_size = $this->image->size;
}
$this->phone=substr($this->phone,0,3).substr($this->phone,4,3).substr($this->phone,8,4);
return parent::beforeSave();
}
CActiveRecord::beforeSave is supposed to return true if the model is in a valid state and can be saved, and false if it's not.
Is any way, how to create two classes, which will be referenced both way and will use only one FK? This interest me in One-to-One as like as One-to-Many cases.
f.e.:
Class First: Entity
{
Second second;
}
Class Second: Entity
{
First first;
}
String TwoWayReference()
{
First Fir = new First();
Second Sec = new Second();
Fir.second = Sec; // I need it is equivalent to: Sec.first = Fir;
if (Sec.first == Fir)
return "Is any way how to do this code works and code return this string?";
else
return "Or it is impossible?"
}
simplest would be
class First : Entity
{
private Second second;
public virtual Second Second
{
get { return this.second; }
set {
if (value != null)
{
value.First = this;
this.second = value;
}
}
}
}
class Second : Entity
{
private First first;
public virtual First First
{
get { return this.first; }
set {
if (value != null && value.Second != this)
{
value.Second = this;
this.first = value;
}
}
}
}