override searchProvider.php does not work - prestashop

Create : override/modules/ps_facetedsearch/src/Product/SearchProvider.php
Create : override/modules/ps_facetedsearch/ps_facetedsearch.php
Delete the cache manually
Version : 1.7.6.4
override SearchProvider.php doesn't work
override ps_facetedsearch.php work
I need to delete 2 types of sorting ... into SearchProvider.php:getAvailableSortOrders() but I never go through the overloaded function
if (!defined('PS_VERSION'))
exit;
class SearchProviderOverride extends SearchProvider
{
/**
* #return array
*/
public function getAvailableSortOrders()
{
$sortPosAsc = new SortOrder('product', 'position', 'asc');
$sortPriceAsc = new SortOrder('product', 'price', 'asc');
$sortPriceDesc = new SortOrder('product', 'price', 'desc');
$translator = $this->module->getTranslator();
die('hello not world!');
return [
$sortPosAsc->setLabel(
$translator->trans('Relevance', [], 'Modules.Facetedsearch.Shop')
),
$sortPriceAsc->setLabel(
$translator->trans('Price, low to high', [], 'Shop.Theme.Catalog')
),
$sortPriceDesc->setLabel(
$translator->trans('Price, high to low', [], 'Shop.Theme.Catalog')
),
];
}
}```

"getAvailableSortOrders" is a private method. You are probably only hidding the private method with your public one.
You should override the "runQuery" method since it's the only one that uses "getAvailableSortOrders". This way your overriden "runQuery" will call the appropriate "getAvailableSortOrders" method.
And finally, you can make your "getAvailableSortOrders" private since its only meant to be used in this class.
if (!defined('PS_VERSION'))
exit;
class SearchProviderOverride extends SearchProvider
{
/**
* #return array
*/
private function getAvailableSortOrders()
{
$sortPosAsc = new SortOrder('product', 'position', 'asc');
$sortPriceAsc = new SortOrder('product', 'price', 'asc');
$sortPriceDesc = new SortOrder('product', 'price', 'desc');
$translator = $this->module->getTranslator();
die('hello not world!');
return [
$sortPosAsc->setLabel(
$translator->trans('Relevance', [], 'Modules.Facetedsearch.Shop')
),
$sortPriceAsc->setLabel(
$translator->trans('Price, low to high', [], 'Shop.Theme.Catalog')
),
$sortPriceDesc->setLabel(
$translator->trans('Price, high to low', [], 'Shop.Theme.Catalog')
),
];
}
/**
* #param ProductSearchContext $context
* #param ProductSearchQuery $query
*
* #return ProductSearchResult
*/
public function runQuery(
ProductSearchContext $context,
ProductSearchQuery $query
) {
$result = new ProductSearchResult();
// extract the filter array from the Search query
$facetedSearchFilters = $this->filtersConverter->createFacetedSearchFiltersFromQuery($query);
$context = $this->module->getContext();
$facetedSearch = new Search($context);
// init the search with the initial population associated with the current filters
$facetedSearch->initSearch($facetedSearchFilters);
$orderBy = $query->getSortOrder()->toLegacyOrderBy(false);
$orderWay = $query->getSortOrder()->toLegacyOrderWay();
$filterProductSearch = new Filters\Products($facetedSearch);
// get the product associated with the current filter
$productsAndCount = $filterProductSearch->getProductByFilters(
$query->getResultsPerPage(),
$query->getPage(),
$orderBy,
$orderWay,
$facetedSearchFilters
);
$result
->setProducts($productsAndCount['products'])
->setTotalProductsCount($productsAndCount['count'])
->setAvailableSortOrders($this->getAvailableSortOrders());
// now get the filter blocks associated with the current search
$filterBlockSearch = new Filters\Block(
$facetedSearch->getSearchAdapter(),
$context,
$this->module->getDatabase()
);
$idShop = (int) $context->shop->id;
$idLang = (int) $context->language->id;
$idCurrency = (int) $context->currency->id;
$idCountry = (int) $context->country->id;
$idCategory = (int) $query->getIdCategory();
$filterHash = md5(
sprintf(
'%d-%d-%d-%d-%d-%s',
$idShop,
$idCurrency,
$idLang,
$idCategory,
$idCountry,
serialize($facetedSearchFilters)
)
);
$filterBlock = $filterBlockSearch->getFromCache($filterHash);
if (empty($filterBlock)) {
$filterBlock = $filterBlockSearch->getFilterBlock($productsAndCount['count'], $facetedSearchFilters);
$filterBlockSearch->insertIntoCache($filterHash, $filterBlock);
}
$facets = $this->filtersConverter->getFacetsFromFilterBlocks(
$filterBlock['filters']
);
$this->labelRangeFilters($facets);
$this->addEncodedFacetsToFilters($facets);
$this->hideUselessFacets($facets, (int) $result->getTotalProductsCount());
$facetCollection = new FacetCollection();
$nextMenu = $facetCollection->setFacets($facets);
$result->setFacetCollection($nextMenu);
$result->setEncodedFacets($this->facetsSerializer->serialize($facets));
return $result;
}
}

Related

Symfony 5 - A problem with package HTTP-Client (NativeHttpClient)

I have an error that I don't understand.
I'm trying to validate the creation of a MangoPay account and I'm using the Http-Client package for the APIs (I've also installed mangopay's package).
When I try to create one, this error shows up:
Unsupported option "0" passed to "Symfony\Component\HttpClient\NativeHttpClient", did you mean "auth_basic", "auth_bearer", "query", "headers", "body", "json", "user_data", "max_redirects", "http_version", "base_uri", "buffer", "on_progress", "resolve", "proxy", "no_proxy", "timeout", "max_duration", "bindto", "verify_peer", "verify_host", "cafile", "capath", "local_cert", "local_pk", "passphrase", "ciphers", "peer_fingerprint", "capture_peer_cert_chain", "extra"?
That's the file I'm working on:
<?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use MangoPay;
use App\Service\MockStorage;
class CallApiService
{
private $mangoPayApi;
private $client;
public function __construct(HttpClientInterface $httpClient)
{
$this->client = $httpClient;
$this->mangoPayApi = new MangoPay\MangoPayApi();
$this->mangoPayApi->Config->ClientId = $_ENV['CLIENT_ID'];
$this->mangoPayApi->Config->ClientPassword = $_ENV['API_KEY'];
// $this->mangoPayApi->Config->TemporaryFolder = 'mangotemp';
$this->mangoPayApi->OAuthTokenManager->RegisterCustomStorageStrategy(new MockStorage());
}
public function createProfilMango($form)
{
$date = date_format($form['birthday']->getData(), "Ymd");
$userMango = $this->client->request(
'POST',
$_ENV['SERVER_URL'] . '/' . $_ENV['VERSION'] . '/' . $_ENV['CLIENT_ID'] .'/users/natural',
[
$UserNatural = new MangoPay\UserNatural(),
$UserNatural->FirstName = $form['firstname']->getData(),
$UserNatural->LastName = $form['lastname']->getData(),
$UserNatural->Email = $form['email']->getData(),
$UserNatural->Address = new MangoPay\Address(),
$UserNatural->Address->AddressLine1 = $form['streetNumber']->getData() . " " . $form['address']->getData(),
$UserNatural->Address->AddressLine2 = "",
$UserNatural->Address->City = $form['city']->getData(),
$UserNatural->Address->Region = "",
$UserNatural->Address->PostalCode = $form['zipCode']->getData(),
$UserNatural->Address->Country = "FR",
$UserNatural->Birthday = intval($date),
$UserNatural->Nationality = $form['nationality']->getData(),
$UserNatural->CountryOfResidence = "FR",
$UserNatural->Capacity = "NORMAL",
$Result = $this->mangoPayApi->Users->Create($UserNatural),
]
);
return $userMango;
}
}
The CallApiService.php is called upon the signup controller of my website:
// RegistrationController.php
[...]
public function register(CallApiService $callApiService, User $user = null, Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppCustomAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
{
if(!$user){
$user = new User();
}
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$callApiService->createProfilMango($form);
// $entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
// return $userAuthenticator->authenticateUser(
// $user,
// $authenticator,
// $request,
// // 'main' // firewall name in security.yaml
// );
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
'editMode'=> $user-> getId() !== null,
]);
}
I've tried to change the $client value with new NativeHttpClient() and new CurlHttpClient() but the error doesn't change.
What's the error? How can I fix it?

how to show my admin tab in prestashop 1.7?

i succeeded to show my admin tab in ps 1.6 bu it does not appear in 1.7, here is my code :
public function installTab() {
$tab = new Tab();
$tab->id_parent = 0;
//$tab->id_parent = (int)Tab::getIdFromClassName('AdminCatalog');
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'Scan des codes barre';
}
$tab->class_name = 'AdminBarCodeGenerator';
$tab->module = $this->name;
$tab->active = 1;
return $tab->add();
}
and my controller's methods:
public function __construct(){
$this->bootstrap = true;
$this->display='';
$this->context = Context::getContext();
return parent::__construct();
}
public function renderList()
{
$scan_form=$this->renderForm2();
$this->context->smarty->assign('scan_form',$scan_form);
return $this->context->smarty->fetch(_PS_MODULE_DIR_.'barcode/views/templates/admin/tabs/scan.tpl');
}
is there a specific way to handle it in ps 1.7 please?
I think problem might be in setting parent id to 0. Try:
$tab->id_parent = (int)Tab::getIdFromClassName('DEFAULT');
Also your class name should start with Admin
This is how you have to do it;
public function installTab()
{
$tab = new Tab();
$tab->active = 1;
$tab->class_name = "NewsletterBuildingConfig";
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = "Newsletter Settings";
}
$tab->id_parent = (int)Tab::getIdFromClassName('AdminParentThemes');
$tab->module = $this->name;
$tab->add();
return true;
}
Make sure to install your tab in the install() function
$this->installTab('AdminParentThemes', 'NewsletterBuildingConfig', 'Newsletter Settings')
And then for your admin controller NewsletterBuildingConfig.php
class NewsletterBuildingConfigController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
// Redirect is not needed, you can display your tpl file here
Tools::redirect(
$this->context->link->getAdminLink('AdminModules', true).'&configure=newsletterbuilderandsender'
);
}
}
It's easy in new versions.
In main php file of your module(yourmodule.php):
public $tabs = [
[
'name' => 'Merchant Expertise', // One name for all langs
'class_name' => 'AdminGamification',
'visible' => true,
'parent_class_name' => 'ShopParameters',
],
];
name can be an array for languages:
[
'en' => 'Some text',
'fa' => 'متن تست'
]
If do you want to show the tabs event 1.6 and 1.7, use this structure :
For 1.7:
class mymodule extends Module
{
public $tabs = array(
array(
'name' => 'My Tab Name',
'class_name' => 'AdminMyModuleNameControllerName',
'visible' => true,
'parent_class_name' => 'AdminParentModulesSf',
),
);
....
and for 1.6:
public function install()
{ ...
if(!version_compare(_PS_VERSION_, '1.7', '>=')){
foreach ($this->tabs as $tabItem) {
$tab = new Tab();
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = $tabItem['name'];
}
$tab->class_name = $tabItem['class_name'];
$tab->id_parent = Tab::getIdFromClassName($tabItem['parent_class_name']);
$tab->module = $this->name;
$tab->position = 0;
$tab->active = $tabItem['visible'] ? 1 : 0;
$tab->save();
}
}
...
}

How to implement Custom Material Data Source for Data Table?

I'm trying to implement DataSource for Material DataTable with pagenator, sorting etc.
An example of implementation is described here: https://blog.angular-university.io/angular-material-data-table/
From service i'm get following model:
export interface IResult {
results: Flat[];
currentPage: number;
pageCount: number;
pageSize: number;
length: number;
firstRowOnPage: number;
lastRowOnPage: number;
}
Method in service looks following:
getObjects(sort: string, order: string,
pageNumber = 1, pageSize = 20): Observable<IResult> {
return this.http.get<IResult>(this.serviceUrl,
{
params: new HttpParams()
.set("sort", sort)
.set("order", order)
.set('pageNumber', pageNumber.toString())
.set('pageSize', pageSize.toString())
});
}
DataSource realization:
export class OtherDataSource implements DataSource<Flat> {
private flatSubject = new BehaviorSubject<Flat[]>([]);
private loadingSubject = new BehaviorSubject<boolean>(false);
public loading$ = this.loadingSubject.asObservable();
constructor(private service: ObjectsService) {
}
connect(collectionViewer: CollectionViewer): Observable<Flat[]> {
return this.flatSubject.asObservable();
}
disconnect(collectionViewer: CollectionViewer): void {
this.flatSubject.complete();
this.loadingSubject.complete();
}
loadData(filter = '',
sortDirection = 'asc', pageIndex = 1, pageSize = 20) {
this.loadingSubject.next(true);
this.service.getObjects(filter, sortDirection,
pageIndex, pageSize).pipe(
catchError(() => of([])),
finalize(() => this.loadingSubject.next(false))
)
.subscribe(obj => this.flatSubject.next(obj));
}
}
In subscribe(obj => this.flatSubject.next(obj)) i'm getting following error: IResult is not assignable to type Flat[]. I have no error when casting obj to <Flat[]>obj, also i see that's backend return data but result in UI is empty.
I think that error here subscribe(obj => this.flatSubject.next(<Flat[]>obj)) but have no ideas how it fixing. What I'm doing wrang?
I implemented an DataSource differently. Realization looks following:
export class NmarketDataSource extends DataSource<Flat> {
resultsLength = 0;
isLoadingResults = true;
isRateLimitReached = false;
cache$: Flat[];
constructor(private nmarketService: ObjectsService,
private sort: MatSort,
private paginator: MatPaginator) {
super();
}
connect(): Observable<Flat[]> {
const displayDataChanges = [
this.sort.sortChange,
this.paginator.page
];
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 1);
return merge(...displayDataChanges)
.pipe(
startWith(null),
switchMap(() => {
return this.nmarketService.getObjects(
this.sort.active,
this.sort.direction,
this.paginator.pageIndex+1,
this.paginator.pageSize);
}),
map(data => {
this.isLoadingResults = false;
this.isRateLimitReached = false;
this.resultsLength = data.rowCount;
this.cache$ = data.results;
return data.results;
}),
catchError(() => {
this.isLoadingResults = false;
this.isRateLimitReached = true;
return of([]);
})
);
}
disconnect() { }
}
It works but doesn't match in my case.
replace this code
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;">
</mat-row>
at the end of table
this is the error

what is returned by Engine_Api::_()->getTable

I wonder if it is an array that is returned by calling getTable('tbl', 'module') or an object. I am facing problems when I try to insert the returned value into another array and then encode it as JSON. Passing it per se to $this->_helper->json() generates JSON output successfully, but using it as part of another array just does not work and is a null array. See below for the controller code:
//put your code here
public function indexAction () {
}
public function browseAction () {
$resultArray = $this->prepareArray();
$this->sendResponse($resultArray);
}
/**
* HTTP Response Codes
*/
const HTTP_OK = 200;
public function sendResponse($data) {
ob_clean();
$this->_helper->json($data);
}
public function prepareArray() {
//Here we will prepare the array to be later encoded as JSON
$responseArray = [
'status_code' => '',
'body' => [
'itemsCount' => '',
'foods' => [
'food_id' => '',
'food_title' => '',
'image' => '',
],
],
];
$foodTable = Engine_Api::_()->getDbTable('foods', 'restapi');
$foods = $foodTable->getFoods($this->view);
$foodArray = [];
print_r($foods);
while ($row = mysqli_fetch_row($foods)) {
$foodArray['food_id'] = $row['food_id'];
$foodArray['food_title'] = $row['title'];
$foodArray['image'] = $row['image'];
}
error_log( $row ['food_id'] . ',' . $row['title']);
$responseArray['status_code'] = '200';
$responseArray['body']['itemsCount'] = count($foods);
$responseArray['body']['foods']= $foodsArray;
return $responseArray;
}
If everything is set properly, getTable('tbl', 'module') returns object(Module_Model_DbTable_Tbl). This is the model of your database table.
Your particular error is on the line where you're assigning foodsArray variable that isn't defined anywhere. You should have assign foodArray here instead.
$responseArray['body']['foods']= $foodsArray; // should be foodArray

get values between two dates in silverstripe

i have added two date fields. i want to retrieve the data between those two table.PaymentDate and ChequePostedDate are two fields. so i need to get the rows between two dates.
simply search content have two date fields. i want to retrieve the rows(data) between those two dates
public function __construct($modelClass, $fields = null, $filters = null) {
$fields = new FieldList(array(
DateField::create('PaymentDate','Payment Date : from')
->setConfig('dateformat', 'yyyy-MM-dd')
->setConfig('showcalendar', true)
->setAttribute('placeholder','YYYY-MM-DD')
->setDescription(sprintf(
_t('FormField.Example', 'e.g. %s', 'Example format'),
Convert::raw2xml(Zend_Date::now()->toString('yyyy-MM-dd'))
)),
DateField::create('ChequePostedDate','cr Date : to')
->setConfig('dateformat', 'yyyy-MM-dd')
->setConfig('showcalendar', true)
->setAttribute('placeholder','YYYY-MM-DD')
->setDescription(sprintf(
_t('FormField.Example', 'e.g. %s', 'Example format'),
Convert::raw2xml(Zend_Date::now()->toString('yyyy-MM-dd'))
)),
));
$filters = array(
'PaymentDate' => new PartialMatchFilter('PaymentDate'),
'ChequePostedDate' => new PartialMatchFilter('ChequePostedDate'),
);
parent::__construct($modelClass, $fields, $filters);
}
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) {
$dataList = parent::getQuery($searchParams, $sort, $limit, $existingQuery);
$params = is_object($searchParams) ? $searchParams->getVars() : $searchParams;
$query = $dataList->dataQuery();
if(!is_object($searchParams)) {
if (isset($params['PaymentDate'])&& $params['ChequePostedDate'] ) {
$query->where('`PaymentNote`.PaymentDate BETWEEN \''.$params['PaymentDate'].' \' AND \''.$params['ChequePostedDate'].'\'');
}
}
return $dataList->setDataQuery($query);
}
}
You can also use WithinRangeFilter something like the following, but you need to use the setMin(), setMax() methods as per this forum response: https://www.silverstripe.org/community/forums/form-questions/show/11685
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) {
$dataList = parent::getQuery($searchParams, $sort, $limit, $existingQuery);
$params = is_object($searchParams) ? $searchParams->getVars() : $searchParams;
$query = $dataList->dataQuery();
if(!is_object($searchParams)) {
if (!empty($params['PaymentDate'] && !empty($params['ChequePostedDate'])) {
return $dataList->filter('PaymentDate:WithinRange', [$params['PaymentDate'], $params['ChequePostedDate']]);
}
}
return $dataList;
}
i solved it..
simply remove $filters
$filters = array(
// 'PaymentDate' => new PartialMatchFilter('PaymentDate'),
//'ChequePostedDate' => new PartialMatchFilter('ChequePostedDate'),
);
then it works