Where can I change the calendar start date to Sunday from Monday - date

I am trying to make this calendar start on Sunday instead of Monday. I've looked through all the help files I can Google on this topic & cannot find any that have any lines of code that I can reference. Am I missing some? Or can some one point me to the point I need to change in order to make it start on Sunday instead of Monday?
<?php
/**
* Copyright (C) 2006-2014 Center Stage Software
* 1191 Luxton Street, Seaside, California 93955
* All Rights Reserved
*/
// ensure that this file is called up by another file
defined('_VALID_ACCESS') or die('Direct access of this file is prohibited.');
/**
* renderEventCalendar - Creates a calendar display of events for one month.
*
* #global (Array) $cstage
*/
function renderEventCalendar() {
global $cstage, $path;
// Result variable.
$calendar = '';
// Calculate first and last day of the month.
$firstDayTimestamp = strtotime($_SESSION[SYS_USER]['search']['Month'].'-01 07:00:00');
list($year,$month) = explode("-",$_SESSION[SYS_USER]['search']['Month']);
if( $month == 12 ) {
$month = 00;
$year += 1;
}
$lastDayTimestamp = strtotime("$year-".($month+1).'-01 08:00:00') - 86400; // minus 24 hours and add 1-day-light-savings-hour
// TODO: Remove the following calendar debugging code.
// print "<h1>first=$firstDayTimestamp; last=$lastDayTimestamp</h1>";
// Build query to retrieve entries for that month.
$aryEvents = array();
$extraSqlFields = '';
$extraSqlJoins = '';
$extraSqlConditions = '';
if( !empty($_SESSION[SYS_USER]['domain']) ) {
$extraSqlJoins .= 'LEFT JOIN "domainlist" AS "domain2" ON "domain2"."master_id" = "master"."master_id" ';
$extraSqlConditions .= sprintf('AND "domain2"."domain"=\'%s\' '
,$_SESSION[SYS_USER]['domain']
);
}
if( !empty($_SESSION[SYS_USER]['search']['Theatre']) ) {
$extraSqlConditions .= sprintf('AND "master"."theatre"=\'%s\' '
,$_SESSION[SYS_USER]['search']['Theatre']
);
}
if( !empty($_SESSION[SYS_USER]['search']['Location']) ) {
$extraSqlConditions .= sprintf('AND "master"."location"=\'%s\' '
,$_SESSION[SYS_USER]['search']['Location']
);
}
if( !empty($_SESSION[SYS_USER]['search']['EventType']) ) {
$extraSqlConditions .= sprintf('AND "master"."event_types_id"=\'%s\' '
,$_SESSION[SYS_USER]['search']['EventType']
);
}
if( !empty($_SESSION[SYS_USER]['search']['Month']) ) {
$extraSqlConditions .= sprintf('AND "shows"."show_date" >= \'%s\' AND "shows"."show_date" <= \'%s\' '
,date('Y-m-d',$firstDayTimestamp)
,date('Y-m-d',$lastDayTimestamp)
);
}
if( $cstage['rttEnable'] ) {
$extraSqlFields .= ', "rtt"."label" AS "rtt_label"';
$extraSqlJoins .= 'LEFT JOIN "rtt" ON "master"."rtt_id" = "rtt"."rtt_id" ';
} else {
$extraSqlConditions .= 'AND "master"."rtt_id"=0 ';
}
if( $cstage['enableEventPriceListTest'] ) {
$aryGroups = array($cstage['tixDomain']);
if( !empty($_SESSION[SYS_USER]['info']) && count($_SESSION[SYS_USER]['info']['groupDomains']) > 0 ) {
$aryGroups = array_merge($aryGroups, $_SESSION[SYS_USER]['info']['groupDomains']);
}
$extraSqlFields .= ', GROUP_CONCAT(DISTINCT "prices"."prices_id" SEPARATOR \';\') AS "priceidlist"';
$extraSqlFields .= ', GROUP_CONCAT(DISTINCT CONCAT("price_category"."label",\' \', "prices"."printed_as") SEPARATOR \', \') AS "pricelist"';
$extraSqlJoins .= ' INNER JOIN "price_category" ON "price_category"."master_id" = "master"."master_id" OR "price_category"."master_id"=0 INNER JOIN "prices" ON "price_category"."price_category_id" = "prices"."price_category_id" INNER JOIN "price_category_domain" ON "price_category_domain"."price_category_id" = "price_category"."price_category_id"';
$extraSqlConditions .= sprintf('AND ("price_category"."master_id"="shows"."master_id" OR "price_category"."master_id"=0) AND ("prices"."shows_id"="shows"."shows_id" OR "prices"."shows_id"=0) AND "prices"."onsale"<=CONCAT("shows"."show_date", \' 00:00:00\') AND ("prices"."offsale">=CONCAT("shows"."show_date", \' 23:59:59\') OR "prices"."offsale" IS NULL) AND "price_category_domain"."domain" IN (\'%s\') '
,implode('\',\'', $aryGroups)
);
}
$now = date('Y-m-d H:i:s');
$query = sprintf('SELECT "master".*, "shows"."show_date", "shows"."show_time", "shows"."onsale" AS "min_onsale"%s FROM "shows" INNER JOIN "master" ON "master"."master_id" = "shows"."master_id" INNER JOIN "domainlist" ON "domainlist"."master_id" = "master"."master_id" %s WHERE "shows"."offsale" >= \'%s\' AND ("shows"."wt_offsale"=\'\' OR "shows"."wt_offsale"=0) AND "domainlist"."domain"=\'%s\' %s GROUP BY "shows"."shows_id"'
,$extraSqlFields
,$extraSqlJoins
,$now
,$cstage['tixDomain']
,$extraSqlConditions
);
$keyEventCalendar = 'eventCalendar_'.$cstage['dbDatabase'].sha1($query);
if( !empty($cstage['cache']) ) {
// Is the record cached to save on slow database connections?
$calendar = $cstage['cache']->load($keyEventCalendar);
}
if( !$calendar ) {
$result = $cstage['pdo']->query( $query );
while( $show = $result->fetch( PDO::FETCH_ASSOC ) ) {
$show['domainlist'] = $cstage['tixDomain'].';'.$_SESSION[SYS_USER]['domain'];
$show['comingSoon'] = false;
if( $now < $show['min_onsale'] ) {
$show['comingSoon'] = true;
}
if( $show['rtt_id'] > 0 ) {
$show['master_id'] = trim($show['rtt_label']).trim($show[]);
if( !$cstage['rttEnable'] ) {
$show = array();
}
}
$path->inc_once('_includes/libf_okEvent.php');
if( okEvent($show) ) {
// Save data into array for usage later
$extraSort = $cstage['useEventSortOrder'] === true ? sprintf('%05d',$show['sortorder']) : '';
$aryEvents[$show['show_date']][date('H:i',strtotime($show['show_time'])).$extraSort.$show['showname'].$show['master_id']] = $show;
}
}
// Start rendering calendar.
$calendar = "\r\n".'<table class="EventCalendar ui-widget-content ui-corner-all"><thead '._HEAD.'><tr><th colspan="7">'.lang('Calendar of events for').' '.date('Y F',strtotime($_SESSION[SYS_USER]['search']['Month'].'-02')).'</th></tr>'
. "\r\n".'<tr><th>'.lang('Sunday').'</th><th>'.lang('Monday').'</th><th>'.lang('Tuesday').'</th><th>'.lang('Wednesday').'</th><th>'.lang('Thursday').'</th><th>'.lang('Friday').'</th><th>'.lang('Saturday').'</th></tr></thead>'
. '<tbody>';
$dayOfWeekPadding = date('w',$firstDayTimestamp);
if( $dayOfWeekPadding == 0 ) {
$dayOfWeekPadding = 6;
} else {
$dayOfWeekPadding -= 0;
}
if( $dayOfWeekPadding > 0 ) {
$calendar .= "\r\n<tr>";
}
for($day=7; $day < $dayOfWeekPadding; $day++) {
$calendar .= "\r\n<td class='EventCalendar_EmptyDate'> </td>";
}
$timeStamp = $firstDayTimestamp;
while( $timeStamp <= $lastDayTimestamp ) {
if( date('w',$timeStamp) == 0 ) {
$calendar .= "\r\n<tr>";
}
$dateKey = date('Y-m-d',$timeStamp);
$calendar .= "\r\n".'<td class="EventCalendar_Date" id="date'.$dateKey.'" onmouseover="tbl_colorchange(\'date'.$dateKey.'\', \'highlight\');" onmouseout="tbl_colorchange(\'date'.$dateKey.'\', \'normal\');"><span>'.date('d',$timeStamp).'</span>';
if( !empty($aryEvents[$dateKey]) ) {
ksort($aryEvents[$dateKey]);
$count = 0;
foreach( $aryEvents[$dateKey] as $key=>$show ) {
$cssClass = $count++ % 2 ? _ODD : _EVEN;
$rttUrl = ( $show['rtt_id'] > 0 ) ? '&rtt='.$show['rtt_id'] : '';
$strPriceList = (empty($show['pricelist']) ? '' : ' title="'.lang('Prices available').': '.$show['pricelist'].'"');
$calendar .= "\r\n<div {$cssClass}{$strPriceList}><a href='event-details.php?e={$show['master_id']}&date={$dateKey}{$rttUrl}'>{$show['']} # {$show['show_time']}</a></div>";
}
}
$calendar .= '</td>';
if( date('w',$timeStamp) == 0 ) {
$calendar .= "\r\n</tr>";
}
$timeStamp += 86400; // Get the next day (in seconds).
}
$dayOfWeekPadding = date('w',$timeStamp);
if( $dayOfWeekPadding == 0 ) {
$dayOfWeekPadding = 6;
} else if( $dayOfWeekPadding == 0 ) {
$dayOfWeekPadding = 0; // do not have an empty week of padding
} else {
$dayOfWeekPadding = 6 - $dayOfWeekPadding;
}
for($day=0; $day < $dayOfWeekPadding; $day++) {
$calendar .= "\r\n<td class='EventCalendar_EmptyDate'> </td>";
}
if( $dayOfWeekPadding > 0 ) {
$calendar .= "\r\n</tr>";
}
$calendar .= "\r\n</tbody></table>";
if( !empty($cstage['cache']) ) {
$cstage['cache']->save($calendar, $keyEventCalendar);
}
}
return $calendar;
}

Related

Remove duplicated date in Woocommerce product page

I'm using "Woo estimated shipping date" plugin to sell pre-order products with estimated shipping date but the pluging show the message plus the date, and I need to show only the message not the "06/04/2022:
That is given by the product page editor option in "Estimated Delivery Time in Days":
Here's the plugin code:
<?php
namespace Saimon\WCESD\Views;
use Saimon\WCESD\Date_Calculator;
use Saimon\WCESD\Helper;
defined( 'ABSPATH' ) || exit;
class Single_Product {
public function __construct() {
$this->hooks();
}
private function hooks() {
add_action( 'woocommerce_after_add_to_cart_form', [ self::class, 'show_date' ] );
}
public static function show_date() {
if ( 'yes' !== Helper::get_option( 'wc_esd_date_enable' ) ) {
return;
}
$wc_esd_date = Helper::get_option( 'wc_esd_date' );
$wc_esd_date = $wc_esd_date ? $wc_esd_date : 5;
$wc_esd_date_message = Helper::get_option( 'wc_esd_date_message' );
$wc_esd_date_message = $wc_esd_date_message ? $wc_esd_date_message : __( 'Estimated Delivery Date', 'wcesd' );
$today = strtotime( current_time( 'mysql' ) );
$to_date = '';
if ( Helper::is_weekend_excluded() ) {
$date = ( new Date_Calculator( $today, $wc_esd_date ) )->get_date();
if ( Helper::is_date_range_enabled() ) {
$wc_esd_date = $wc_esd_date + Helper::get_date_range_gap();
$to_date = ( new Date_Calculator( $today, $wc_esd_date ) )->get_date();
}
} else {
$date = ( new Date_Calculator( $today, $wc_esd_date, false ) )->get_date();
if ( Helper::is_date_range_enabled() ) {
$wc_esd_date = $wc_esd_date + Helper::get_date_range_gap();
$to_date = ( new Date_Calculator( $today, $wc_esd_date ) )->get_date();
}
}
if ( ! empty( $to_date ) ) {
$message = $wc_esd_date_message . ' ' . Helper::display_date( $date ) . ' - ' . Helper::display_date( $to_date );
} else {
$message = $wc_esd_date_message . ' ' . Helper::display_date( $date );
}
$html = '<div class="wesd-box">';
$html .= '<strong class="shipper-date">';
$html .= $message;
$html .= '</strong>';
$html .= '</div>';
echo wp_kses_post(
$html
);
}
}
And I need to achieve this:
The problem is if I don't put a day in the "Estimated delivery time in days" field, the system use a default 5 days time and put the date again (06/04/2022) again.
Any idea how to achieve this guys? Thank you very much.

How to debug a script that will kill by system when await multi Promise?

Answer "the script used too much memory".
I start a few start to read text file line by line, and convert the text to blob, then send it to socket:
use experimental :pack;
sub heart-msg(Str $msg, Str $device-id --> Blob) {
my $heart-msg = $msg.substr(0, $msg.chars - 8);
my $header-buf = pack("H*", $heart-msg);
my $device-id-buf = pack("L*", $device-id);
$header-buf ~ $device-id-buf
}
sub deal-message(Str $msg, Str $device-id --> Blob) {
my $now = DateTime.now();
my $year = $now.year;
my $month = $now.month;
my $day = $now.day;
my $hour = $now.hour;
my $minute = $now.minute;
my $second = $now.second.Int;
my $check-number = 0;
my $head-msg = $msg.substr(0,4);
my $device-id-length = $msg.substr(4,2);
my $pf-code = $msg.substr(14, 2);
my $msg-length = $msg.substr(16, 4);
my $rel-time = $msg.substr(20, 4);
my $end-msg = $msg.substr(38, $msg.chars - 38 - 2);
my $head-msg-buf = pack("H*", $head-msg);
my $device-id-len-buf = pack("H*", $device-id-length);
my $device-id-buf = pack("L*", $device-id);
my $pf-code-buf = pack("H*", $pf-code);
my $msg-length-buf = pack("H*", $msg-length);
my $rel-time-buf = pack("H*", $rel-time);
my $year-buf = pack("S*", $year);
my $month-buf = pack("C*", $month);
my $day-buf = pack("C*", $day);
my $hour-buf = pack("C*", $hour);
my $minute-buf = pack("C*", $minute);
my $second-buf = pack("C*", $second);
my $end-msg-buf = pack("H*", $end-msg);
my #bufs = [$device-id-len-buf, $device-id-buf, $pf-code-buf, $msg-length-buf,
$rel-time-buf, $year-buf, $month-buf, $day-buf, $hour-buf, $minute-buf, $second-buf,
$end-msg-buf];
for #bufs -> $byte {
my $byte_sum = [+] $byte.contents;
$check-number += $byte_sum;
}
$check-number = $check-number % 256;
my $checked-msg-buf = pack("C*", $check-number);
[~] $head-msg-buf, |#bufs, $checked-msg-buf
}
sub deal-data(Str $msg, Str $device-id --> Blob) {
my $header = $msg.substr(0, 4);
given $header {
when '6868' {
return deal-message($msg, $device-id);
}
when '7468' {
return heart-msg($msg, $device-id);
}
default { return Buf.new }
}
}
sub MAIN(
:$host = '10.0.0.77',
Int() :$port = 4444,
Rat() :$interval = 0.001,
:$file = 'data.txt',
Int() :$device-num = 169) {
my $conn = IO::Socket::INET.new(:$host, :$port);
my #devices = "modelId.txt".IO.lines;
my #promises = gather for #devices[159..$device-num] -> $device-id {
take start {
my $f = lazy $file.IO.lines;
my $iterator = $f.iterator;
react {
whenever Supply.interval($interval) {
try {
my $line := $iterator.pull-one;
if $line =:= IterationEnd {
done;
} else {
my $blob = deal-data($line.chomp.split(/\s+/).tail, $device-id.Str);
#say $blob;
$conn.write($blob);
}
}
QUIT {
$conn.close;
say 'connecting closed';
default {
say .^name, '→ ', .Str;
say "handled in line $?LINE";
}
}
LAST {
say "Connection closed";
done;
}
}
}
}
}
await #promises;
}
When running on CentOS 7.4(12 core, 32G RAM), after a few seconds, my script was killed by system. When running on Win11(12 core, 16G RAM), it's OK.
So how to debug this script?

SuiteCRM add custom where condition in view.popup.php?

How to add custom where condition in view.popup.php ?
Someone suggests me to add custom where condition in view.list.php but my problem is, view.popup.php not calling the view.list.php so there is no meaning to add a custom condition in view.list.php.
Or is this possible to link custom view.popup.php to custom view.list.php ?
Following is long code because most of the code is the default and you will need to change where and select parts according to your need. I have used "Meeting" module for ilustration.
File path: custom\modules\\CustomMeeting.php
<?php
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
require_once("modules/Meetings/Meeting.php");
class CustomMeeting extends Meeting
{
/**
* * constructor
* */
public function __construct()
{
parent::Meeting();
}
/**
* Return the list query used by the list views and export button. Next generation of create_new_list_query function.
*
* Override this function to return a custom query.
*
* #param string $order_by custom order by clause
* #param string $where custom where clause
* #param array $filter Optioanal
* #param array $params Optional *
* #param int $show_deleted Optional, default 0, show deleted records is set to 1.
* #param string $join_type
* #param boolean $return_array Optional, default false, response as array
* #param object $parentbean creating a subquery for this bean.
* #param boolean $singleSelect Optional, default false.
* #return String select query string, optionally an array value will be returned if $return_array= true.
*/
function create_new_list_query($order_by, $where, $filter = array(), $params = array(), $show_deleted = 0, $join_type = '', $return_array = false, $parentbean = null, $singleSelect = false, $ifListForExport = false)
{
$GLOBALS['log']->fatal(print_r($_REQUEST, 1));
global $beanFiles, $beanList;
$selectedFields = array();
$secondarySelectedFields = array();
$ret_array = array();
$distinct = '';
if ($this->bean_implements('ACL') && ACLController::requireOwner($this->module_dir, 'list')) {
global $current_user;
$owner_where = $this->getOwnerWhere($current_user->id);
if (empty($where)) {
$where = $owner_where;
} else {
$where .= ' AND ' . $owner_where;
}
}
/* BEGIN - SECURITY GROUPS */
global $current_user, $sugar_config;
if ($this->module_dir == 'Users' && !is_admin($current_user) && isset($sugar_config['securitysuite_filter_user_list']) && $sugar_config['securitysuite_filter_user_list'] == true
) {
require_once('modules/SecurityGroups/SecurityGroup.php');
global $current_user;
$group_where = SecurityGroup::getGroupUsersWhere($current_user->id);
//$group_where = "user_name = 'admin'";
if (empty($where)) {
$where = " (" . $group_where . ") ";
} else {
$where .= " AND (" . $group_where . ") ";
}
} else
if ($this->bean_implements('ACL') && ACLController::requireSecurityGroup($this->module_dir, 'list')) {
require_once('modules/SecurityGroups/SecurityGroup.php');
global $current_user;
$owner_where = $this->getOwnerWhere($current_user->id);
$group_where = SecurityGroup::getGroupWhere($this->table_name, $this->module_dir, $current_user->id);
if (!empty($owner_where)) {
if (empty($where)) {
$where = " (" . $owner_where . " or " . $group_where . ") ";
} else {
$where .= " AND (" . $owner_where . " or " . $group_where . ") ";
}
} else {
$where .= ' AND ' . $group_where;
}
}
/* END - SECURITY GROUPS */
if (!empty($params['distinct'])) {
$distinct = ' DISTINCT ';
}
if (empty($filter)) {
$ret_array['select'] = " SELECT $distinct $this->table_name.* ";
} else {
$ret_array['select'] = " SELECT $distinct $this->table_name.id ";
}
$ret_array['from'] = " FROM $this->table_name ";
$ret_array['from_min'] = $ret_array['from'];
$ret_array['secondary_from'] = $ret_array['from'];
$ret_array['where'] = '';
$ret_array['order_by'] = '';
//secondary selects are selects that need to be run after the primary query to retrieve additional info on main
if ($singleSelect) {
$ret_array['secondary_select'] = & $ret_array['select'];
$ret_array['secondary_from'] = & $ret_array['from'];
} else {
$ret_array['secondary_select'] = '';
}
$custom_join = $this->getCustomJoin(empty($filter) ? true : $filter );
if ((!isset($params['include_custom_fields']) || $params['include_custom_fields'])) {
$ret_array['select'] .= $custom_join['select'];
}
$ret_array['from'] .= $custom_join['join'];
// Bug 52490 - Captivea (Sve) - To be able to add custom fields inside where clause in a subpanel
$ret_array['from_min'] .= $custom_join['join'];
$jtcount = 0;
//LOOP AROUND FOR FIXIN VARDEF ISSUES
require('include/VarDefHandler/listvardefoverride.php');
if (file_exists('custom/include/VarDefHandler/listvardefoverride.php')) {
require('custom/include/VarDefHandler/listvardefoverride.php');
}
$joined_tables = array();
if (!empty($params['joined_tables'])) {
foreach ($params['joined_tables'] as $table) {
$joined_tables[$table] = 1;
}
}
if (!empty($filter)) {
$filterKeys = array_keys($filter);
if (is_numeric($filterKeys[0])) {
$fields = array();
foreach ($filter as $field) {
$field = strtolower($field);
//remove out id field so we don't duplicate it
if ($field == 'id' && !empty($filter)) {
continue;
}
if (isset($this->field_defs[$field])) {
$fields[$field] = $this->field_defs[$field];
} else {
$fields[$field] = array('force_exists' => true);
}
}
} else {
$fields = $filter;
}
} else {
$fields = $this->field_defs;
}
$used_join_key = array();
//walk through the fields and for every relationship field add their relationship_info field
//relationshipfield-aliases are resolved in SugarBean::create_new_list_query through their relationship_info field
$addrelate = array();
foreach ($fields as $field => $value) {
if (isset($this->field_defs[$field]) && isset($this->field_defs[$field]['source']) &&
$this->field_defs[$field]['source'] == 'non-db') {
$addrelatefield = $this->get_relationship_field($field);
if ($addrelatefield)
$addrelate[$addrelatefield] = true;
}
if (!empty($this->field_defs[$field]['id_name'])) {
$addrelate[$this->field_defs[$field]['id_name']] = true;
}
}
$fields = array_merge($addrelate, $fields);
foreach ($fields as $field => $value) {
//alias is used to alias field names
$alias = '';
if (isset($value['alias'])) {
$alias = ' as ' . $value['alias'] . ' ';
}
if (empty($this->field_defs[$field]) || !empty($value['force_blank'])) {
if (!empty($filter) && isset($filter[$field]['force_exists']) && $filter[$field]['force_exists']) {
if (isset($filter[$field]['force_default']))
$ret_array['select'] .= ", {$filter[$field]['force_default']} $field ";
else
//spaces are a fix for length issue problem with unions. The union only returns the maximum number of characters from the first select statement.
$ret_array['select'] .= ", ' ' $field ";
}
continue;
}
else {
$data = $this->field_defs[$field];
}
//ignore fields that are a part of the collection and a field has been removed as a result of
//layout customization.. this happens in subpanel customizations, use case, from the contacts subpanel
//in opportunities module remove the contact_role/opportunity_role field.
if (isset($data['relationship_fields']) and ! empty($data['relationship_fields'])) {
$process_field = false;
foreach ($data['relationship_fields'] as $field_name) {
if (isset($fields[$field_name])) {
$process_field = true;
break;
}
}
if (!$process_field)
continue;
}
if ((!isset($data['source']) || $data['source'] == 'db') && (!empty($alias) || !empty($filter) )) {
$ret_array['select'] .= ", $this->table_name.$field $alias";
$selectedFields["$this->table_name.$field"] = true;
} else if ((!isset($data['source']) || $data['source'] == 'custom_fields') && (!empty($alias) || !empty($filter) )) {
//add this column only if it has NOT already been added to select statement string
$colPos = strpos($ret_array['select'], "$this->table_name" . "_cstm" . ".$field");
if (!$colPos || $colPos < 0) {
$ret_array['select'] .= ", $this->table_name" . "_cstm" . ".$field $alias";
}
$selectedFields["$this->table_name.$field"] = true;
}
if ($data['type'] != 'relate' && isset($data['db_concat_fields'])) {
$ret_array['select'] .= ", " . $this->db->concat($this->table_name, $data['db_concat_fields']) . " as $field";
$selectedFields[$this->db->concat($this->table_name, $data['db_concat_fields'])] = true;
}
//Custom relate field or relate fields built in module builder which have no link field associated.
if ($data['type'] == 'relate' && (isset($data['custom_module']) || isset($data['ext2']))) {
$joinTableAlias = 'jt' . $jtcount;
$relateJoinInfo = $this->custom_fields->getRelateJoin($data, $joinTableAlias, false);
$ret_array['select'] .= $relateJoinInfo['select'];
$ret_array['from'] .= $relateJoinInfo['from'];
//Replace any references to the relationship in the where clause with the new alias
//If the link isn't set, assume that search used the local table for the field
$searchTable = isset($data['link']) ? $relateJoinInfo['rel_table'] : $this->table_name;
$field_name = $relateJoinInfo['rel_table'] . '.' . !empty($data['name']) ? $data['name'] : 'name';
$where = preg_replace('/(^|[\s(])' . $field_name . '/', '${1}' . $relateJoinInfo['name_field'], $where);
$jtcount++;
}
//Parent Field
if ($data['type'] == 'parent') {
//See if we need to join anything by inspecting the where clause
$match = preg_match('/(^|[\s(])parent_(\w+)_(\w+)\.name/', $where, $matches);
if ($match) {
$joinTableAlias = 'jt' . $jtcount;
$joinModule = $matches[2];
$joinTable = $matches[3];
$localTable = $this->table_name;
if (!empty($data['custom_module'])) {
$localTable .= '_cstm';
}
global $beanFiles, $beanList, $module;
require_once($beanFiles[$beanList[$joinModule]]);
$rel_mod = new $beanList[$joinModule]();
$nameField = "$joinTableAlias.name";
if (isset($rel_mod->field_defs['name'])) {
$name_field_def = $rel_mod->field_defs['name'];
if (isset($name_field_def['db_concat_fields'])) {
$nameField = $this->db->concat($joinTableAlias, $name_field_def['db_concat_fields']);
}
}
$ret_array['select'] .= ", $nameField {$data['name']} ";
$ret_array['from'] .= " LEFT JOIN $joinTable $joinTableAlias
ON $localTable.{$data['id_name']} = $joinTableAlias.id";
//Replace any references to the relationship in the where clause with the new alias
$where = preg_replace('/(^|[\s(])parent_' . $joinModule . '_' . $joinTable . '\.name/', '${1}' . $nameField, $where);
$jtcount++;
}
}
if ($this->is_relate_field($field)) {
$this->load_relationship($data['link']);
if (!empty($this->$data['link'])) {
$params = array();
if (empty($join_type)) {
$params['join_type'] = ' LEFT JOIN ';
} else {
$params['join_type'] = $join_type;
}
if (isset($data['join_name'])) {
$params['join_table_alias'] = $data['join_name'];
} else {
$params['join_table_alias'] = 'jt' . $jtcount;
}
if (isset($data['join_link_name'])) {
$params['join_table_link_alias'] = $data['join_link_name'];
} else {
$params['join_table_link_alias'] = 'jtl' . $jtcount;
}
$join_primary = !isset($data['join_primary']) || $data['join_primary'];
$join = $this->$data['link']->getJoin($params, true);
$used_join_key[] = $join['rel_key'];
$rel_module = $this->$data['link']->getRelatedModuleName();
$table_joined = !empty($joined_tables[$params['join_table_alias']]) || (!empty($joined_tables[$params['join_table_link_alias']]) && isset($data['link_type']) && $data['link_type'] == 'relationship_info');
//if rname is set to 'name', and bean files exist, then check if field should be a concatenated name
global $beanFiles, $beanList;
// °3/21/2014 FIX NS-TEAM - Relationship fields could not be displayed in subpanels
//if($data['rname'] && !empty($beanFiles[$beanList[$rel_module]])) {
if (isset($data['rname']) && $data['rname'] == 'name' && !empty($beanFiles[$beanList[$rel_module]])) {
//create an instance of the related bean
require_once($beanFiles[$beanList[$rel_module]]);
$rel_mod = new $beanList[$rel_module]();
//if bean has first and last name fields, then name should be concatenated
if (isset($rel_mod->field_name_map['first_name']) && isset($rel_mod->field_name_map['last_name'])) {
$data['db_concat_fields'] = array(0 => 'first_name', 1 => 'last_name');
}
}
if ($join['type'] == 'many-to-many') {
if (empty($ret_array['secondary_select'])) {
$ret_array['secondary_select'] = " SELECT $this->table_name.id ref_id ";
if (!empty($beanFiles[$beanList[$rel_module]]) && $join_primary) {
require_once($beanFiles[$beanList[$rel_module]]);
$rel_mod = new $beanList[$rel_module]();
if (isset($rel_mod->field_defs['assigned_user_id'])) {
$ret_array['secondary_select'].= " , " . $params['join_table_alias'] . ".assigned_user_id {$field}_owner, '$rel_module' {$field}_mod";
} else {
if (isset($rel_mod->field_defs['created_by'])) {
$ret_array['secondary_select'].= " , " . $params['join_table_alias'] . ".created_by {$field}_owner , '$rel_module' {$field}_mod";
}
}
}
}
if (isset($data['db_concat_fields'])) {
$ret_array['secondary_select'] .= ' , ' . $this->db->concat($params['join_table_alias'], $data['db_concat_fields']) . ' ' . $field;
} else {
if (!isset($data['relationship_fields'])) {
$ret_array['secondary_select'] .= ' , ' . $params['join_table_alias'] . '.' . $data['rname'] . ' ' . $field;
}
}
if (!$singleSelect) {
$ret_array['select'] .= ", ' ' $field ";
}
$count_used = 0;
foreach ($used_join_key as $used_key) {
if ($used_key == $join['rel_key'])
$count_used++;
}
if ($count_used <= 1) {//27416, the $ret_array['secondary_select'] should always generate, regardless the dbtype
// add rel_key only if it was not aready added
if (!$singleSelect) {
$ret_array['select'] .= ", ' ' " . $join['rel_key'] . ' ';
}
$ret_array['secondary_select'] .= ', ' . $params['join_table_link_alias'] . '.' . $join['rel_key'] . ' ' . $join['rel_key'];
}
if (isset($data['relationship_fields'])) {
foreach ($data['relationship_fields'] as $r_name => $alias_name) {
if (!empty($secondarySelectedFields[$alias_name]))
continue;
$ret_array['secondary_select'] .= ', ' . $params['join_table_link_alias'] . '.' . $r_name . ' ' . $alias_name;
$secondarySelectedFields[$alias_name] = true;
}
}
if (!$table_joined) {
$ret_array['secondary_from'] .= ' ' . $join['join'] . ' AND ' . $params['join_table_alias'] . '.deleted=0';
if (isset($data['link_type']) && $data['link_type'] == 'relationship_info' && ($parentbean instanceOf SugarBean)) {
$ret_array['secondary_where'] = $params['join_table_link_alias'] . '.' . $join['rel_key'] . "='" . $parentbean->id . "'";
}
}
} else {
if (isset($data['db_concat_fields'])) {
$ret_array['select'] .= ' , ' . $this->db->concat($params['join_table_alias'], $data['db_concat_fields']) . ' ' . $field;
} else {
$ret_array['select'] .= ' , ' . $params['join_table_alias'] . '.' . $data['rname'] . ' ' . $field;
}
if (isset($data['additionalFields'])) {
foreach ($data['additionalFields'] as $k => $v) {
if (!empty($data['id_name']) && $data['id_name'] == $v && !empty($fields[$data['id_name']])) {
continue;
}
$ret_array['select'] .= ' , ' . $params['join_table_alias'] . '.' . $k . ' ' . $v;
}
}
if (!$table_joined) {
$ret_array['from'] .= ' ' . $join['join'] . ' AND ' . $params['join_table_alias'] . '.deleted=0';
if (!empty($beanList[$rel_module]) && !empty($beanFiles[$beanList[$rel_module]])) {
require_once($beanFiles[$beanList[$rel_module]]);
$rel_mod = new $beanList[$rel_module]();
if (isset($value['target_record_key']) && !empty($filter)) {
$selectedFields[$this->table_name . '.' . $value['target_record_key']] = true;
$ret_array['select'] .= " , $this->table_name.{$value['target_record_key']} ";
}
if (isset($rel_mod->field_defs['assigned_user_id'])) {
$ret_array['select'] .= ' , ' . $params['join_table_alias'] . '.assigned_user_id ' . $field . '_owner';
} else {
$ret_array['select'] .= ' , ' . $params['join_table_alias'] . '.created_by ' . $field . '_owner';
}
$ret_array['select'] .= " , '" . $rel_module . "' " . $field . '_mod';
}
}
}
// To fix SOAP stuff where we are trying to retrieve all the accounts data where accounts.id = ..
// and this code changes accounts to jt4 as there is a self join with the accounts table.
//Martin fix #27494
if (isset($data['db_concat_fields'])) {
$buildWhere = false;
if (in_array('first_name', $data['db_concat_fields']) && in_array('last_name', $data['db_concat_fields'])) {
$exp = '/\(\s*?' . $data['name'] . '.*?\%\'\s*?\)/';
if (preg_match($exp, $where, $matches)) {
$search_expression = $matches[0];
//Create three search conditions - first + last, first, last
$first_name_search = str_replace($data['name'], $params['join_table_alias'] . '.first_name', $search_expression);
$last_name_search = str_replace($data['name'], $params['join_table_alias'] . '.last_name', $search_expression);
$full_name_search = str_replace($data['name'], $this->db->concat($params['join_table_alias'], $data['db_concat_fields']), $search_expression);
$buildWhere = true;
$where = str_replace($search_expression, '(' . $full_name_search . ' OR ' . $first_name_search . ' OR ' . $last_name_search . ')', $where);
}
}
if (!$buildWhere) {
$db_field = $this->db->concat($params['join_table_alias'], $data['db_concat_fields']);
$where = preg_replace('/' . $data['name'] . '/', $db_field, $where);
// For relationship fields replace their alias by the corresponsding link table and r_name
if (isset($data['relationship_fields']))
foreach ($data['relationship_fields'] as $r_name => $alias_name) {
$db_field = $this->db->concat($params['join_table_link_alias'], $r_name);
$where = preg_replace('/' . $alias_name . '/', $db_field, $where);
}
}
} else {
$where = preg_replace('/(^|[\s(])' . $data['name'] . '/', '${1}' . $params['join_table_alias'] . '.' . $data['rname'], $where);
// For relationship fields replace their alias by the corresponsding link table and r_name
if (isset($data['relationship_fields']))
foreach ($data['relationship_fields'] as $r_name => $alias_name)
$where = preg_replace('/(^|[\s(])' . $alias_name . '/', '${1}' . $params['join_table_link_alias'] . '.' . $r_name, $where);
}
if (!$table_joined) {
$joined_tables[$params['join_table_alias']] = 1;
$joined_tables[$params['join_table_link_alias']] = 1;
}
$jtcount++;
}
}
}
if (!empty($filter)) {
if (isset($this->field_defs['assigned_user_id']) && empty($selectedFields[$this->table_name . '.assigned_user_id'])) {
$ret_array['select'] .= ", $this->table_name.assigned_user_id ";
} else if (isset($this->field_defs['created_by']) && empty($selectedFields[$this->table_name . '.created_by'])) {
$ret_array['select'] .= ", $this->table_name.created_by ";
}
if (isset($this->field_defs['system_id']) && empty($selectedFields[$this->table_name . '.system_id'])) {
$ret_array['select'] .= ", $this->table_name.system_id ";
}
}
$where_auto = '1=1';
if ($show_deleted == 0) {
$where_auto = "$this->table_name.deleted=0";
} else if ($show_deleted == 1) {
$where_auto = "$this->table_name.deleted=1";
}
if ($where != "")
$ret_array['where'] = " where ($where) AND $where_auto";
else
$ret_array['where'] = " where $where_auto";
//make call to process the order by clause
$order_by = $this->process_order_by($order_by);
if (!empty($order_by)) {
$ret_array['order_by'] = " ORDER BY " . $order_by;
}
if ($singleSelect) {
unset($ret_array['secondary_where']);
unset($ret_array['secondary_from']);
unset($ret_array['secondary_select']);
}
if (<your check goes here>) {
$ret_array['from'] = $ret_array['from'] . " your sql to fetch data ";
$ret_array['where'] .=" AND meetings.id NOT IN (some condition)";
}
if ($return_array) {
return $ret_array;
}
return $ret_array['select'] . $ret_array['from'] . $ret_array['where'] . $ret_array['order_by'];
}
}

How can I speed up this nested foreach loop in Perl?

I have a Perl script that compares two sets of data loaded into two arrays, and I'm trying to make the comparison more efficient. Currently the code is as follows:
foreach(#{FILE_DATA}) {
if((++$file_current_linenum % 200) == 0) {
$progress = int($file_current_linenum / $file_total_lines * 10000) / 100;
logg("Processed $file_current_linenum file rows, $progress%, $mismatches mismatches.");
}
$file_current_line = $_;
$match_found = 0;
foreach(#{DB_DATA}) {
$db_current_line = $_;
if($file_current_line->{"channel"} == $db_current_line->{"channel"} ) {
if ($file_current_line->{"checksum"} == $db_current_line->{"checksum"} &&
$file_current_line->{"time"} > ($db_current_line->{"date_part"} - $TIME_MATCH_TOLERANCE) &&
$file_current_line->{"time"} < ($db_current_line->{"date_part"} + $TIME_MATCH_TOLERANCE) ){
$match_found = 1;
last; # break;
}
}
}
if($match_found != 1) {
push(#results, $file_current_line);
$mismatches++;
}
}
My first thought would be to remove matches from both arrays to reduce the pool size, would that affect the iterators position?
Both sets of data can have up to a couple of million elements and the comparison can take a few hours to complete.
Your solution is O(DB * FILE).
The following is O(DB + FILE) if and only if there never more than a few lines with the same channel and checksum:
my %DB_DATA;
for my $db_line (#DB_DATA) {
push #{ $DB_DATA{ $db_line->{channel} }{ $db_line->{checksum} } }, $db_line;
}
for my $file_line_idx (0..$#FILE_DATA) {
my $file_line = $FILE_DATA[$file_line_idx];
my $found = 0;
if (my $r1 = $DB_DATA{ $file_line->{channel} } ) {
if (my $r2 = $r1->{ $file_line->{checksum} } ) {
my $file_time = $file_line->{time};
for my $db_line (#$r2) {
my $db_time = $db_line->{date_part};
if (abs($file_time - $db_time) < $TIME_MATCH_TOLERANCE) {
$found = 1;
last;
}
}
}
}
push #mismatches, $file_line if !$found;
if ((($file_line_idx+1) % 200) == 0) {
logg(sprintf("Processed %d file rows, %d%, %d mismatches.",
$file_line_idx+1,
int(($file_line_idx+1)/#FILE_DATA) * 100,
0+#mismatches,
));
}
}
The following is O(DB + FILE) even if there are many lines with the same channel and checksum, but uses a lot of memory if $TIME_MATCH_TOLERANCE is big:
my %DB_DATA;
for my $db_line (#DB_DATA) {
for my $db_time (
$db_line->{date_part} - $TIME_MATCH_TOLERANCE + 1
..
$db_line->{date_part} + $TIME_MATCH_TOLERANCE - 1
) {
++$DB_DATA{ $db_line->{channel} }{ $db_line->{checksum} }{$db_time};
}
}
for my $file_line_idx (0..$#FILE_DATA) {
my $file_line = $FILE_DATA[$file_line_idx];
my $found = 0;
if (my $r1 = $DB_DATA{ $file_line->{channel} } ) {
if (my $r2 = $r1->{ $file_line->{checksum} } ) {
if ($r2->{ $file_line->{time} } {
$found = 1;
last;
}
}
}
push #mismatches, $file_line if !$found;
if ((($file_line_idx+1) % 200) == 0) {
logg(sprintf("Processed %d file rows, %d%, %d mismatches.",
$file_line_idx+1,
int(($file_line_idx+1)/#FILE_DATA) * 100,
0+#mismatches,
));
}
}
Note: Assumes the timestamps are integers. If they're not, convert them to integers before using them as keys.
The following is O((DB + FILE) log DB) [ which is very close to O(DB + FILE) ] even if there are many lines with the same channel and checksum, and uses minimal memory:
sub binsearch(&\#) {
my ($compare, $array) = #_;
my $i = 0;
my $j = $#$array;
return 0 if $j == -1;
while (1) {
my $k = int(($i+$j)/2);
for ($array->[$k]) {
my $cmp = $compare->()
or return 1;
if ($cmp < 0) {
$j = $k-1;
return 0 if $i > $j;
} else {
$i = $k+1;
return 0 if $i > $j;
}
}
}
}
my %DB_DATA;
for my $db_line (#DB_DATA) {
push #{ $DB_DATA{ $db_line->{channel} }{ $db_line->{checksum} } }, $db_line;
}
for my $r1 (values(%DB_DATA)) {
for my $r2 (values(%$r1)) {
#$r2 = sort { $a->{date_part} <=> $b->{date_part} } #$r2;
}
}
for my $file_line_idx (0..$#FILE_DATA) {
my $file_line = $FILE_DATA[$file_line_idx];
my $found = 0;
if (my $r1 = $DB_DATA{ $file_line->{channel} } ) {
if (my $r2 = $r1->{ $file_line->{checksum} } ) {
my $file_time = $file_line->{time};
my $min_db_time = $file_time - $TIME_MATCH_TOLERANCE;
my $max_db_time = $file_time + $TIME_MATCH_TOLERANCE;
if ( binsearch {
$_->{date_part} >= $max_db_time ? -1
: $_->{date_part} <= $min_db_time ? +1
: 0
} #$r2 ) {
$found = 1;
last;
}
}
}
push #mismatches, $file_line if !$found;
if ((($file_line_idx+1) % 200) == 0) {
logg(sprintf("Processed %d file rows, %d%, %d mismatches.",
$file_line_idx+1,
int(($file_line_idx+1)/#FILE_DATA) * 100,
0+#mismatches,
));
}
}
You could probably reduce the time significantly by pre-building a hash from DB_DATA, using the concatenation of the "channel" and "checksum" values as the key, and each value being a list of all of the DB_DATA entries with that channel and checksum. That way, for each FILE_DATA entry, you only need to check that list.
If there are a lot of entries with a given channel and checksum, you try to improve even more by sorting them by date_part, and then trying to binary search to find a valid entry.
If there are very few entries with a given channel and checksum, this should reduce your run time by a factor of a million or so, since it reduces the run time from O($#FILE_DATA * $#DB_DATA) to O($#FILE_DATA + $#DB_DATA).

Optimize perl code

I have to write to check array ref for more than 3 params. If the value is coming from an array then I have written a foreach loop for that, then chop that and assign to a new variable after concatenating pipe.
Code
if ( defined $args->{hotel} ) {
if ( ref( $args->{hotel} ) eq "ARRAY" ) {
foreach my $hotel ( #{ $args->{hotel} } ) {
$hotel .= $hotel . "|";
}
chop($hotel);
$args->{hotel_name} = $hotel;
} else {
$args->{hotel_name} = $args->{hotel};
}
} else {
$args->{hotel_name} = $hotel;
}
if ( defined $args->{country} ) {
if ( ref( $args->{country} ) eq "ARRAY" ) {
foreach my $country_name ( #{ $args->{country} } ) {
$country_name .= $country_name . "|";
}
chop($country_name);
$args->{country_name} = $country_name;
} else {
$args->{country_name} = $args->{country};
}
} else {
$args->{country_name} = $country_name;
}
if ( defined $args->{city} ) {
if ( ref( $args->{city} ) eq "ARRAY" ) {
foreach my $city ( #{ $args->{city} } ) {
$city .= $city . "|";
}
chop($city);
$args->{city_name} = $city;
} else {
$args->{city_name} = $args->{city};
}
} else {
$args->{city_name} = $city;
}
I want to write a function for this kind of work so that there will be no repetition of same code. Please help me; how can we do this in Perl?
You can write:
sub convert_to_name ($$) { # ($value, $fallback_name)
my ($value, $fallback_name) = #_;
if (defined $value) {
if (ref($value) eq 'ARRAY') {
return join '|', #$value;
} else {
return "$value";
}
} else {
return $fallback_name;
}
}
$args->{'hotel_name'} = convert_to_name $args->{'hotel'}, $hotel;
$args->{'country_name'} = convert_to_name $args->{'country'}, $country;
$args->{'city_name'} = convert_to_name $args->{'city'}, $city;
There appear to be some potential bugs in your code, the biggest centering around reusing variable names at lower scopes.
However, of course you can add an iteration loop to your code that would remove the need for 3 nearly identical sections. The following does that by creating a intermediate hash data structure to relate field names to values.
Note: I also simplified the code by inverting the logic if your first if statement so that all ifs could be at the same level. Additionally, it makes sense to use a join instead of rolling your own such functionality.
my %hash = (
hotel => $hotel,
country => $country_name,
city => $city,
);
while ( my ( $field, $value ) = each %hash ) {
if ( !defined $args->{$field} ) {
$args->{"${field}_name"} = $value;
} elsif ( ref( $args->{$field} ) eq "ARRAY" ) {
$args->{"${field}_name"} = join '|', #{ $args->{$field} };
} else {
$args->{"${field}_name"} = $args->{$field};
}
}
Also, if you're comfortable with the Conditional operator, this can be reduced further. However, some would consider this too cluttered:
while ( my ( $field, $value ) = each %hash ) {
$args->{"${field}_name"} = !defined $args->{$field}
? $value
: ref( $args->{$field} ) eq "ARRAY"
? join( '|', #{ $args->{$field} } )
: $args->{$field};
}