public function action_index() { // clear buffer helper_ob::clean_all(); // validating do { $options = application::get('flag.numbers.backend.cron.base'); // token if (!empty($options['token']) && request::input('token') != $options['token']) { break; } // ip if (!empty($options['ip']) && !in_array(request::ip(), $options['ip'])) { break; } // get date parts $date_parts = format::now('parts'); print_r($date_parts); echo "GOOD\n"; } while (0); // we need to validate token //$token = request::input('token'); echo "OK\n"; // exit exit; }
/** * Next run datetime * * @param mixed $datetime * @return string */ public function next_run_date($datetime = null) { $result = null; if (empty($datetime)) { $datetime = format::now('unix'); } else { if (!is_numeric($datetime)) { $datetime = strtotime($datetime); } } $parts = format::datetime_parts($datetime); for ($year = $parts['year']; $year <= self::$slot_stats[6]['max']; $year++) { // check if we are in range if (!(in_array('*', $this->parsed_expression['year']) || in_array($year, $this->parsed_expression['year']))) { continue; } for ($month = 1; $month <= 12; $month++) { // check if we are in range if (!(in_array('*', $this->parsed_expression['month']) || in_array($month, $this->parsed_expression['month']))) { continue; } for ($day = 1; $day <= 31; $day++) { // check if we are in range if (!(in_array('*', $this->parsed_expression['day']) || in_array($day, $this->parsed_expression['day']))) { continue; } // check weekday $weekday = date('w', mktime(0, 0, 0, $month, $day, $year)); if (!(in_array('*', $this->parsed_expression['weekday']) || in_array($weekday, $this->parsed_expression['weekday']))) { continue; } // loop through hours for ($hour = 0; $hour <= 23; $hour++) { // check if we are in range if (!(in_array('*', $this->parsed_expression['hour']) || in_array($hour, $this->parsed_expression['hour']))) { continue; } // loop though minutes for ($minute = 0; $minute <= 59; $minute++) { $date = mktime($hour, $minute, 0, $month, $day, $year); if ($date < $datetime) { continue; } else { // check if we are in range if (!(in_array('*', $this->parsed_expression['minute']) || in_array($minute, $this->parsed_expression['minute']))) { continue; } // check the rest $result = format::datetime($date); goto exit1; } } } } } } exit1: return $result; }
/** * see tinyurl::set(); */ public static function set($url, $options = []) { // insert new row into the table $object = new numbers_backend_misc_tinyurl_db_model_tinyurls(); $result = $object->insert(['sm_tinyurl_inserted' => format::now('datetime'), 'sm_tinyurl_url' => $url . '', 'sm_tinyurl_expires' => $options['expires'] ?? null]); if ($result['success']) { $result['data']['id'] = $result['last_insert_id']; $result['data']['hash'] = base_convert($result['last_insert_id'] . '', 10, 36); } else { $result['data'] = []; } array_key_unset($result, ['success', 'error', 'data'], ['preserve' => true]); return $result; }
public function save(&$form) { $model = factory::model($form->options['other']['model']); $save = [$model->column_prefix . 'important' => !empty($form->values['important']) ? 1 : 0, $model->column_prefix . 'comment_value' => $form->values['comment'] . '', $model->column_prefix . 'who_entity_id' => session::get('numbers.entity.em_entity_id'), $model->column_prefix . 'inserted' => format::now('timestamp')]; foreach ($form->options['other']['map'] as $k => $v) { $save[$v] = $form->options['other']['pk'][$k]; } $save_result = $model->save($save, ['ignore_not_set_fields' => true]); if ($save_result['success']) { $form->error('success', 'Comment has been added successfully!'); } else { $form->error('danger', 'Could not add comment!'); } }
/** * Process the lock * @param string $id * @return boolean */ public static function process($id) { $lock_data = lock::exists($id); if ($lock_data !== false) { $minutes = round(abs(strtotime(format::now()) - strtotime($lock_data)) / 60, 2); if ($minutes > 30) { lock::release($id); $lock_data = false; } } // we are ok to proceed if ($lock_data === false) { lock::create($id); return true; } else { return false; } }
/** * Get information * * @param string $ip * @return array */ public function get($ip) { $ip = $ip . ''; // try to get IP information from cache $cache = new cache('db'); $cache_id = 'misc_ip_ipinfo_' . $ip; $data = $cache->get($cache_id); if ($data !== false) { return ['success' => true, 'error' => [], 'data' => $data]; } // if we need to query ipinfo.io $json = file_get_contents("http://ipinfo.io/{$ip}/json"); $data = json_decode($json, true); if (isset($data['country'])) { $temp = explode(',', $data['loc']); $save = ['ip' => $ip, 'date' => format::now('date'), 'country' => $data['country'], 'region' => $data['region'], 'city' => $data['city'], 'postal' => $data['postal'], 'lat' => format::read_floatval($temp[0]), 'lon' => format::read_floatval($temp[1])]; $cache->set($cache_id, $save, ['misc_ip_ipinfo'], 604800); return ['success' => true, 'error' => [], 'data' => $save]; } else { return ['success' => false, 'error' => ['Could not decode IP address!'], 'data' => ['ip' => $ip]]; } }
public function action_index() { $input = request::input(null, true, true); $result = ['success' => false, 'error' => [], 'loggedin' => false, 'expired' => false, 'expires_in' => 0]; if (!empty($input['token']) && !empty($input[session_name()])) { $crypt = new crypt(); $token_data = $crypt->token_validate($input['token'], ['skip_time_validation' => true]); if (!($token_data === false || $token_data['id'] !== 'general')) { // quering database $model = new numbers_backend_session_db_model_sessions(); $db = $model->db_object(); $session_id = $db->escape($input[session_name()]); $expire = format::now('timestamp'); $sql = <<<TTT \t\t\t\t\tSELECT \t\t\t\t\t\tsm_session_expires, \t\t\t\t\t\tsm_session_user_id \t\t\t\t\tFROM {$model->name} \t\t\t\t\tWHERE 1=1 \t\t\t\t\t\tAND sm_session_id = '{$session_id}' \t\t\t\t\t\tAND sm_session_expires >= '{$expire}' TTT; $temp = $db->query($sql); // put values into result $result['expired'] = empty($temp['rows']); $result['loggedin'] = !empty($temp['rows'][0]['sm_session_user_id']); // calculate when session is about to expire if (!empty($temp['rows'])) { $now = format::now('unix'); $expires = strtotime($temp['rows'][0]['sm_session_expires']); $result['expires_in'] = $expires - $now; } $result['success'] = true; } } // rendering layout::render_as($result, 'application/json'); }
/** * Merge data to database * * @param array $data * @param array $options * @param object $form * @return array */ public function merge($data, $options = [], &$form = null) { $result = ['success' => false, 'error' => [], 'warning' => [], 'deleted' => false, 'inserted' => false, 'new_serials' => [], 'options_model' => []]; do { // start transaction $this->primary_model->db_object->begin(); // load data from database $original = []; if (array_key_exists('original', $options)) { $original = $options['original']; } else { // load data from database // assemble primary key $pk = []; $full_pk = true; foreach ($this->data['pk'] as $v) { if (isset($data[$v])) { $pk[$v] = $data[$v]; } else { $full_pk = false; } } // load data if (!empty($pk) && $full_pk) { $original = $this->get(['where' => $pk, 'single_row' => true]); } } // validate optimistic lock if ($this->primary_model->optimistic_lock && !empty($original)) { if (($data[$this->primary_model->optimistic_lock_column] ?? '') !== $original[$this->primary_model->optimistic_lock_column]) { $result['error'][] = object_content_messages::optimistic_lock; break; } } // we need to validate options_model if (!empty($options['options_model'])) { // get existing values foreach ($options['options_model'] as $k => $v) { // current values $value = array_key_get($data, $v['key']); if ($value !== null && (is_string($value) && $value !== '')) { if (is_array($value)) { $value = array_keys($value); } else { $value = [$value]; } $options['options_model'][$k]['current_values'] = $value; } else { $options['options_model'][$k]['current_values'] = null; } // we skip if we have no values if (empty($options['options_model'][$k]['current_values'])) { unset($options['options_model'][$k]); continue; } // existing values $value = array_key_get($original, $v['key']); if ($value !== null) { if (is_array($value)) { $value = array_keys($value); } else { $value = [$value]; } $options['options_model'][$k]['existing_values'] = $value; } else { $options['options_model'][$k]['existing_values'] = null; } } // validate object_data $sql_options = []; foreach ($options['options_model'] as $k => $v) { // we skip inactive model validations if ($v['options_model'] == 'object_data_model_inactive') { continue; } // process models $temp = explode('::', $v['options_model']); $model = factory::model($temp[0], true); if (empty($temp[1])) { $temp[1] = 'options'; } if ($model->initiator_class == 'object_data' || $model->initiator_class == 'object_table' && !in_array($temp[1], ['options', 'options_active'])) { $temp_options = array_keys(object_data_common::process_options($v['options_model'], null, $v['options_params'], $v['existing_values'])); // difference between arrays $diff = array_diff($v['current_values'], $temp_options); if (!empty($diff)) { $result['options_model'][$k] = 1; } } else { if ($model->initiator_class == 'object_table' && in_array($temp[1], ['options', 'options_active'])) { // last element in the pk is a field $pk = $model->pk; $last = array_pop($pk); // handling inactive $options_active = []; if ($temp[1] == 'options_active') { $options_active = $model->options_active ? $model->options_active : [$model->column_prefix . 'inactive' => 0]; } $sql_options[$k] = ['model' => $temp[0], 'field' => $last, 'params' => $v['options_params'], 'values' => $v['current_values'], 'existing_values' => $v['existing_values'], 'options_active' => $options_active]; } } } // validating options if (!empty($sql_options)) { $sql_model = new object_table_validator(); $sql_result = $sql_model->validate_options_multiple($sql_options); if (!empty($sql_result['discrepancies'])) { foreach ($sql_result['discrepancies'] as $k => $v) { $result['options_model'][$k] = 1; } } } // we roll back if we have errors if (!empty($result['options_model'])) { break; } } // comapare main row $this->timestamp = format::now('timestamp'); $temp = $this->compare_one_row($data, $original, $this->data, ['flag_delete_row' => $options['flag_delete_row'] ?? false, 'flag_main_record' => true]); // if we goe an error if (!empty($temp['error'])) { $result['error'] = $temp['error']; break; } // we display warning if form has not been changed if (empty($temp['data']['total'])) { $result['warning'][] = object_content_messages::no_changes; break; } // insert history if (!empty($temp['data']['history'])) { foreach ($temp['data']['history'] as $k => $v) { $temp2 = $this->primary_model->db_object->insert($k, $v); if (!$temp2['success']) { $result['error'] = $temp2['error']; goto error; } } } // audit if (!empty($temp['data']['audit'])) { // we need to put relation into pk if (!empty($this->primary_model->relation['field'])) { $temp['data']['audit']['pk'][$this->primary_model->relation['field']] = $temp['new_serials'][$this->primary_model->relation['field']] ?? $data[$this->primary_model->relation['field']] ?? $original[$this->primary_model->relation['field']]; } // merge $temp2 = factory::model($this->primary_model->audit_model, true)->merge($temp['data']['audit'], ['changes' => $temp['data']['total']]); if (!$temp2['success']) { $result['error'] = $temp2['error']; break; } } // if we got here we can commit $result['success'] = true; $result['deleted'] = $temp['data']['deleted']; $result['inserted'] = $temp['data']['inserted']; $result['updated'] = $temp['data']['updated']; $result['new_serials'] = $temp['new_serials']; // commit transaction $this->primary_model->db_object->commit(); return $result; } while (0); // we roll back on error error: $this->primary_model->db_object->rollback(); return $result; }
/** * Garbage collector * * @param int $mode - 1 - old, 2 - all * @param array $tags * @return boolean */ public function gc($mode = 1, $tags = []) { if ($mode == 2) { $sql = 'DELETE FROM ' . $this->model_cache->name; } else { if ($mode == 1) { $sql = 'DELETE FROM ' . $this->model_cache->name . ' WHERE sm_cache_expire < \'' . format::now('timestamp') . '\''; if (!empty($tags)) { $tags2 = array_fix($tags); $temp = []; foreach ($tags2 as $v) { $temp[] = "sm_cache_tags LIKE '% {$v} %'"; } $sql .= ' OR (' . implode(' OR ', $temp) . ')'; } } } $db = new db($this->model_cache->db_link); $result = $db->query($sql); return $result['success']; }
/** * Render * * @param string $type * @return string */ public function render($type) { $result = ''; $session = new session(); // main switch switch ($type) { case 'pdf': // document properties $this->header['pdf']['orientation'] = isset($this->header['pdf']['orientation']) ? $this->header['pdf']['orientation'] : 'P'; $this->header['pdf']['unit'] = 'mm'; $this->header['pdf']['format'] = isset($this->header['pdf']['format']) ? $this->header['pdf']['format'] : 'LETTER'; $this->header['pdf']['encoding'] = isset($this->header['pdf']['encoding']) ? $this->header['pdf']['encoding'] : 'UTF-8'; $this->header['pdf']['font'] = isset($this->header['pdf']['font']) ? $this->header['pdf']['font'] : array('family' => 'helvetica', 'style' => '', 'size' => 8); //include 'tcpdf/tcpdf.php'; // create new PDF document $pdf = new TCPDF($this->header['pdf']['orientation'], $this->header['pdf']['unit'], $this->header['pdf']['format'], true, $this->header['pdf']['encoding'], false); // set margins $pdf->SetMargins(0, 0, 0); $pdf->setPrintHeader(false); // disable auto break $pdf->SetAutoPageBreak(false, 0); // set default font subsetting mode $pdf->setFontSubsetting(true); // set color for background $pdf->SetFillColor(255, 255, 255); // set font $pdf->SetFont($this->header['pdf']['font']['family'], $this->header['pdf']['font']['style'], $this->header['pdf']['font']['size']); // stats $page_counter = 1; $page_y = 0; $flag_new_page = true; $flag_filter = true; $flag_first_row = true; $columns = array(); $all_columns = array(); // gethering all columns foreach ($this->data as $k => $v) { if ($v['t'] == 'columns') { $all_columns[] = $v; } } // looping through the data foreach ($this->data as $k => $v) { if ($v['t'] == 'columns') { continue; } if ($flag_new_page) { // add new page $pdf->AddPage($this->header['pdf']['orientation'], '', true); // drawing header $pdf->MultiCell(40, 5, format::datetime(format::now()), 0, 'L', 1, 0, 5, 5, true, 0, false, true, 10, 'T'); // company + book name $pw = $pdf->getPageWidth(); $pdf->SetFont($this->header['pdf']['font']['family'], 'B', $this->header['pdf']['font']['size']); // todo: fix here $pdf->MultiCell($pw - 90, 5, $session->company_name . ': ' . $session->book_name, 0, 'C', 1, 0, 40, 5, true, 0, false, true, 10, 'T'); // page counter $pdf->SetFont($this->header['pdf']['font']['family'], '', $this->header['pdf']['font']['size']); $pdf->MultiCell(40, 5, 'Page ' . $page_counter, 0, 'R', 1, 0, $pw - 45, 5, true, 0, false, true, 10, 'T'); // report name $pdf->SetFont($this->header['pdf']['font']['family'], 'B', $this->header['pdf']['font']['size']); $report_name = $this->header['name'] . ' (' . implode('-', application::get(array('mvc', 'controllers'))) . ')'; $pdf->MultiCell($pw - 10, 5, $report_name, 0, 'L', 1, 0, 5, 10, true, 0, false, true, 10, 'T'); if (isset($this->header['description'])) { $pdf->SetFont($this->header['pdf']['font']['family'], 'B', $this->header['pdf']['font']['size']); $pdf->MultiCell(205, 5, $this->header['description'], 0, 'L', 1, 0, 5, 15, true, 0, false, true, 10, 'T'); $page_y = 25; } else { $page_y = 20; } // if we need to add a filter if ($flag_filter) { if (isset($this->header['filter'])) { foreach ($this->header['filter'] as $k2 => $v2) { $pdf->SetFont($this->header['pdf']['font']['family'], 'B', $this->header['pdf']['font']['size']); $pdf->MultiCell(50, 5, $k2 . ':', 0, 'L', 1, 0, 5, $page_y, true, 0, false, true, 10, 'T'); $pdf->SetFont($this->header['pdf']['font']['family'], '', $this->header['pdf']['font']['size']); $number_of_cells = $pdf->MultiCell($pdf->getPageWidth() - 60, 5, $v2, 0, 'L', 1, 0, 55, $page_y, true, 0, false, true, 10, 'T'); if ($number_of_cells > 1) { $page_y += 5 * ($number_of_cells - 1); } $page_y += 5; } } $flag_filter = false; // adding one line space $page_y += 5; } // page counter $page_counter++; $flag_new_page = false; } // rendering rows if ($flag_first_row) { if (empty($columns)) { $columns = current($all_columns); // repopulate width $count_empty = 0; $taken = 0; foreach ($columns['d'] as $k2 => $v2) { if (empty($v2['w'])) { $count_empty++; } else { $taken += $v2['w']; } } if (!empty($count_empty)) { $new_width = floor(($pdf->getPageWidth() - 10 - $taken) / $count_empty); foreach ($v['d'] as $k2 => $v2) { $columns['d'][$k2]['w'] = $new_width; } } } $flag_first_row = false; // columns foreach ($all_columns as $k20 => $v20) { $x = 5; foreach ($columns['d'] as $k10 => $v10) { foreach (array('v', 'c', 'a', 'b', 's', 't', 'u') as $v30) { if (isset($v20['d'][$k10][$v30])) { $v10[$v30] = $v20['d'][$k10][$v30]; } } $new_width = @$v10['w']; if (!empty($v10['c'])) { // we need to get width of next elements for ($i = $k10 + 1; $i < $k10 + $v10['c']; $i++) { $new_width += $columns['d'][$k10]['w']; } } $align = str_replace(array('left', 'right', 'center'), array('L', 'R', 'C'), @$v10['a']); if (empty($align)) { $align = 'L'; } if (@$v10['b']) { $pdf->SetFont($this->header['pdf']['font']['family'], 'B', $this->header['pdf']['font']['size']); } else { $pdf->SetFont($this->header['pdf']['font']['family'], '', $this->header['pdf']['font']['size']); } $pdf->MultiCell($new_width, 5, @$v10['v'], $this->flag_pdf_show_borders, $align, 1, 0, $x, $page_y, true, 0, false, true, 10, 'T'); // underline if (@$v10['u']) { $pdf->SetLineStyle(array('width' => 0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0))); $pdf->Line($x, $page_y + 5, $x + @$v10['w'], $page_y + 5); } $x += @$v10['w']; } $page_y += 5; } } $pdf->SetFont($this->header['pdf']['font']['family'], '', $this->header['pdf']['font']['size']); $x = 5; foreach ($columns['d'] as $k10 => $v10) { // we do do render cells if no data if (isset($v['d'][$k10]['v'])) { $align = str_replace(array('left', 'right', 'center'), array('L', 'R', 'C'), @$v['d'][$k10]['a']); if (empty($align)) { $align = 'L'; } if (@$v['d'][$k10]['b']) { $pdf->SetFont($this->header['pdf']['font']['family'], 'B', $this->header['pdf']['font']['size']); } else { $pdf->SetFont($this->header['pdf']['font']['family'], '', $this->header['pdf']['font']['size']); } // if we override width $width = $v10['w']; if (isset($v['d'][$k10]['w'])) { $width = $v['d'][$k10]['w']; } else { if (isset($v['d'][$k10]['c'])) { // colspan // we need to get width of next elements for ($i = $k10 + 1; $i < $k10 + $v['d'][$k10]['c']; $i++) { $width += @$columns['d'][$i]['w']; } } } $value = @$v['d'][$k10]['v']; $value = str_replace(' ', ' ', $value); // rendering cell $pdf->MultiCell($width, 5, $value, $this->flag_pdf_show_borders, $align, 1, 0, $x, $page_y, true, 0, false, true, 10, 'T'); // underline if (@$v['d'][$k10]['u']) { $pdf->SetLineStyle(array('width' => 0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0))); $pdf->Line($x, $page_y + 5, $x + $v10['w'], $page_y + 5); } // subtotal if (@$v['d'][$k10]['s']) { $pdf->SetLineStyle(array('width' => 0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0))); $pdf->Line($x + 1, $page_y, $x + $v10['w'] - 1, $page_y); } // total if (@$v['d'][$k10]['t']) { $pdf->SetLineStyle(array('width' => 0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0))); $pdf->Line($x + 1, $page_y, $x + $v10['w'] - 1, $page_y); $pdf->Line($x + 1, $page_y - 0.75, $x + $v10['w'] - 1, $page_y - 0.75); } } $x += @$v10['w']; } // incrementing $page_y += 5; if ($page_y > $pdf->getPageHeight() - 10) { $flag_new_page = true; $flag_first_row = true; } } $pdf->Output($this->header['name'] . '.pdf', 'I'); exit; break; case 'csv': case 'txt': case 'xlsx': $sheet = strip_tags($this->header['name']); $sheet = str_replace(['/', '\\'], '', $sheet); // generating header $header = []; $header[$sheet][] = [format::datetime(format::now()), '', $session->company_name . ': ' . $session->book_name, '', 'Page 1']; $controllers = application::get(['mvc', 'controllers']); $header[$sheet][] = [strip_tags($this->header['name']) . ' (' . implode('-', $controllers) . ')']; if (isset($this->header['description'])) { $header[$sheet][] = [$this->header['description']]; } $header[$sheet][] = [' ']; $temp = $header; // displaying filter if (isset($this->header['filter'])) { $temp2 = []; foreach ($this->header['filter'] as $k => $v) { $temp[$sheet][] = [strip_tags($k), strip_tags($v)]; } $temp[$sheet][] = [' ']; } // converting data foreach ($this->data as $k => $v) { $temp2 = []; foreach ($v['d'] as $k2 => $v2) { if (is_array($v2)) { $value = $v2['v'] ?? null; } else { $value = $v2; } // replaces $value = str_replace(' ', ' ', $value); $temp2[] = strip_tags($value); } $temp[$sheet][] = $temp2; } // get output buffering helper_ob::clean_all(); // content switch ($type) { case 'xlsx': echo io::array_to_excel($temp, io::$formats[$type]['excel_code'], null); break; default: // csv or text header('Content-Type: ' . numbers_frontend_exports_csv_base::$formats[$type]['content_type']); header('Content-Disposition: attachment; filename="' . $sheet . '.' . $type . '"'); header('Cache-Control: max-age=0'); echo numbers_frontend_exports_csv_base::array_to_csv($temp, numbers_frontend_exports_csv_base::$formats[$type]['delimiter'], numbers_frontend_exports_csv_base::$formats[$type]['enclosure']); } exit; break; case 'html': case 'html2': default: // rendering data $table = ['options' => []]; $counter = 1; foreach ($this->data as $k => $v) { $flag_colspan = 0; $row = []; if (!empty($v['d'])) { foreach ($v['d'] as $k2 => $v2) { if ($flag_colspan > 0) { $flag_colspan--; continue; } $colspan = ''; if ($v2['c'] ?? null) { $colspan = $v2['c']; $flag_colspan = $v2['c'] - 1; } $align = 'left'; $title = ''; $style = ''; if (is_array($v2)) { $value = $v2['v'] ?? null; if (!empty($v2['h'])) { $v2['h']['value'] = $value; $value = html::a($v2['h']); } if (!empty($v2['a'])) { $align = $v2['a']; } if (!empty($v2['l'])) { $title = $v2['l']; } // bold lines if ($v2['b'] ?? null) { $value = '<b>' . $value . '</b>'; } // todo: convert styles to classes if ($v2['s'] ?? null) { $style .= 'border-top: 1px solid #000;'; } if ($v2['t'] ?? null) { $style .= 'border-top: 3px double #000;'; } if ($v2['u'] ?? null) { $style .= 'border-bottom: 1px solid #000;'; } } else { $value = $v2; } $row[$k2] = ['value' => $value, 'align' => $align, 'colspan' => $colspan, 'style' => $style, 'title' => $title, 'nowrap' => true]; } } else { $row[0] = ['value' => ' ']; } $table['options'][$counter] = $row; $counter++; } $result = html::table($table); // printable export if ($type == 'html2') { $header = ['options' => []]; $header['options'][] = [0 => format::datetime(format::now()), 1 => '', 2 => $session->company_name . ': ' . $session->book_name, 3 => '', 4 => 'Page 1']; $controllers = application::get(['mvc', 'controllers']); $header['options'][] = [['value' => strip_tags($this->header['name']) . ' (' . implode('-', $controllers) . ')', 'colspan' => 5]]; if (isset($this->header['description'])) { $header['options'][] = [$this->header['description']]; } $header['options'][] = [' ']; // displaying filter if (isset($this->header['filter'])) { $temp2 = []; foreach ($this->header['filter'] as $k => $v) { $header['options'][] = [strip_tags($k) . ':', strip_tags($v)]; } $header['options'][] = [' ']; } $header = html::table($header); layout::render_as($header . $result, 'text/html'); } } return $result; }
/** * Process default value * * @param string $key * @param mixed $default * @param array $neighbouring_values * @return mixed */ private function process_default_value($key, $default, $value, &$neighbouring_values, $set_neightbouring_values = true, $changed_field = [], $options = []) { if (strpos($default, 'dependent::') !== false) { // nothing } else { if (strpos($default, 'master_object::') !== false) { $field = explode('::', str_replace(['master_object::', 'static::'], '', $default)); $value = $this->master_object->{$field[0]}->{$field[1]}->{$field[2]}; } else { if (strpos($default, 'parent::') !== false) { $field = str_replace(['parent::', 'static::'], '', $default); $value = $this->values[$field] ?? null; } else { if ($default === 'now()') { $default = format::now('timestamp'); } $value = $default; } } } // handling override_field_value method if (!empty($this->wrapper_methods['process_default_value']['main'])) { // fix changed field if (empty($changed_field)) { $changed_field = []; } $changed_field['parent'] = $changed_field['parent'] ?? null; $changed_field['detail'] = $changed_field['detail'] ?? null; $changed_field['subdetail'] = $changed_field['subdetail'] ?? null; // call override method $model = $this->wrapper_methods['process_default_value']['main'][0]; $model->{$this->wrapper_methods['process_default_value']['main'][1]}($this, $key, $default, $value, $neighbouring_values, $changed_field, $options); } // if we need to set neightbouring values if ($set_neightbouring_values) { $neighbouring_values[$key] = $value; } return $value; }
/** * Process who columns * * @param mixed $types * @param array $row */ public function process_who_columns($types, &$row, $timestamp = null) { if ($types === 'all') { $types = array_keys($this->who); } if (!is_array($types)) { $types = [$types]; } if (empty($timestamp)) { $timestamp = format::now('timestamp'); } foreach ($types as $type) { if (!empty($this->who[$type])) { // timestamp $row[$this->column_prefix . $type . '_timestamp'] = $timestamp; // entity # $row[$this->column_prefix . $type . '_entity_id'] = entity::id(); } else { if ($type == 'optimistic_lock') { if ($this->optimistic_lock) { $row[$this->optimistic_lock_column] = $timestamp; } } } } }
/** * Garbage collector * * @param int $life * @return boolean */ public function gc($life) { // step 1: we need to move expired sessions to logins table $db = new db($this->model_seessions->db_link); $expire = format::now('timestamp'); // generating sqls $sql_move = <<<TTT \t\t\tINSERT INTO sm_logins ( \t\t\t\tsm_login_id, \t\t\t\tsm_login_started, \t\t\t\tsm_login_last_requested, \t\t\t\tsm_login_pages_count, \t\t\t\tsm_login_user_ip, \t\t\t\tsm_login_user_id \t\t\t) \t\t\tSELECT \t\t\t\tnextval('sm_logins_sm_login_id_seq') sm_login_id, \t\t\t\ts.sm_session_started sm_login_started, \t\t\t\ts.sm_session_last_requested sm_login_last_requested, \t\t\t\ts.sm_session_pages_count sm_login_pages_count, \t\t\t\ts.sm_session_user_ip sm_login_user_ip, \t\t\t\ts.sm_session_user_id sm_login_user_id \t\t\tFROM sm_sessions s \t\t\tWHERE 1=1 \t\t\t\tAND s.sm_session_expires < '{$expire}' TTT; // session cleaning sql $sql_delete = <<<TTT \t\t\tDELETE FROM sm_sessions \t\t\tWHERE 1=1 \t\t\t\tAND sm_session_expires < '{$expire}' TTT; // making changes to database $db->begin(); $result = $db->query($sql_move); if (!$result['success']) { $db->rollback(); return false; } $result = $db->query($sql_delete); if (!$result['success']) { $db->rollback(); return false; } $db->commit(); return true; }