'noaccess']); } include 'general/noaccess.php'; exit; } // Set baseUrl for use it in several locations in this class. $this->baseUrl = ui_get_full_url('index.php?sec=gmodules&sec2=godmode/modules/manage_module_templates'); // Capture all parameters before start. $this->id_np = get_parameter('id_np', -1); if ($this->id_np > 0) { // Profile exists. Set the attributes with the info. $this->setNetworkProfile(); } else { $this->name = get_parameter('name', ''); $this->description = get_parameter('description', ''); $this->pen = get_parameter('pen', ''); } $this->action = get_parameter('action_button', ''); $this->offset = get_parameter('offset', 0); $this->ajaxController = $ajax_controller; $this->ncGroup = get_parameter('add-modules-group', '0'); $this->ncFilter = get_parameter('add-modules-filter', ''); // Get all of PENs valid for autocomplete. $getPENs = db_get_all_rows_sql('SELECT pen,manufacturer FROM tpen'); $outputPENs = []; $this->validPen = ''; $this->penRefs = []; foreach ($getPENs as $pen) { $this->validPen .= ((int) $pen['pen']).','; $this->penRefs[] = [ 'label' => io_safe_output($pen['manufacturer']), 'value' => $pen['pen'], ]; // Reverse autocompletion. $this->penRefs[] = [ 'label' => $pen['pen'], 'value' => $pen['pen'], ]; } chop($this->validPen); return $this; } /** * Run main page. * * @return void */ public function run() { // CSS. ui_require_css_file('wizard'); ui_require_css_file('discovery'); // Javascript. ui_require_javascript_file('jquery.caret.min'); // Breadcrums. $this->setBreadcrum([]); if ($this->id_np > 0) { // Add a breadcrumb with the current template. $urlToGo = $this->baseUrl.'&id_np='.$this->id_np; $this->prepareBreadcrum( [ ['label' => __('Configuration')], ['label' => __('Templates')], [ 'link' => $this->baseUrl, 'label' => __('Module template management'), 'selected' => false, ], [ 'link' => $urlToGo, 'label' => $this->name, 'selected' => true, ], ], true ); } else { $this->prepareBreadcrum( [ ['label' => __('Configuration')], ['label' => __('Templates')], [ 'link' => $this->baseUrl, 'label' => __('Module template management'), 'selected' => true, ], ], true ); } // Prints the header. ui_print_page_header( __('Module template management'), '', false, '', true, '', false, '', GENERIC_SIZE_TEXT, '', $this->printHeader(true) ); // Process the data if action is required if (!empty($this->action)) { $this->processData(); } if ($this->id_np === -1) { // List all Module Blocks. $this->moduleTemplateList(); } else { // Show form for create or update template. $this->moduleTemplateForm(); } $this->loadJS(); } /** * Minor function to dump json message as ajax response. * * @param string $type Type: result || error. * @param string $msg Message. * * @return void */ private function ajaxMsg($type, $msg) { if ($type == 'error') { echo json_encode( [ $type => ui_print_error_message( __($msg), '', true ), ] ); } else { echo json_encode( [ $type => ui_print_success_message( __($msg), '', true ), ] ); } exit; } /** * Save or Update the data received. * * @return void */ public function processData() { // Only needed if process data. $modules_submit = get_parameter('add-modules-submit', ''); // Success variable. $success = false; // Evaluate the modules allowed. if (!empty($this->action)) { $numberComponent = []; foreach ($_POST as $k => $value) { if (strpos($k, 'module_check_') >= 0 && $value == 1) { $tmpNumberComponent = explode('_', $k); $numberComponent[] = $tmpNumberComponent[2]; } } switch ($this->action) { case 'Update': $dbResult_tnp = db_process_sql_update( 'tnetwork_profile', [ 'name' => $this->name, 'description' => $this->description, ], ['id_np' => $this->id_np] ); if ($dbResult_tnp === false) { $success = false; } else { db_process_sql_delete('tnetwork_profile_pen', ['id_np' => $this->id_np]); $pensList = explode(',', $this->pen); if (count($pensList) > 0) { // Set again the new PENs associated. foreach ($pensList as $currentPen) { $dbResult_pen = db_process_sql_insert( 'tnetwork_profile_pen', [ 'pen' => $currentPen, 'id_np' => $this->id_np, ] ); if ($dbResult_pen === false) { $success = false; break; } $success = true; } } else { $success = true; } } if ($success === true) { $msg = __('Template %s successfully updated', $this->name); } else { $msg = __('Error updating template'); } break; case 'Create': $dbResult_tnp = db_process_sql_insert( 'tnetwork_profile', [ 'name' => $this->name, 'description' => $this->description, ] ); // The insert gone fine! if ($dbResult_tnp != false) { // Set the new id_np. $this->id_np = $dbResult_tnp; $pensList = explode(',', $this->pen); // Insert all of new PENs associated with this id_np. foreach ($pensList as $currentPen) { $dbResult_pen = db_process_sql_insert( 'tnetwork_profile_pen', [ 'pen' => $currentPen, 'id_np' => $this->id_np, ] ); // If something is wrong, is better stop. if ($dbResult_pen === false) { break; } $success = true; } } if ($success === true) { $msg = __('Template %s successfully created', $this->name); } else { $msg = __('Error creating template'); } break; case 'Delete': $success = db_process_sql_delete('tnetwork_profile', ['id_np' => $this->id_np]); if ($success != false) { $msg = __('Template %s successfully deleted', $this->name); } else { $msg = __('Error deleting %s template', $this->name); } // Reset id_np for show the templates list. $this->id_np = -1; break; default: // There is possible want do an action detailed. $action_detailed = explode('_', $this->action); // Action deletion. if ($action_detailed[0] === 'del') { // Block or Module is affected. switch ($action_detailed[1]) { case 'module': $success = $this->deleteModule($action_detailed[2]); if ($success != false) { $msg = __('Module successfully deleted'); } else { $msg = __('Error deleting module'); } break; case 'block': $block = explode('-', $action_detailed[2]); foreach ($block as $module) { $success = $this->deleteModule($module); } if ($success != false) { $msg = __('Block successfully deleted'); } else { $msg = __('Error deleting block'); } break; default: // Do nothing. break; } } else { $msg = __('Something gone wrong. Please, try again'); } break; } if ($success === false) { ui_print_error_message($msg); } else { ui_print_success_message($msg); } } else if ($modules_submit != '') { $modulesToAdd = get_parameter('add-modules-components', ''); $modulesToAddList = explode(',', $modulesToAdd); foreach ($modulesToAddList as $module) { db_process_sql_insert( 'tnetwork_profile_component', [ 'id_nc' => $module, 'id_np' => $this->id_np, ] ); } $this->ajaxMsg('result', __('Components added sucessfully')); } } /** * Delete of block the module desired * * @param integer $id_module Id of module that must delete. * * @return mixed Return false if something went wrong. */ private function deleteModule($id_module) { $dbResult = db_process_sql_delete( 'tnetwork_profile_component', [ 'id_np' => $this->id_np, 'id_nc' => $id_module, ] ); return $dbResult; } /** * Show the adding modules form * * @return void */ public function addingModulesForm() { // Get the groups for select input. $result = db_get_all_rows_in_table('tnetwork_component_group', 'name'); if ($result === false) { $result = []; } // 2 arrays. 1 with the groups, 1 with the groups by parent $groups = []; $groups_compound = []; // Default group filter. $groups_compound[0] = 'Group - All'; foreach ($result as $row) { $groups[$row['id_sg']] = $row['name']; } foreach ($result as $row) { $groups_compound[$row['id_sg']] = ''; if ($row['parent'] > 1) { $groups_compound[$row['id_sg']] = $groups[$row['parent']].' / '; } } // Get the components for show in a list for select. if ($this->ncGroup > 0) { $sql = sprintf( " SELECT id_nc, name, id_group FROM tnetwork_component WHERE id_group = %d AND name LIKE '%".$this->ncFilter."%' ORDER BY name", $this->ncGroup ); } else { $sql = " SELECT id_nc, name, id_group FROM tnetwork_component WHERE name LIKE '%".$this->ncFilter."%' ORDER BY name"; } $result = db_get_all_rows_sql($sql); $components = []; if ($result === false) { $result = []; } foreach ($result as $row) { $components[$row['id_nc']] = $row['name']; } // Main form. $form = [ 'action' => $this->baseUrl, 'id' => 'add_module_form', 'method' => 'POST', 'class' => 'modal', 'extra' => '', ]; // Inputs. $inputs = []; $inputs[] = [ 'id' => 'inp-id_np', 'arguments' => [ 'name' => 'id_np', 'type' => 'hidden', 'value' => $this->id_np, 'return' => true, ], ]; $inputs[] = [ 'id' => 'add-modules-components-values', 'arguments' => [ 'name' => 'add-modules-components-values', 'type' => 'hidden', 'value' => '', 'return' => true, ], ]; $inputs[] = [ 'label' => __('Filter'), 'id' => 'txt-add-modules-filter', 'arguments' => [ 'name' => 'add-modules-filter', 'input_class' => 'flex-row', 'type' => 'text', 'value' => '', 'return' => true, ], ]; $inputs[] = [ 'arguments' => [ 'label' => __('Filter'), 'name' => 'add-modules-submit', 'type' => 'button', 'script' => 'this.form.submit()', 'attributes' => 'class="sub search"', 'return' => true, ], ]; $inputs[] = [ 'label' => __('Group'), 'id' => 'add-modules-group', 'arguments' => [ 'name' => 'add-modules-group', 'input_class' => 'flex-row', 'type' => 'select', 'script' => 'this.form.submit()', 'fields' => $groups_compound, 'nothing' => $groups_compound[$this->ncGroup], 'return' => true, ], ]; $inputs[] = [ 'label' => __('Components'), 'id' => 'slc-add-modules-components', 'arguments' => [ 'name' => 'add-modules-components', 'input_class' => 'flex-row', 'type' => 'select', 'multiple' => true, 'fields' => $components, 'return' => true, ], ]; $this->printForm( [ 'form' => $form, 'inputs' => $inputs, true ] ); } /** * General setter * * @return void */ private function setNetworkProfile() { $profileInfo = db_get_row('tnetwork_profile', 'id_np', $this->id_np); $this->name = $profileInfo['name']; $this->description = $profileInfo['description']; $penInfo = db_get_all_rows_filter('tnetwork_profile_pen', ['id_np' => $this->id_np]); $penList = []; foreach ($penInfo as $pen) { $penList[] = $pen['pen']; } $this->pen = implode(',', $penList); } /** * Create the table with the list of Blocks Templates * * @return void */ public function moduleTemplateList() { global $config; // Get the count of Blocks. $countModuleBlocks = db_get_value( 'count(*)', 'tnetwork_profile' ); // Get all the data. $resultModuleBlocksTable = db_get_all_rows_filter( 'tnetwork_profile', [ 'order' => 'name', 'limit' => $config['block_size'], 'offset' => $this->offset, ] ); ui_pagination($countModuleBlocks, false, $this->offset); // Create the table with Module Block list. $table = new StdClasS(); $table->class = 'databox data'; $table->width = '75%'; $table->styleTable = 'margin: 2em auto 0;border: 1px solid #ddd;background: white;'; $table->rowid = []; $table->data = []; $table->cellpadding = 0; $table->cellspacing = 0; $table->width = '100%'; $table->class = 'info_table'; $table->head = []; $table->head[0] = html_print_checkbox('all_delete', 0, false, true, false); $table->head[1] = __('Name'); $table->head[2] = __('Description'); $table->head[3] = ''.__('Action').''; $table->size = []; $table->size[0] = '20px'; $table->size[2] = '65%'; $table->size[3] = '15%'; $table->align = []; $table->align[3] = 'left'; $table->data = []; foreach ($resultModuleBlocksTable as $row) { $data = []; $data[0] = html_print_checkbox_extended('delete_multiple[]', $row['id_np'], false, false, '', 'class="check_delete"', true); $data[1] = ''.io_safe_output($row['name']).''; $data[2] = ui_print_truncate_text(io_safe_output($row['description']), 'description', true, true, true, '[…]'); $table->cellclass[][3] = 'action_buttons'; $data[3] = html_print_input_image( 'delete_profile', 'images/cross.png', $row['id_np'], '', true, ['onclick' => 'if (!confirm(\''.__('Are you sure?').'\')) return false;'] ); $data[3] .= html_print_input_image( 'export_profile', 'images/csv.png', $row['id_np'], '', true, ['title' => 'Export to CSV'] ); $data[3] = ''.html_print_image('images/cross.png', true, ['title' => __('Delete')]).''; $data[3] .= ''.html_print_image('images/csv.png', true, ['title' => __('Export to CSV')]).''; array_push($table->data, $data); } html_print_table($table); $output = '