variable scope migrating from php5/smarty2.6 to php7/smarty3.1 - php-7

I'm currently trying to upgrade a few of our sites that were in PHP5 and Smarty 2.6 to PHP7 and Smarty 3.1.31.
We previously had a site with a system of classes extending other classes, here is a simplified version of it:
class Site extends WebView
{
//functions
}
class WebView extends LandingPage
{
//functions
}
class LandingPage extends Smarty
{
function LandingPage()
{
$this->sessionInit = false;
$this->flag = array();
$this->engineVersion = '';
$this->charset = 'utf-8';
$this->content_type = 'text/html';
$this->strings = array();
$this->rtid=null;
}
}
and before, this worked fine in that all of the variables were correctly site using $this->variable_name. i oculd access them directly using $this, such as $this->flag['TEST'] from inside classes, or externally i could use $site, such as $site->flag['TEST']. Now however I am getting a series of undefined property errors
Notice: Undefined property: Site::$sessionInit in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
Notice: Undefined property: Site::$flag in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
Notice: Undefined property: Site::$engineVersion in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
Notice: Undefined property: Site::$charset in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
Notice: Undefined property: Site::$content_type in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
Notice: Undefined property: Site::$strings in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
Notice: Undefined property: Site::$rtid in /var/www/html/vendor/smarty/smarty/libs/Smarty.class.php on line 1447
I went into the Smarty class (obviously I didn't write this) and grabbed out the function for line 1447, which is the generic setter:
/**
* <<magic>> Generic setter.
* Calls the appropriate setter function.
* Issues an E_USER_NOTICE if no valid setter is found.
*
* #param string $name property name
* #param mixed $value parameter passed to setter
*/
public function __set($name, $value)
{
if (isset($this->accessMap[ $name ])) {
$method = 'set' . $this->accessMap[ $name ];
$this->{$method}($value);
} elseif (in_array($name, $this->obsoleteProperties)) {
return;
} else {
if (is_object($value) && method_exists($value, $name)) {
$this->$name = $value;
} else {
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
}
}
}
it's generic, and used everywhere, so i would assume that is fine and the issue is how i am calling it. what's getting me is how the errors refer to Site::$sessionInit, as if the variable had to be site in the Site, and is getting mad when i try and set it in the landingpage.
i know that variable scope has changed some in php7, is this telling me i need setters for all my variables in the site class? would i have to do one for each variable or would a generic work? (it seems like smarty already has a generic that is not working)

Notice: Undefined property: Site::$sessionInit in /var/www/html/vendor ....
....
....
this kind of error doesn't means that the variables is called from out of it's scopes.
this is mean that you are trying to call a variable which is not defined anywhere before.
and to solve this , you simply need to define those variables as follows:
class LandingPage extends Smarty
{
public $sessionInit;
public $flag; // or what ever the right visibility term
// .... and so on
function LandingPage()
{
$this->sessionInit = false;
$this->flag = array();
$this->engineVersion = '';
$this->charset = 'utf-8';
$this->content_type = 'text/html';
$this->strings = array();
$this->rtid=null;
}
}

Related

Too few arguments to function Livewire\LivewireManager::mount(), 0 passed in

Thanks in advance for helpful advice. I am using Laravel Livewire for creating components and Jetstrap for authentication for those routes that require it.
At the moment I only have one route set up for testing authentication, yet after I have logged in to view that route, I get the following error:
Too few arguments to function Livewire\LivewireManager::mount(), 0 passed in /var/www/mvp/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 261 and at least 1 expected
This seems to be originating from the LivewireManager class, inside the getInstance() function:
public function getInstance($component, $id)
{
$componentClass = $this->getClass($component);
throw_unless(class_exists($componentClass), new ComponentNotFoundException(
"Component [{$component}] class not found: [{$componentClass}]"
));
return new $componentClass($id);
}
It seems to be expecting a component argument from the Facade class in /vendor/laravel/framework/src/Illuminate/Support/Facades/, but isn't getting the component it needs. I checked the page code, and there is definitely a component there.
The Facade function creating the error:
/**
* Handle dynamic, static calls to the object.
*
* #param string $method
* #param array $args
* #return mixed
*
* #throws \RuntimeException
*/
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
return $instance->$method(...$args);
}
And the page that's supposed to be loading its component:
#extends('layouts.app')
#section('content')
#livewire('component')
#stop
Is there a simple way to fix the problem? Or am I missing something?
I got same error like what you get when i try to passing parameter and then i'm solving the problem by following the documentation with changing the way to render livewire component from using blade directive #livewire() into <livewire: >

Working with checkboxes - unable to check if checkboxes are disabled

Looking for some review on this to let me know if this is the right approach to check for disabled checkboxes.
Part of my page model here:
class eligibleAccountType {
constructor (text) {
this.label = label.withText(text);
this.checkbox = this.label.find('input[type=checkbox]');
}
}
class ProgramOptionsSubscriptionRulesPage{
constructor(){
this.contractsatAccountLevel = Selector("#program_option_allow_participant_account_contracts")
this.eligibleAccountTypesList = [
new eligibleAccountType("Residential"),
new eligibleAccountType("Commercial"),
new eligibleAccountType("Industrial")
];
Part of my test here
if (userdata.userrole == "Read Only") {
for (const eligibleAccountType of programOptionsSubscriptionRulesPage.eligibleAccountTypeList) {
await t.expect(eligibleAccountType.hasAttribute('disabled')).ok()
}
}
Getting error such as:
ReferenceError: label is not defined
I think I found out the problem, I had not defined the
const label = Selector('label');
I see no label definition in your example. You can try to rewrite your eligibleAccountType constructor by using Selector:
class eligibleAccountType {
constructor (text) {
this.label = Selector(...).withText(text);
this.checkbox = Selector(...).find('input[type=checkbox]');
}
}
In this situation it may be useful to check the markup of required elements. Please refer to the "TestCafe Examples" repository: https://github.com/DevExpress/testcafe-examples/blob/master/examples/element-properties/check-element-markup.js
Update:
and now I see that my list is actually not even building and I get this error " 1) TypeError: programOptionsSubscriptionRulesPage.eligibleAccountTypeList is not iterable"
It seems like you have a naming mistake in your loop:
for (const eligibleAccountType of programOptionsSubscriptionRulesPage.eligibleAccountTypeList) {
According to your ProgramOptionsSubscriptionRulesPage class definition, the list name should be eligibleAccountTypesList (with the "s" character).

Respect\Validation custom Rule with PDO?

I am learning Slim Framework v4 and decided to use Respect\Validation to validate inputted data and have hit a snag where I do not know how to inject the PDO into my custom rule I created.
The idea is to validate some inputs against the database if the provided data exist (or in another instances, if it was inputted correctly). In this specific case, I am tying to validate user's credentials for log in. My idea is this:
AuthController.php:
v::with('app\\Validators\\');
$userValidation = v::notBlank()->email()->length(null, 255)->EmailExists()->setName('email');
EmailExists() is my custom rule.
EmailExists.php:
namespace app\Validators;
use PDO;
use Respect\Validation\Rules\AbstractRule;
class EmailExists extends AbstractRule
{
protected $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function validate($input, $id = null)
{
// a PDO query that checks if the email exists in database
}
}
But I get an error of Too few arguments to function app\Validators\EmailExists::__construct(), 0 passed and exactly 1 expected, which is somewhat expected since the AbstractRule does not have a PDO injected and my class extends it.
So how to inject the PDO interface so that I can use it in my custom rules?
Are you guys using another approach in validating this kind of data? Do note that I am writing an API, so the database validation is somewhat a must and after Googling for past two days, I have no solutions at hand.
I am also using a PHP-DI where I create PDO interface. This is my dependencies.php file:
declare(strict_types=1);
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use app\Handlers\SessionMiddleware;
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
PDO::class => function (ContainerInterface $c) {
$settings = $c->get('settings')['db'];
$db = new PDO("mysql:host={$settings['host']};dbname={$settings['database']};charset={$settings['charset']},{$settings['username']},{$settings['password']}");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8',time_zone='{$offset}'");
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $db;
},
'session' => function(ContainerInterface $c) {
return new SessionMiddleware;
}
]);
};
And (part of) index.php:
declare(strict_types=1);
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);
// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);
// Build PHP-DI Container instance
$container = $containerBuilder->build();
// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
// Add Routing Middleware
$app->addRoutingMiddleware();
// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
Any help would be appreciated.
Use your user model to count the number of rows in the user table where there is a hit.
If it is not exactly 0, the check returns false, if it is exactly 0, the check passes.
So you don't have to include a PDO at this point. I use Slim 3 and that works quite well.
namespace app\Validators;
use Respect\Validation\Rules\AbstractRule;
class EmailAvailable extends AbstractRule {
/**
* #param $input
*
* #return bool
*/
public function validate ($sInput) {
return User::where('user_email', $sInput)->count() === 0;
}
}
class EmailAvailable extends AbstractRule {
/**
* #param $input
*
* #return bool
*/
public function validate ($sInput) {
return User::where('user_email', $sInput)->count() === 0;
}
}

Configuration::get() returning empty value in Controller of PrestaShop 1.6

I am using PrestaShop 1.6.1.0
I have a field in my module supporting different languages, the postProcess() looks like this:
protected function postProcess()
{
$languages = Language::getLanguages(false);
$values = array();
foreach ($languages as $lang)
{
$values['CUSTOMPAGECONFIGURATION_HEADING_TEXT'][$lang['id_lang']] = Tools::getValue('CUSTOMPAGECONFIGURATION_HEADING_TEXT_'.$lang['id_lang']);
}
Configuration::updateValue('CUSTOMPAGECONFIGURATION_HEADING_TEXT', $values['CUSTOMPAGECONFIGURATION_HEADING_TEXT'], true);
return $this->displayConfirmation($this->l('The settings have been updated.'));
}
Problem:
When I try to get this configuration variable value in a custom controller (path: /controllers/front/CustomPageController.php) like shown below, it is not fetching any results:
$headtxt = Configuration::get('CUSTOMPAGECONFIGURATION_HEADING_TEXT');
print_r($headtxt);
exit;
It is not printing anything. I want to fetch this value in a controller, but it does not work and returns an empty string.
Site is using PrestaShop 1.6.1.0 and I tried many codes available. Am I missing something?
I am using this code in new controller created by me for custom page:
$headtxt = Configuration::get('CUSTOMPAGECONFIGURATION_HEADING_TEXT');
print_r($headtxt);
exit;
I expect to output results of configuration variable which saved by the postProcess() method.
Hi #Ankur and Welcome to SO!
In your postProcess() method, you are saving this Configuration value as a multilingual value.
However, while calling the Configuration::get() static method, it seems to me you forgot to mention the language ID, here's the function prototype:
public static function get($key, $idLang = null, $idShopGroup = null, $idShop = null, $default = false)
This should work:
$id_lang = (int)$this->context->cookie->id_lang
$headtxt = Configuration::get('CUSTOMPAGECONFIGURATION_HEADING_TEXT', $id_lang);
print_r($headtxt);
exit;

How to call a console command in web application action in Yii?

I have a console command to do a consumer time, AND I need to know how to call (execute) it in a web application action in YII.
class MyCommand extends CConsoleCommand{
public function actionIndex(){
$model = new Product();
$model->title = 'my product';
...
$model->save();
.
.
.
}
}
I want to execute this code.
try this:
Yii::import('application.commands.*');
$command = new MyCommand("test", "test");
$command->run(null);
The 2 parameters with value "test" must be set but do not have an impact, they are used for the --help option when using the console.
/**
* Constructor.
* #param string $name name of the command
* #param CConsoleCommandRunner $runner the command runner
*/
public function __construct($name,$runner)
{
$this->_name=$name;
$this->_runner=$runner;
$this->attachBehaviors($this->behaviors());
}
https://github.com/yiisoft/yii/blob/master/framework/console/CConsoleCommand.php#L65
Try this
Yii::import('application.commands.*');
$command = new GearmanCommand('start', Yii::app()->commandRunner);
$command->run(array('start', '--daemonize', '--initd'));
where array('start', '--daemonize', '--initd') is a action and action parameters
I had same problem - i need to call action from inside controller and from command
I said same problem because it actually same - you have action which you need to call from console, and call it from controller too.
If you need to call an action(command) as a part of controller action, then i think you need to modify this solution a little. Or is my solution is enough for you?
So here is my solution:
first create action as said in http://www.yiichina.net/doc/guide/1.1/en/basics.controller#action
class NotifyUnsharedItemsAction extends CAction
{
public function run()
{
echo "ok";
}
}
then in controller action is loaded as usuall:
class TestController extends Controller
{
public function actions() {
return array(
'notifyUnsharedItems'=>'application.controllers.actions.NotifyUnsharedItemsAction',
);
}
and in command i run action in such way:
class NotifyUnsharedItemsCommand extends CConsoleCommand
{
public function run($args)
{
$action = Yii::createComponent('application.controllers.actions.NotifyUnsharedItemsAction',$this,'notify');
$action->run();
}
}
Accepting that we are on linux server, for Yii 1.1 real life example would be:
$run = '/usr/bin/php ' . Yii::getPathOfAlias('root').'/yiic' [command]
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $run, '/dev/null', '/dev/null'));
This will run Yii console command in the background.
Yii is PHP -> you can use the standard php constructs specified at http://php.net/manual/en/function.exec.php and the related methods near the bottom of the page, depending on what exactly you want to achieve.
Also, another very clean solution from cebe on gist:
<?php
// ...
$runner=new CConsoleCommandRunner();
$runner->commands=array(
'commandName' => array(
'class' => 'application.commands.myCommand',
),
);
ob_start();
$runner->run(array(
'yiic',
'idbrights',
));
echo nl2br(htmlentities(ob_get_clean(), null, Yii::app()->charset));
Yii::app()->end();
Typically what you should do in these situations is refactor.
Move the "common" code out of the MyCommand and place it into a class located in the components folder.
Now you can place any head on top of the "common" code without altering your functionality. For example:
protected/components/Mywork.php:
<?php
class Mywork
{
public function doWork()
{
$model = new Product();
$model->title = 'my product';
...
$model->save();
...
}
}
protected/controller/MyworkController.php:
<?php
class MyworkController
{
public function actionDowork()
{
$mywork = new Mywork;
...
}
}
protected/commands/MyworkCommand.php:
<?php
class MyworkCommand extends CConsoleCommand
{
public function run($args)
{
$mywork = new Mywork;
...
}
}
This approach makes testing easier too as you can test Mywork as a single unit outside of the view you are using.