opencart creating and using a custom helper - php

I created a helper in opencart following information I found here on stackoverflow, but I have a problem:
First I created a helper file called general.php and placed inside the folder:
system/helper/general.php
Then started in startup.php file
require_once(DIR_SYSTEM 'helper/general.php');
Finally, I use it inside the controller: register.php which is inside the folder catalog/controller/account/register.php.
I used it in this way:
if (empty($this->request->post['doc']) && $this->general->validate($this->request->post['doc'])) {
$this->error['doc'] = 'doc is invalid';
}
And is returning the following error:
Fatal error: Call to a member function validate() on a non-object in / home/centralshopdistribuidora/www/vqmod/vqcache/vq2-catalog_controller_account_register.php on line 515
line 515:
if (empty($this->request->post['doc']) && $this->general->validate($this->request->post['doc'])) {
The general.php file:
function validate($doc) {
$d1 = 0;
$d2 = 0;
$doc = preg_replace("/[^0-9]/", "", $doc);
$ignore_list = array(
'00000000000',
'01234567890',
'11111111111',
'22222222222',
'33333333333',
'44444444444',
'55555555555',
'66666666666',
'77777777777',
'88888888888',
'99999999999'
);
if (strlen($doc) != 11 || in_array($doc, $ignore_list)) {
return false;
} else {
for ($i = 0; $i < 9; $i++) {
$d1 += $doc[$i] * (10 - $i);
}
$r1 = $d1 % 11;
$d1 = ($r1 > 1) ? (11 - $r1) : 0;
for ($i = 0; $i < 9; $i++) {
$d2 += $doc[$i] * (11 - $i);
}
$r2 = ($d2 + ($d1 * 2)) % 11;
$d2 = ($r2 > 1) ? (11 - $r2) : 0;
return (substr($doc, -2) == $d1 . $d2) ? true : false;
}
}

You first need to register this helper into the registry, e.g. in Your index.php find line
// Encryption
$registry->set('encryption', new Encryption($config->get('config_encryption')));
(referring to OC 1.5.5 and higher) and after this register Your helper the same way, e.g.
// General
$registry->set('general', new General()));
This should be enough. Make sure to change the constructor call if You have some dependencies...

Related

Nested recursive function is throwing an error [duplicate]

This question already has answers here:
"Fatal error: Cannot redeclare <function>"
(18 answers)
Closed 2 years ago.
I have a nested recursive function it's a task from codewars.
persistence(39) === 3; // because 3 * 9 = 27, 2 * 7 = 14, 1 * 4 = 4
and 4 has only one digit persistence(999) === 4; // because 9 * 9 * 9
= 729, 7 * 2 * 9 = 126, 1 * 2 * 6 = 12, and finally 1 * 2 = 2;
persistence(4) === 0; // because 4 is already a one-digit number
which throws :
Fatal error: Cannot redeclare rec();
function persistence(int $num): int {
$str = (string)$num;
$nums = str_split($str);
$count = count($nums);
if( $count === 0 ){
return 0;
}
$x = 0;
function rec($nums, $n) {
$result = [];
for ($i = 0; $i < count($nums); ++$i) {
if ($i == 0) {
$result = $nums[$i];
} else {
$result *= $nums[$i];
}
}
$num = implode('',$nums);
$diff = $num - $result;
if ( $diff != 0 ) {
$n += 1;
$result = str_split($result,1);
return rec($result, $n);
}
return $n;
}
$result = rec($nums,$x);
return (int)$result;
}
I've tried to use an variable function
$rec = 'rec';
$result = $rec($nums,$n)
but there are another error : `
Fatal error: Cannot redeclare rec()
`
PHP 7.4 version. Can anyone explain me what the problem is ?
Don't define the function inside the other function:
You have:
function persistence(int $num): int {
// code ...
function rec($nums, $n) {
// code ...
}
}
This means that the function rec should be created each time the function persistence gets called.
You should define them first:
function persistence(int $num): int {
// code ...
}
function rec($nums, $n) {
// code ...
}
And then call them to your liking, for example like you already do:
$rec = 'rec';
$result = $rec($nums,$n)

Sort by time PHP

i have this function sort_by_time, the purpose of this function is to swap the time1 if the time2 is greater than time1 but my problem is im having an error of Undefined offset: 0. Sometimes the error is Undefined offset: 1. or Undefined offset: 2. Can someone help me to prevent this error in my code? I'm thinking this in these last 3 days but i can't think of any solution on this.
in this line the error occur.
if (Payroll2::convert_time_in_minutes($_time[$j]->time_in) > Payroll2::convert_time_in_minutes($_time[$j+1]->time_in))
This error occurs because the $_time[0] is not set.
Sample time. This is dynamic not only limited to 3 sometimes it's 4, sometimes it's 5 or 1.
1 => {#6356}
2 => {#6352}
3 => {#6257}
Here's my full function code
public static function sort_by_time($_time)
{
$count = 0;
$n = count($_time);
for ($i = 0; $i < $n-1; $i++)
{
for ($j = 0; $j < $n-$i-1; $j++)
{
if (Payroll2::convert_time_in_minutes($_time[$j]->time_in) > Payroll2::convert_time_in_minutes($_time[$j+1]->time_in))
{
// swap temp and arr[i]
$temp = $_time[$j];
$_time[$j] = $_time[$j+1];
$_time[$j+1] = $temp;
}
}
}
return $_time;
}
Try this:
public static function sort_by_time($_time)
{
usort($_time,function($ad,$bd)
{
$ad = Payroll2::convert_time_in_minutes($ad->time_in);
$bd = Payroll2::convert_time_in_minutes($bd->time_in);
if ($ad == $bd) {
return 0;
}
return $ad < $bd ? -1 : 1;
});
return $_time;
}
if it doesn't work the problem can be in function Payroll2::convert_time_in_minutes

How I can use Eloquent 5.3 Pagination with Twig

lately I have upgraded my Eloquent to the latest version and the pagination stoped working. Basically when I call the links(), in the twig, to print the pagination I'm getting the following error message
Fatal error: Uncaught Error: Call to a member function make() on null in /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/plugins/crm/vendor/illuminate/pagination/LengthAwarePaginator.php:140
Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/plugins/crm/vendor/illuminate/pagination/LengthAwarePaginator.php(119): Illuminate\Pagination\LengthAwarePaginator->render(NULL)
#1 /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/plugins/crm/app/Providers/CMSServiceProvider.php(115): Illuminate\Pagination\LengthAwarePaginator->links()
#2 /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/twig-cache/18/189d0df44f1fe623fdad2b6fb631c15897dda3614de070c872657111c85f5f1a.php(333): Herbert\Framework\Application->{closure}(Object(Illuminate\Pagination\LengthAwarePaginator))
#3 /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/plugins/properties-integration/ in /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/plugins/crm/vendor/illuminate/pagination/LengthAwarePaginator.php on line 140
Then if I do dd($clients->links()) inside my controller I'm getting the following error message
Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /Applications/XAMPP/xamppfiles/htdocs/*****/public_html/wp-content/plugins/crm/vendor/illuminate/pagination/AbstractPaginator.php on line 377
but if I do dd($clients) I'm getting the Eloquent object with the selected records and the current page.
Here are the links of the classes
https://github.com/illuminate/pagination/blob/5.3/LengthAwarePaginator.php#L140-L143
https://github.com/illuminate/pagination/blob/master/AbstractPaginator.php#L405-L424
UPDATE
I did a temporary solution which basically I used the available functions of the AbstractPaginator. If anyone has a better approach please feel free to post it.
$function = new Twig_SimpleFunction("render", function ($object) {
$html = '';
$data = array(); // start out array
$data['pages'] = ceil($object->total() / $object->perPage()); // calc pages
$data['si'] = ($object->currentPage() * $object->perPage()) - $object->perPage(); // what row to start at
$data['curr_page'] = $object->currentPage(); // Whats the current page
$max = 7;
if ($data['curr_page'] < $max) {
$sp = 1;
} elseif ($data['curr_page'] >= ($data['pages'] - floor($max / 2))) {
$sp = $data['pages'] - $max + 1;
} elseif ($data['curr_page'] >= $max) {
$sp = $data['curr_page'] - floor($max / 2);
}
if ($data['curr_page'] > $max) {
$html .= '<li >First Page</li>';
}
for ($i = $sp; $i <= ($sp + $max - 1); $i++) {
if ($i > $data['pages']) {
continue;
}
if ($object->currentPage() == $i) {
$html .= ' <li class="active">';
} else {
$html .= '<li >';
}
$html .= ' ' . $i . '
</li>';
}
if ($data['curr_page'] < ($data['pages'] - floor($max / 2))) {
$html .= '<li >Last Page</li>';
}
return $html;
});

Fatal error : Using $this when not in object context in [duplicate]

This question already has answers here:
PHP Fatal error: Using $this when not in object context
(9 answers)
Closed 7 years ago.
I have the below function, But when I run the code make error like:
Fatal error: Using $this when not in object context in E:....
How to fix it. I replace $this-> with self:: but it failed too.
Please help in this regards,
<?php
function cehck_files()
{
$file1 = 'C:\xampp\htdocs\test\test1.php';
$file2 = 'C:\xampp\htdocs\test\test2.php';
$test = $this->compareFiles($file1,$file2,true);
$test_display = $this->toTable($test);
echo "<pre>";
print_r($test_display);
print_r($test);
echo "</pre>";
}
function compareFiles($file1, $file2, $compareCharacters = false) {
return $this->compare(file_get_contents($file1),file_get_contents($file2),$compareCharacters);
}
function compare($string1, $string2, $compareCharacters = false) {
$start = 0;
if ($compareCharacters){
$sequence1 = $string1;
$sequence2 = $string2;
$end1 = strlen($string1) - 1;
$end2 = strlen($string2) - 1;
} else {
$sequence1 = preg_split('/\R/', $string1);
$sequence2 = preg_split('/\R/', $string2);
$end1 = count($sequence1) - 1;
$end2 = count($sequence2) - 1;
}
// skip any common prefix
while ($start <= $end1 && $start <= $end2 && $sequence1[$start] == $sequence2[$start]) {
$start ++;
}
// skip any common suffix
while ($end1 >= $start && $end2 >= $start && $sequence1[$end1] == $sequence2[$end2]) {
$end1 --;
$end2 --;
}
// compute the table of longest common subsequence lengths
$table = self::computeTable($sequence1, $sequence2, $start, $end1, $end2);
// generate the partial diff
$partialDiff =
self::generatePartialDiff($table, $sequence1, $sequence2, $start);
// generate the full diff
$diff = array();
for ($index = 0; $index < $start; $index ++){
$diff[] = array($sequence1[$index], UNMODIFIED);
}
while (count($partialDiff) > 0) $diff[] = array_pop($partialDiff);
for ($index = $end1 + 1; $index < ($compareCharacters ? strlen($sequence1) : count($sequence1)); $index ++) {
$diff[] = array($sequence1[$index], UNMODIFIED);
}
// return the diff
return $diff;
}
function computeTable($sequence1, $sequence2, $start, $end1, $end2) {
$length1 = $end1 - $start + 1;
$length2 = $end2 - $start + 1;
// initialise the table
$table = array(array_fill(0, $length2 + 1, 0));
// loop over the rows
for ($index1 = 1; $index1 <= $length1; $index1 ++) {
// create the new row
$table[$index1] = array(0);
// loop over the columns
for ($index2 = 1; $index2 <= $length2; $index2 ++){
// store the longest common subsequence length
if ($sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]) {
$table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1;
} else {
$table[$index1][$index2] =
max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]);
}
}
}
// return the table
return $table;
}
function generatePartialDiff( $table, $sequence1, $sequence2, $start ) {
$diff = array();
// initialise the indices
$index1 = count($table) - 1;
$index2 = count($table[0]) - 1;
// loop until there are no items remaining in either sequence
while ($index1 > 0 || $index2 > 0){
// check what has happened to the items at these indices
if ($index1 > 0 && $index2 > 0 && $sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]) {
// update the diff and the indices
$diff[] = array($sequence1[$index1 + $start - 1], UNMODIFIED);
$index1 --;
$index2 --;
} elseif ($index2 > 0 && $table[$index1][$index2] == $table[$index1][$index2 - 1]) {
// update the diff and the indices
$diff[] = array($sequence2[$index2 + $start - 1], INSERTED);
$index2 --;
}else{
// update the diff and the indices
$diff[] = array($sequence1[$index1 + $start - 1], DELETED);
$index1 --;
}
}
// return the diff
return $diff;
}
function toTable($diff, $indentation = '', $separator = '<br>') {
$html = $indentation . "<table class=\"diff\">\n";
// loop over the lines in the diff
$index = 0;
while ($index < count($diff)){
// determine the line type
switch ($diff[$index][1]){
// display the content on the left and right
case UNMODIFIED:
$leftCell =
self::getCellContent(
$diff, $indentation, $separator, $index, UNMODIFIED);
$rightCell = $leftCell;
break;
// display the deleted on the left and inserted content on the right
case DELETED:
$leftCell =
self::getCellContent(
$diff, $indentation, $separator, $index, DELETED);
$rightCell =
self::getCellContent(
$diff, $indentation, $separator, $index, INSERTED);
break;
// display the inserted content on the right
case INSERTED:
$leftCell = '';
$rightCell =
self::getCellContent(
$diff, $indentation, $separator, $index, INSERTED);
break;
}
// extend the HTML with the new row
$html .=
$indentation
. " <tr>\n"
. $indentation
. ' <td class="diff'
. ($leftCell == $rightCell
? 'Unmodified'
: ($leftCell == '' ? 'Blank' : 'Deleted'))
. '">'
. $leftCell
. "</td>\n"
. $indentation
. ' <td class="diff'
. ($leftCell == $rightCell
? 'Unmodified'
: ($rightCell == '' ? 'Blank' : 'Inserted'))
. '">'
. $rightCell
. "</td>\n"
. $indentation
. " </tr>\n";
}
// return the HTML
return $html . $indentation . "</table>\n";
}
?>
You are using $this for a function which is not a method of any class.
Instead of
$test = $this->compareFiles($file1,$file2,true);
Use:
$test = compareFiles($file1,$file2,true);
Also, change
return $this->compare(file_get_contents($file1),file_get_contents($file2),$compareCharacters);
To
return compare(file_get_contents($file1),file_get_contents($file2),$compareCharacters);
And to the remaining changes in this way.

Separating an array effectively

I'm having an asbolute nightmare dealing with an array of numbers which has the following structure :
Odd numbers in the array : NumberRepresenting Week
Even numbers in the array : NumberRepresenting Time
So for example in the array :
index : value
0 : 9
1 : 1
2 : 10
3 : 1
Would mean 9 + 10 on Day 1 (Monday).
The problem is, I have a an unpredictable number of these and I need to work out how many "sessions" there are per day. The rules of a session are that if they are on a different day they are automatically different sessions. If they are next to each other like in the example 9 + 10 that would count as a single session. The maximum number than can be directly next to eachother is 3. After this there needs to be a minimum of a 1 session break in between to count as a new session.
Unfortunately, we cannot also assume that the data will be sorted. It will always follow the even / odd pattern BUT could potentially not have sessions stored next to each other logically in the array.
I need to work out how many sessions there are.
My code so far is the following :
for($i = 0; $i < (count($TimesReq)-1); $i++){
$Done = false;
if($odd = $i % 2 )
{
//ODD WeekComp
if(($TimesReq[$i] != $TimesReq[$i + 2])&&($TimesReq[$i + 2] != $TimesReq[$i + 4])){
$WeeksNotSame = true;
}
}
else
{
//Even TimeComp
if(($TimesReq[$i] != ($TimesReq[$i + 2] - 1))&& ($TimesReq[$i + 2] != ($TimesReq[$i + 4] - 1)))
$TimesNotSame = true;
}
if($TimesNotSame == true && $Done == false){
$HowMany++;
$Done = true;
}
if($WeeksNotSame == true && $Done == false){
$HowMany++;
$Done = true;
}
$TimesNotSame = false;
$WeeksNotSame = false;
}
However this isn't working perfectly. for example it does not work if you have a single session and then a break and then a double session. It is counting this as one session.
This is, probably as you guessed, a coursework problem, but this is not a question out of a textbook, it is part of a timetabling system I am implementing and is required to get it working. So please don't think i'm just copy and pasting my homework to you guys!
Thank you so much!
New Code being used :
if (count($TimesReq) % 2 !== 0) {
//throw new InvalidArgumentException();
}
for ($i = 0; $i < count($TimesReq); $i += 2) {
$time = $TimesReq[$i];
$week = $TimesReq[$i + 1];
if (!isset($TimesReq[$i - 2])) {
// First element has to be a new session
$sessions += 1;
$StartTime[] = $TimesReq[$i];
$Days[] = $TimesReq[$i + 1];
continue;
}
$lastTime = $TimesReq[$i - 2];
$lastWeek = $TimesReq[$i - 1];
$sameWeek = ($week === $lastWeek);
$adjacentTime = ($time - $lastTime === 1);
if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
if(!$sameWeek){//Time
$Days[] = $TimesReq[$i + 1];
$StartTime[] = $TimesReq[$i];
$looking = true;
}
if($sameWeek && !$adjacentTime){
}
if($looking && !$adjacentTime){
$EndTime[] = $TimesReq[$i];
$looking = false;
}
//Week
$sessions += 1;
}
}
If you want a single total number of sessions represented in the data, where each session is separated by a space (either a non-contiguous time, or a separate day). I think this function will get you your result:
function countSessions($data)
{
if (count($data) % 2 !== 0) throw new InvalidArgumentException();
$sessions = 0;
for ($i = 0; $i < count($data); $i += 2) {
$time = $data[$i];
$week = $data[$i + 1];
if (!isset($data[$i - 2])) {
// First element has to be a new session
$sessions += 1;
continue;
}
$lastTime = $data[$i - 2];
$lastWeek = $data[$i - 1];
$sameWeek = ($week === $lastWeek);
$adjacentTime = ($time - $lastTime === 1);
if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
$sessions += 1;
}
}
return $sessions;
}
$totalSessions = countSessions(array(
9, 1,
10, 1,
));
This of course assumes the data is sorted. If it is not, you will need to sort it first. Here is an alternate implementation that includes support for unsorted data.
function countSessions($data)
{
if (count($data) % 2 !== 0) throw new InvalidArgumentException();
$slots = array();
foreach ($data as $i => $value) {
if ($i % 2 === 0) $slots[$i / 2]['time'] = $value;
else $slots[$i / 2]['week'] = $value;
}
usort($slots, function($a, $b) {
if ($a['week'] == $b['week']) {
if ($a['time'] == $b['time']) return 0;
return ($a['time'] < $b['time']) ? -1 : 1;
} else {
return ($a['week'] < $b['week']) ? -1 : 1;
}
});
$sessions = 0;
for ($i = 0; $i < count($slots); $i++) {
if (!isset($slots[$i - 1])) { // First element has to be a new session
$sessions += 1;
continue;
}
$sameWeek = ($slots[$i - 1]['week'] === $slots[$i]['week']);
$adjacentTime = ($slots[$i]['time'] - $slots[$i - 1]['time'] === 1);
if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
$sessions += 1;
}
}
return $sessions;
}
Here is my little attempt at solving your problem. Hopefully I understand what you want:
$TimesReq = array(9,4,11,4,13,4,8,4,7,2,12,4,16,4,18,4,20,4,17,4);
// First just create weeks with all times lumped together
$weeks = array();
for($tri=0; $tri<count($TimesReq); $tri+=2){
$time = $TimesReq[$tri];
$week = $TimesReq[$tri+1];
$match_found = false;
foreach($weeks as $wi=>&$w){
if($wi==$week){
$w[0] = array_merge($w[0], array($time));
$match_found = true;
break;
}
}
if(!$match_found) $weeks[$week][] = array($time);
}
// Now order the times in the sessions in the weeks
foreach($weeks as &$w){
foreach($w as &$s) sort($s);
}
// Now break up sessions by gaps/breaks
$breaking = true;
while($breaking){
$breaking = false;
foreach($weeks as &$w){
foreach($w as &$s){
foreach($s as $ti=>&$t){
if($ti>0 && $t!=$s[$ti-1]+1){
// A break was found
$new_times = array_splice($s, $ti);
$s = array_splice($s, 0, $ti);
$w[] = $new_times;
$breaking = true;
break;
}
}
}
}
}
//print_r($weeks);
foreach($weeks as $wi=>&$w){
echo 'Week '.$wi.' has '.count($w)." session(s):\n";
foreach($w as $si=>&$s)
{
echo "\tSession ".($si+1).":\n";
echo "\t\tStart Time: ".$s[0]."\n";
echo "\t\tEnd Time: ".((int)($s[count($s)-1])+1)."\n";
}
}
Given $TimesReq = array(9,4,11,4,13,4,8,4,7,2,12,4,16,4,18,4,20,4,17,4); the code will produce as output:
Week 4 has 4 session(s):
Session 1:
Start Time: 8
End Time: 10
Session 2:
Start Time: 11
End Time: 14
Session 3:
Start Time: 16
End Time: 19
Session 4:
Start Time: 20
End Time: 21
Week 2 has 1 session(s):
Session 1:
Start Time: 7
End Time: 8
Hope that helps.

Categories