Ejemplo n.º 1
0
 /**
  * Whether we can perform certain action
  *
  * @param mixed $action_code_or_id
  * @return boolean
  */
 public static function can($action_code_or_id)
 {
     if (self::$cache_actions === null) {
         self::$cache_actions = factory::model('numbers_backend_system_controller_model_actions')->get();
     }
     if (is_string($action_code_or_id)) {
         foreach (self::$cache_actions as $k => $v) {
             if ($v['sm_cntractn_code'] == $action_code_or_id) {
                 $action_code_or_id = $k;
                 break;
             }
         }
     }
     if (!isset(self::$cache_actions[$action_code_or_id])) {
         throw new Exception('Unknown action!');
     }
     $permissions = application::get(['controller', 'acl', 'permissions']);
     $start = $action_code_or_id;
     do {
         // see if we have permission
         if (empty($permissions[$start])) {
             break;
         }
         // we need to check permission on a parent
         if (!empty(self::$cache_actions[$start]['sm_cntractn_parent_id'])) {
             $start = self::$cache_actions[$start]['sm_cntractn_parent_id'];
         } else {
             // exit if there's no parent
             return true;
         }
     } while (1);
     return false;
 }
Ejemplo n.º 2
0
 /**
  * Render form
  *
  * @return mixed
  */
 public function render()
 {
     // ajax requests from another form
     if ($this->flag_another_ajax_call) {
         return null;
     }
     $this->tabindex = 1;
     // css & js
     numbers_frontend_media_libraries_jssha_base::add();
     layout::add_js('/numbers/media_submodules/numbers_frontend_html_form_media_js_base.js', -10000);
     // include master js
     if (!empty($this->master_object) && method_exists($this->master_object, 'add_js')) {
         $this->master_object->add_js();
     }
     // include js
     $filename = str_replace('_model_', '_media_js_', $this->form_class) . '.js';
     if (file_exists('./../libraries/vendor/' . str_replace('_', '/', $filename))) {
         layout::add_js('/numbers/media_submodules/' . $filename);
     }
     $this->misc_settings['extended_js_class'] = 'numbers.' . $this->form_class;
     // include css
     $filename = str_replace('_model_', '_media_css_', $this->form_class) . '.css';
     if (file_exists('./../libraries/vendor/' . str_replace('_', '/', $filename))) {
         layout::add_css('/numbers/media_submodules/' . $filename);
     }
     // load mask
     numbers_frontend_media_libraries_loadmask_base::add();
     // new record action
     $mvc = application::get('mvc');
     if (object_controller::can('record_new')) {
         $onclick = 'return confirm(\'' . strip_tags(i18n(null, object_content_messages::confirm_blank)) . '\');';
         $this->actions['form_new'] = ['value' => 'New', 'sort' => -31000, 'icon' => 'file-o', 'href' => $mvc['full'] . '?' . $this::button_submit_blank . '=1', 'onclick' => $onclick, 'internal_action' => true];
     }
     // back to list
     if (object_controller::can('list_view')) {
         $this->actions['form_back'] = ['value' => 'Back', 'sort' => -32000, 'icon' => 'arrow-left', 'href' => $mvc['controller'] . '/_index', 'internal_action' => true];
     }
     // reload button
     if ($this->values_loaded) {
         $url = $mvc['full'] . '?' . http_build_query2($this->pk);
         $this->actions['form_refresh'] = ['value' => 'Refresh', 'sort' => 32000, 'icon' => 'refresh', 'href' => $url, 'internal_action' => true];
     }
     // handling override_field_value method
     if (!empty($this->wrapper_methods['pre_render']['main'])) {
         call_user_func_array($this->wrapper_methods['pre_render']['main'], [&$this]);
     }
     // assembling everything into result variable
     $result = [];
     // order containers based on order column
     array_key_sort($this->data, ['order' => SORT_ASC]);
     foreach ($this->data as $k => $v) {
         if (!$v['flag_child']) {
             if ($v['type'] == 'fields' || $v['type'] == 'details') {
                 // reset tabs
                 $this->current_tab = [];
                 $temp = $this->render_container($k);
                 if ($temp['success']) {
                     $result[$k] = $temp['data'];
                 }
             } else {
                 if ($v['type'] == 'tabs') {
                     // tabs
                     $tab_id = "form_tabs_{$this->form_link}_{$k}";
                     $tab_header = [];
                     $tab_values = [];
                     $tab_options = [];
                     $have_tabs = false;
                     // sort rows
                     array_key_sort($v['rows'], ['order' => SORT_ASC]);
                     foreach ($v['rows'] as $k2 => $v2) {
                         $this->current_tab[] = "{$tab_id}_{$k2}";
                         $labels = '';
                         foreach (['records', 'danger', 'warning', 'success', 'info'] as $v78) {
                             $labels .= html::label2(['type' => $v78 == 'records' ? 'primary' : $v78, 'style' => 'display: none;', 'value' => 0, 'id' => implode('__', $this->current_tab) . '__' . $v78]);
                         }
                         $tab_header[$k2] = i18n(null, $v2['options']['label_name']) . $labels;
                         $tab_values[$k2] = '';
                         // handling override_tabs method
                         if (!empty($this->wrapper_methods['override_tabs']['main'])) {
                             $tab_options[$k2] = call_user_func_array($this->wrapper_methods['override_tabs']['main'], [&$this, &$v2, &$k2, &$this->values]);
                             if (empty($tab_options[$k2]['hidden'])) {
                                 $have_tabs = true;
                             }
                         } else {
                             $have_tabs = true;
                         }
                         // tab index for not hidden tabs
                         if (empty($tab_options[$k2]['hidden'])) {
                             $tab_options[$k2]['tabindex'] = $this->tabindex;
                             $this->tabindex++;
                         }
                         // render containers
                         array_key_sort($v2['elements'], ['order' => SORT_ASC]);
                         foreach ($v2['elements'] as $k3 => $v3) {
                             $temp = $this->render_container($v3['options']['container']);
                             if ($temp['success']) {
                                 $tab_values[$k2] .= $temp['data']['html'];
                             }
                         }
                         // remove last element from an array
                         array_pop($this->current_tab);
                     }
                     // if we do not have tabs
                     if ($have_tabs) {
                         $result[$k]['html'] = html::tabs(['id' => $tab_id, 'class' => 'form-tabs', 'header' => $tab_header, 'options' => $tab_values, 'tab_options' => $tab_options]);
                     }
                 }
             }
         }
     }
     // formatting data
     $temp = [];
     foreach ($result as $k => $v) {
         $temp[] = $v['html'];
     }
     $result = implode('', $temp);
     // we need to skip internal actions
     if (!empty($this->options['no_actions'])) {
         foreach ($this->actions as $k0 => $v0) {
             if (!empty($v0['internal_action'])) {
                 unset($this->actions[$k0]);
             }
         }
     }
     // rendering actions
     if (!empty($this->actions)) {
         $value = '<div style="text-align: right;">' . $this->render_actions() . '</div>';
         $value .= '<hr class="simple" />';
         $result = $value . $result;
     }
     // messages
     if (!empty($this->errors['general'])) {
         $messages = '';
         foreach ($this->errors['general'] as $k => $v) {
             $messages .= html::message(['options' => $v, 'type' => $k]);
         }
         $result = '<div class="form_message_container">' . $messages . '</div>' . $result;
     }
     // couple hidden fields
     $result .= html::hidden(['name' => '__form_link', 'value' => $this->form_link]);
     $result .= html::hidden(['name' => '__form_values_loaded', 'value' => $this->values_loaded]);
     $result .= html::hidden(['name' => '__form_onchange_field_values_key', 'value' => '']);
     if (!empty($this->options['bypass_hidden_values'])) {
         foreach ($this->options['bypass_hidden_values'] as $k => $v) {
             $result .= html::hidden(['name' => $k, 'value' => $v]);
         }
     }
     // js to update counters in tabs
     if (!empty($this->errors['tabs'])) {
         foreach ($this->errors['tabs'] as $k => $v) {
             layout::onload("\$('#{$k}').html({$v}); \$('#{$k}').show();");
         }
     }
     // if we have form
     if (empty($this->options['skip_form'])) {
         $mvc = application::get('mvc');
         $result = html::form(['action' => $mvc['full'], 'name' => "form_{$this->form_link}_form", 'id' => "form_{$this->form_link}_form", 'value' => $result, 'onsubmit' => empty($this->options['no_ajax_form_reload']) ? 'return numbers.form.on_form_submit(this);' : null]);
     }
     // if we came from ajax we return as json object
     if (!empty($this->options['input']['__ajax'])) {
         $result = ['success' => true, 'error' => [], 'html' => $result, 'js' => layout::$onload];
         layout::render_as($result, 'application/json');
     }
     $result = "<div id=\"form_{$this->form_link}_form_mask\"><div id=\"form_{$this->form_link}_form_wrapper\">" . $result . '</div></div>';
     // if we have segment
     if (isset($this->options['segment'])) {
         $temp = is_array($this->options['segment']) ? $this->options['segment'] : [];
         $temp['value'] = $result;
         $result = html::segment($temp);
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Export
  *
  * @param object $object
  * @param string $format
  * @return string
  */
 public static function export($object, $format = 'html')
 {
     $header = ['name' => i18n(null, object_controller::title()), 'filter' => numbers_frontend_html_list_filter::human($object)];
     // sort
     if (!empty($object->orderby)) {
         $temp = [];
         foreach ($object->orderby as $k => $v) {
             if ($k == 'full_text_search') {
                 $temp[] = i18n(null, 'Text Search') . ' ' . ($v == SORT_ASC ? 'Asc.' : 'Desc.');
             } else {
                 $temp[] = i18n(null, $object->columns[$k]['name']) . ' ' . ($v == SORT_ASC ? 'Asc.' : 'Desc.');
             }
         }
         $header['filter'][i18n(null, 'Sort')] = implode(', ', $temp);
     }
     // report object
     $report = new numbers_frontend_html_report_base($header);
     // adding columns
     $columns = [];
     foreach ($object->columns as $k => $v) {
         if ($k == 'action') {
             continue;
         }
         $columns[] = ['value' => i18n(null, $v['name']), 'bold' => true, 'width' => 10];
     }
     $report->add($columns, 'columns');
     // adding data to report
     if (empty($object->rows)) {
         $report->add([['value' => i18n(null, object_content_messages::no_rows_found)]]);
     } else {
         $counter = 1;
         foreach ($object->rows as $k => $v) {
             $data = [];
             foreach ($object->columns as $k2 => $v2) {
                 // we skip action column when exporting
                 if ($k2 == 'action') {
                     continue;
                 }
                 $value = [];
                 // process rows
                 if ($k2 == 'row_number') {
                     $value['value'] = $counter . '.';
                 } else {
                     if ($k2 == 'offset_number') {
                         $value['value'] = $object->offset + $counter . '.';
                     } else {
                         if (!empty($v2['options']) && !is_array($v[$k2])) {
                             $value['value'] = $v2['options'][$v[$k2]]['name'];
                         } else {
                             if (isset($v[$k2])) {
                                 $value['value'] = $v[$k2];
                             } else {
                                 $value['value'] = null;
                             }
                         }
                     }
                 }
                 // put value into row
                 $data[] = $value;
             }
             $report->add($data);
             $counter++;
         }
     }
     // render report
     return $report->render($format);
 }
Ejemplo n.º 4
0
 /**
  * Data default renderer
  *
  * @return string
  */
 private final function render_data_default()
 {
     $result = '';
     // if we have no rows we display a messsage
     if ($this->num_rows == 0) {
         return html::message(['type' => 'warning', 'options' => [i18n(null, object_content_messages::no_rows_found)]]);
     }
     $counter = 1;
     $table = ['header' => [], 'options' => []];
     // action flags
     $actions = [];
     if (object_controller::can('record_view')) {
         $actions['view'] = true;
     }
     // generate columns
     foreach ($this->columns as $k => $v) {
         // if we can not view we skip action column
         if (empty($actions) && $k == 'action') {
             continue;
         }
         $table['header'][$k] = ['value' => i18n(null, $v['name']), 'nowrap' => true, 'width' => $v['width'] ?? null];
     }
     // generate rows
     foreach ($this->rows as $k => $v) {
         // process all columns first
         $row = [];
         foreach ($this->columns as $k2 => $v2) {
             // if we can not view we skip action column
             if (empty($actions) && $k2 == 'action') {
                 continue;
             }
             $value = [];
             // create cell properties
             foreach (['width', 'align'] as $v3) {
                 if (isset($v2[$v3])) {
                     $value[$v3] = $v2[$v3];
                 }
             }
             // process rows
             if ($k2 == 'action') {
                 $value['value'] = [];
                 if (!empty($actions['view'])) {
                     $mvc = application::get('mvc');
                     $pk = extract_keys($this->model_object->pk, $v);
                     $url = $mvc['controller'] . '/_edit?' . http_build_query2($pk);
                     $value['value'][] = html::a(['value' => i18n(null, 'View'), 'href' => $url]);
                 }
                 $value['value'] = implode(' ', $value['value']);
             } else {
                 if ($k2 == 'row_number') {
                     $value['value'] = format::id($counter) . '.';
                 } else {
                     if ($k2 == 'offset_number') {
                         $value['value'] = format::id($this->offset + $counter) . '.';
                     } else {
                         if (!empty($v2['options_model'])) {
                             if (strpos($v2['options_model'], '::') === false) {
                                 $v2['options_model'] .= '::options';
                             }
                             $params = $v2['options_params'] ?? [];
                             if (!empty($v2['options_depends'])) {
                                 foreach ($v2['options_depends'] as $k0 => $v0) {
                                     $params[$k0] = $v[$v0];
                                 }
                             }
                             $crypt_object = new crypt();
                             $hash = $crypt_object->hash($v2['options_model'] . serialize($params));
                             if (!isset($this->cached_options[$hash])) {
                                 $method = factory::method($v2['options_model'], null, true);
                                 $this->cached_options[$hash] = call_user_func_array($method, [['where' => $params]]);
                             }
                             if (isset($this->cached_options[$hash][$v[$k2]])) {
                                 $value['value'] = $this->cached_options[$hash][$v[$k2]]['name'];
                             } else {
                                 $value['value'] = null;
                             }
                         } else {
                             if (!empty($v2['options']) && !is_array($v[$k2])) {
                                 if (isset($v2['options'][$v[$k2]])) {
                                     $value['value'] = $v2['options'][$v[$k2]]['name'];
                                 } else {
                                     $value['value'] = null;
                                 }
                             } else {
                                 if (isset($v[$k2])) {
                                     $value['value'] = $v[$k2];
                                 } else {
                                     $value['value'] = null;
                                 }
                             }
                         }
                     }
                 }
             }
             // put value into row
             if (!empty($v2['format'])) {
                 $format_options = $v2['format_options'] ?? [];
                 if (!empty($v2['format_depends'])) {
                     $format_depends = $v2['format_depends'];
                     $this->process_params_and_depends($format_depends, $v);
                     $format_options = array_merge_hard($format_options, $format_depends);
                 }
                 $method = factory::method($v2['format'], 'format');
                 $value['value'] = call_user_func_array([$method[0], $method[1]], [$value['value'], $format_options]);
             }
             $row[$k2] = $value;
         }
         // put processed columns though user defined function
         if (method_exists($this, 'render_data_rows')) {
             $table['options'][$counter] = $this->render_data_rows($row, $v);
         } else {
             $table['options'][$counter] = $row;
         }
         $counter++;
     }
     return html::table($table);
 }