Magento2: wrong config data value per store - magento2

I am trying to get some Magento2 custom core_config_data values, as explained in other topics, but I have some wrong values related to the store IDs. I will try to explain, let's start with some relevant code:
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) {..}
{
$store = $this->storeManager->getStore();
$this->logger->debug($store->getId() . ": " . $store->getCode());
$message = $this->scopeConfig->getValue(self::CONF_MESSAGE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$link = $this->scopeConfig->getValue(self::CONF_LINK,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
What happens here is that the store ID and the store code are correct.
In the logs I see
main.DEBUG: 3: tedesco
The values I got in $message and $link are not correct: they are the values of another store (the correct store ID should be 3 as shown from the debug log, but the value is the one from the store with ID 1).
Of course I have checked the DB and the values are just fine as shown in the picture: .
Magento 2.1.4.
Any hints?
Thanks in advance.

What happens if you pass store or store id to 3rd param?
$message = $this->scopeConfig->getValue(self::CONF_MESSAGE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);

Related

TYPO3 ConnectionPool find a file after the uid of the file reference and update data

The concept is that, after a successfull save of my object, it should update a text in the database (With a Hook). Lets call the field 'succText'. The table i would like to access is the sys_file but i only get the sys_file_reference id when i save the object. So i thought i could use the ConnectionPool to select the sys_file row of this file reference and then insert the data on the field 'succText'.
I tried this:
public function processDatamap_preProcessFieldArray(array &$fieldArray, $table, $id, \TYPO3\CMS\Core\DataHandling\DataHandler &$pObj) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
$findItemsId = $queryBuilder
->select('*')
->from('sys_file_reference')
->join(
'sys_file_reference',
'sys_file',
'reference',
$queryBuilder->expr()->eq('reference.uid', $queryBuilder->quoteIdentifier('uid_local'))
)
->where(
$queryBuilder->expr()->eq('uid_local', $queryBuilder->createNamedParameter($fieldArray['downloads'], \PDO::PARAM_INT))
)
->execute();
}
But this give me back the sys_file_reference id and not the id and the field values of the sys_file table.
As for the update, i havent tried it yet, cause i haven't figured out yet, how to get the row that needs to be updated. I gues with a subquery after the row is found, i don't really know.
The processDatamap_preProcessFieldArray is going to be renamed to post. I only have it this way in order to get the results on the backend.
Thanks in advance,
You might want to make use of the FileRepository class here.
$fileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\FileRepository::class);
$fileObjects = $fileRepository->findByRelation('tablename', 'fieldname', $uid);
Where $uid is the ID of the record that the files are connected to via file reference.
You will get back an array of file objects to deal with.
I resolved my problem by removing the first code and adding a filerepository instance.
$fileRepository = GeneralUtility::makeInstance(FileRepository::class);
$fileObjects = $fileRepository->findByRelation('targetTable', 'targetField', $uid);
VERY IMPORTANT!
If you are creating a new element then TYPO3 assigns a temp UID variable with a name that looks like this NEW45643476. In order to get the $uid from the processDatamap_afterDatabaseOperations you need to add this code before you get the instance of the fileRepository.
if (GeneralUtility::isFirstPartOfStr($uid, 'NEW')) {
$uid = $pObj->substNEWwithIDs[$uid];
}
Now as far as the text concerns, i extracted from a pdf. First i had to get the basename of the file in order to find its storage location and its name. Since i have only one file i don't really need a foreach loop and i can use the [0] as well. So the code looked like this:
$fileID = $fileObjects[0]->getOriginalFile()->getProperties()['uid'];
$fullPath[] = [PathUtility::basename($fileObjects[0]->getOriginalFile()->getStorage()->getConfiguration()['basePath']), PathUtility::basename($fileObjects[0]->getOriginalFile()->getIdentifier())];
This, gives me back an array looking like this:
array(1 item)
0 => array(2 items)
0 => 'fileadmin' (9 chars)
1 => 'MyPdf.pdf' (9 chars)
Now i need to save the text from every page in a variable. So the code looks like this:
$getPdfText = '';
foreach ($fullPath as $file) {
$parser = new Parser();
$pdf = $parser->parseFile(PATH_site . $file[0] . '/' . $file[1]);
$pages = $pdf->getPages();
foreach ($pages as $page) {
$getPdfText .= $page->getText();
}
}
Now that i have my text i want to add it on the database so i will be able to use it on my search action. I now use the connection pool to get the file from the sys_file.
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file');
$queryBuilder
->update('sys_file')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($fileID))
)
->set('pdf_text', $getPdfText)
->execute();
Now everytime i choose a PDF from my extension, i save its text on the database.
EXTRA CONTENT
If you want to include the PDFParser as well and you are on composer mode, then add this on your composer.json:
"smalot/pdfparser" : "*"
and on the autoload:
"Smalot\\PdfParser\\" : "Packages/smalot/pdfparser/src/"
Then under: yourExtension/Classes/Hooks/DataHandler.php add the namespace:
use Smalot\PdfParser\Parser;
Now you are able to use the getPages() and getText() functions.
The Documentation
If i missed something let me know and i will add it.

Get Line Items in an Invoice logic hook in SuiteCRM

Via a logic hook I'm trying to update fields of my products, after an invoice has been saved.
What I understand so far is, that I need to get the invoice related AOS_Products_Quotes and from there I could get the products, update the required fields and save the products. Does that sound about right?
The logic hook is being triggered but relationships won't load.
function decrement_stocks ( $bean, $event, $arguments) {
//$bean->product_value_c = $bean->$product_unit_price * $bean->product_qty;
$file = 'custom/modules/AOS_Invoices/decrement.txt';
// Get the Invoice ID:
$sInvoiceID = $bean->id;
$oInvoice = new AOS_Invoices();
$oInvoice->retrieve($sInvoiceID);
$oInvoice->load_relationship('aos_invoices_aos_product_quotes');
$aProductQuotes = $oInvoice->aos_invoices_aos_product_quotes->getBeans();
/*
$aLineItemslist = array();
foreach ($oInvoice->aos_invoices_aos_product_quotes->getBeans() as $lineitem) {
$aLineItemslist[$lineitem->id] = $lineitem;
}
*/
$sBean = var_export($bean, true);
$sInvoice = var_export($oInvoice, true);
$sProductQuotes = var_export($aProductQuotes, true);
$current = $sProductQuotes . "\n\n\n------\n\n\n" . $sInvoice . "\n\n\n------\n\n\n" . $sBean;
file_put_contents($file, $current);
}
The invoice is being retrieved just fine. But either load_relationship isn't doing anything ($sInvoice isn't changing with or without it) and $aProductQuotes is Null.
I'm working on SuiteCRM 7.8.3 and tried it on 7.9.1 as well without success. What am I doing wrong?
I'm not familiar with SuiteCRM specifics, however I'd always suggest to check:
Return value of retrieve(): bean or null?
If null, then no bean with the given ID was found.
In such case $oInvoice would stay empty (Your comment suggests that's not the case here though)
Return value of load_relationship(): true (success) or false (failure, check logs)
And I do wonder, why don't you use $bean?
Instead you seem to receive another copy/reference of $bean (and calling it $oInvoice)? Why?
Or did you mean to receive a different type bean that is somehow connected to $bean?
Then its surely doesn't have the same id as $bean, unless you specifically coded it that way.

Confluence: Accessing multiple values from param

I am trying to create a user macro in confluence that creates a number of links (can be however many) based on user input. The 2 params I use look like this:
## #param LNK:title=Link|type=string|required=true|multiple=true
## #param TTL:title=Title|type=string|required=true|multiple=true
The user will input something like this:
Link: link_1, link_2, link_3
Title: title_1, title_2, title_3
The macro should then create a list of links like this:
title_1; title_2....
My question is: how can I access the content of the LNK/TTL param so that I can retrieve link_1, link_2, etc.? I need to get some kind of index so that I can correctly link LNK[1] to TTL[1].
Any help is welcomed!
Thank you!
Ok, so I found a way (which seems a bit complicated) to do what I needed. Here's the code:
## #param VTP:title=VTP Number|type=string|required=true|multiple=true|desc=VTP number from JIRA (ex: VTP-1)
## #param TCI:title=Test Case Identifier |type=string|required=true|multiple=true|desc=Test Case Identifier (ex: IN_TC01)
#set ($LVTP = [])
#set ($LTCI = [])
#set ($VTP = $paramVTP.split(";"))
#set ($TCI = $paramTCI.split(";"))
#foreach ($element in $VTP)
#set ($xxx = $LVTP.add($element))
#end
#foreach ($element in $TCI)
#set( $xxx = $LTCI.add($element))
#end
#set ($end = ($LVTP.size() - 1))
#foreach ($i in [0..$end])
[$LTCI.get($i)]
#end
I had to do this since it seems that the only way to access the content of an array ($VTP and $TCI in my case) in Confluence if through the #foreach loop. Things like arrays.asList didn't work for me.
If anyone has a more elegant solution, please let me know.

How to get Zend_http_cookie value into another function or page

I have the Following functions searchAction() and recentsearchAction(). I store my cookie value as $row which is an array variable. The problem is I want to get my cookies value in recentsearchAction() method. How to get it. pls help. I m completely new in Zend.
public function searchAction()
{
$form=new Application_Form_Search();
$this->view->form = $form;
if($this->getRequest()->isPost()){
$formData = $this->getRequest()->getPost();
if($form->isValid($formData)){
$uname=$form->getValue('uname');
$search = new Application_Model_DbTable_Hobbies();
$row=$search->searchHobby($uname);
$cookie = new Zend_Http_Cookie('cookiename',$row,'localhost',time() + 7200);
echo $cookie->getName();
echo $cookie->getValue();
echo $cookie->getDomain();
echo $cookie->getPath();
echo $cookie->getExpiryTime();
$this->view->search=$row;
}
}
}
public function recentsearchAction()
{
}
The easiest way I know to persist data in ZF is to dump that data into Zend_Session_Namespace, this will preserve the data until you unset it or overwrite it. Zend_session will accept string, objects and arrays.
basic usage:
//save data to namespace named cookie
$sesion = new Zend_Session_Namespace('cookie');
//namespace = cookie, data = cookieData
$session->cookieData = $cookieData;
//get data from namespace in another controller or action
//every time you need to access the namespace you have instantiate Zend_Session_Namespace
//I usually put a line in init() of each controller assigning it to a protected property
protected $_sesion = new Zend_Session_Namespace('cookie');
//access array or objects parts in session in normal ways
//assuming session is protected/private property
echo $this->_session->cookieData->username;//if object
echo $this->_session->cookieData['email'];//if array, I think
echo $this->_sesion->cookieData; //if single string
To unset data in Zend_Session_Namespace
Zend_Session::namespaceGet('cookie');
Zend_Session::namespaceUnset('cookie');
I usually cast arrays I want to store as objects $data = (object)array('data') to make access simpler.
Try this :
$session=Zend_Session_Namespace("cookie");
$session->setExpirationSeconds(60);
$session->username;
$session->email;
Now you can use this session namespace in any page like:
$session=Zend_Session_Namespace("cookie");
echo "User Name : ".$session->username;
echo "Password : ".$session->email;
It will preserve you data for 60 seconds. You can provide as many as expiration time in seconds.

Help needed formatting Doctrine Query in Zend Framework

Can anyone tell me how to format the query below correctly in my controller.
Currently it gives me nothing in my FilteringSelect. However if I change it to >= I get back all the kennelIDs which is incorrect also but at least I'm getting something.
I've tested that the session variable is set and can confirm that there are kennels with the matching capacity.
// Create autocomplete selection for the service of this booking
public function servkennelAction()
{
$sessionKennelBooking = new Zend_Session_Namespace('sessionKennelBooking');
// disable layout and view rendering
$this->_helper->layout->disableLayout();
$this->getHelper('viewRenderer')->setNoRender(true);
// get list of grooming services for dogs from the table
$qry= Doctrine_Query::create()
->from('PetManager_Model_Kennels k');
//This should be set by default and narrows down the search criteria
if(isset($sessionKennelBooking->numPets)){
$b=(int)$sessionKennelBooking->numPets;
$qry->addWhere('k.capacity = ?','$b');
}
$result=$qry->fetchArray();
//generate and return JSON string using the primary key of the table
$data = new Zend_Dojo_Data('kennelID',$result);
echo $data->toJson();
}
Many thanks in Advance.
Graham
I think that addWhere condition is wrong. It has to be:
$qry->addWhere('k.capacity = ?', $b);
i.e. $b without quotes.