What does this do in PHP/Laravel? - php

I have been trying to figure out some code in Laravel by reading the docs and stuff. I came across a piece of code that I cant quit figure out what it is doing. Could someone explain this? Here is the piece of code in the project I am working on:
// Create placeholders for the mobile app's video settings
$publicVideosEnabled = ( $settings['publicVideosEnabled'] == 1 );
I am specifically asking about $settings['publicVideosEnabled'] == 1 section.
Thanks everyone!
EDIT: For more information, here is where some of the variables are coming from:
$location = Location::find($location_id);
$settings = $this->apiHelper->getFormattedLocationSettings($location);
$publicVideosEnabled = ( $settings['publicVideosEnabled'] == 1 );
I am trying to add this line of code:
$notificationsEnabled = ( $settings['notificationsEnabled'] == 1);
But I am getting this error:
Undefined index: notificationsEnabled at $notificationsEnabled = ( $settings['notificationsEnabled'] == 1);

If $settings['publicVideosEnabled'] equals 1 then $publicVideosEnabled is true, otherwise it is false.

Related

What does $this->genlib->ajaxOnly(); mean?

I've been trying to learn some php these days and to do that I've been reading some code on github. I saw this one project and decided to read the code from there I keep reading this specific line of code but I don't know what it really means. Can anyone explain it to me? (pardon my noobness)
public function lilt(){
$this->genlib->ajaxOnly();
$this->load->helper('text');
//set the sort order
$orderBy = $this->input->get('orderBy', TRUE) ? $this->input->get('orderBy', TRUE) : "name";
$orderFormat = $this->input->get('orderFormat', TRUE) ? $this->input->get('orderFormat', TRUE) : "ASC";
//count the total number of items in db
$totalItems = $this->db->count_all('items');
ajaxOnly() is a method of the genlib class. It seems the function of it is to display an error message if the incoming request was not made via ajax.

Prestashop Db::getInstance()->update() return Undefined variable

I tried to import my CSV file to update my products, it keeps give me time out and after 2 weeks, we ruled out the server is the problem ( we tried changing max_execution_time, memory_limit, or anything that we think would cause it)
The error logs showed following error:
PHP Notice: Undefined variable: return in /var/www/vhosts/22/xxxxxx/webspace/httpdocs/xxxxxx/controllers/admin/AdminImportController.php on line 1518
which is a customized code that we added to sort our products listing;
Following are the code
$prod_pos = get_object_vars($product);
if (isset($info['position']) && !empty($info['position']))
{
$update_prod_cat_id = array();
for ($i = 0; $i < count($product->category); $i++)
{
if (is_numeric($product->category[$i])){
$return &= Db::getInstance()->update('category_product', array(
'position' => $info['position'],
), '`id_category` = '.(int)$product->category[$i].' AND `id_product` = '.(int)$product->id);
}
else
{
$update_prod_cat_id[] = Category::searchByName($default_language_id, trim($product->category[$i]), true);
$return &= Db::getInstance()->update('category_product', array(
'position' => $info['position'],
), '`id_category` = '.(int)$pos_cat_id['id_category'].' AND `id_product` = '.(int)$product->id);
}
}
}
The funny thing is this code was working until we move the hosting to cloud hosting then every time we tried to upload a CSV file more than 300 lines, it will just timed out and the error log will show the above error!
I am wondering if the db_prefix needed for Db::getInstance()->update()
But I read it will automatically put it when we use this instance.
I am not sure. I tried everything I though it would work but it's not.
Any idea?
From what I see in the code, the varable $return is undefined on the two rows that is being assigned
May be the script really timeouts, but this can be confirmed during the debugging process. The code above seems OK.
The db prefix is automatically added in your case.

PHP Array breaking

This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}

How can i solve this error: llegal string offset 'text'?

I have this code :
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];}
I read about this error on this site but i have no ideea how to apply the fix on my particular code. I`m sorry if i repost the problem.
Try doing: var_dump($userdata->yim); to verify if yim does in fact exist and contains the key 'text'.
Or even just var_dump($userdata);
$userdata->yim['text'] is not set, so you should check for that first:
if(isset($userdata->yim['text']) && strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
However, if that section of code relies on that value, something is wrong before that and this will just hide that problem.
In order to debug this you will need to utilize print_r() so that you can see the contents of your object or array:
// see what $userdata contains - update your question with the results of the line below
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata, true).'</pre></div>';
// see what $userdata->yim contains - update your question with the results of the line below as well
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata->yim, true).'</pre></div>';
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']))
{
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];
}

Problem in implementing Sphinx API along with Cake php

I am working on project where I need to implement SphinxSearch with Cake php. So I am simply trying to use a component and behaviour into it. The link to it, is :-
http://bakery.cakephp.org/articles/eugenioclrc/2010/07/10/sphinx-component-and-behavior
I am requesting Sphinx API like below :
$sphinx = array('matchMode' => SPH_MATCH_ALL, 'sortMode' => array(SPH_SORT_EXTENDED => '#relevance DESC'));
$results = $this->ModelName->find('all', array('search' => 'Search_Query', 'sphinx' => $sphinx));
pr($result);
For above it is working fine ,but when I tried to minimise the response time querying to a particular field of the table (using extended match modes,i.e. SPH_MATCH_EXTENDED2) , Sphinx just fails to output any result. The extended query which I used is given below :-
$sphinx = array('matchMode' => SPH_MATCH_EXTENDED2, 'sortMode' => array(SPH_SORT_EXTENDED => '#relevance DESC'));
$results = $this->ModelName->find('all', array('search' => '#Field_name Search_Query', 'sphinx' => $sphinx));
pr($results);
Can anyone recognise where am I going wrong with it? Please help if I am going wrong some where.
Thanks in advance.
Btw, when you use EXTENDED2 mode make sure your rank mode is set accordingly.
Edit:
Anyway back to you problem, looking at that component/behavior code you can see right away that no error checking is done whatsoever. Try changing the code a bit so you can at least see the errors and/or warnings.
Component
if(!isset($query['search'])){
$result = self::$sphinx->Query('', $indexes);
} else {
$result = self::$sphinx->Query($query['search'], $indexes);
}
if ($result === false) {
// throw new SphinxException();
die(self::$sphinx->GetLastError());
}
$warn = self::$sphinx->GetLastWarning();
if ($warn) echo $warn;
Behavior
$result=$this->runtime[$model->alias]['sphinx']->search($s);
if ($result === false) {
die($this->runtime[$model->alias]['sphinx']->GetLastError());
}
$warn = $this->runtime[$model->alias]['sphinx']->GetLastWarning();
if ($warn) echo $warn;
I hope that helps.
As you said ,
Sphinx just fails to output any result.
That means it's an error :
Please check whether you have added the specific field to the indexing by using sql_query
Also check if the field you are searching for is not an attribute
As per the sphinx documentation :
Attributes, unlike the fields, are not full-text indexed. They are stored in the index, but it is not possible to search them as full-text, and attempting to do so results in an error.

Categories