widgetName = JText::translate('VBO_W_STICKYN_TITLE'); $this->widgetDescr = JText::translate('VBO_W_STICKYN_DESCR'); $this->widgetId = basename(__FILE__, '.php'); /** * Define widget and icon and style name. * * @since 1.15.0 (J) - 1.5.0 (WP) */ $this->widgetIcon = ''; $this->widgetStyleName = 'yellow'; // load widget's settings $this->widgetSettings = $this->loadSettings(); // determine if the operating system is MacOS or iOS $ua = JFactory::getApplication()->input->server->getString('HTTP_USER_AGENT', ''); if (stripos($ua, 'windows') === false && (stripos($ua, 'mac') !== false || stripos($ua, 'iphone') !== false || stripos($ua, 'ipad') !== false)) { $this->isMac = true; } } /** * Custom method for this widget only to update one sticky note. * The method is called by the admin controller through an AJAX request. * The visibility should be public, it should not exit the process, and * any content sent to output will be returned to the AJAX response. * * @param int $force_note_index the new index can be forced to insert a new note. * @param int $force_instance the widget's instance to force. * @param string $force_txt the text of the sticky note to force. * * @return void it may output a log with the new widget instance assigned. */ public function updateStickyNote($force_note_index = null, $force_instance = null, $force_txt = null) { $note_instance = VikRequest::getInt('note_instance', -1, 'request'); $note_txt = VikRequest::getString('note_txt', '', 'request', VIKREQUEST_ALLOWRAW); $note_index = VikRequest::getInt('note_index', 0, 'request'); if ($force_note_index !== null && $force_note_index >= 0) { // use the forced index instead $note_index = $force_note_index; } if ($force_instance !== null && $force_instance >= 0) { // use the forced instance instead $note_instance = $force_instance; } if (is_string($force_txt) && strlen($force_txt)) { // use the forced text instead $note_txt = $force_txt; } // make sure the settings of the widget are an array if (!is_array($this->widgetSettings)) { $this->widgetSettings = array(); } // create the new sticky note object $username = $this->getLoggedUserName(); $sticky_note = new stdClass; $sticky_note->html = $note_txt; $sticky_note->ts = time(); if (!empty($username)) { $sticky_note->user = $username; } // eventually append debugging result $result_log = ''; // eventually append the new widget instance assigned for newly added widgets via AJAX $new_instance_response = ''; if (!count($this->widgetSettings)) { // push the first sticky note as the first instance of this widget $this->widgetSettings = array( array($sticky_note) ); $result_log = 'pushing the first sticky note as the first instance of the widget'; $new_instance_response = '[instance=0]'; } elseif (isset($this->widgetSettings[$note_instance])) { // this instance was saved already if (isset($this->widgetSettings[$note_instance][$note_index])) { // we already have this exact sticky note index, so we replace it $this->widgetSettings[$note_instance][$note_index] = $sticky_note; $result_log = 'replacing requested note index in existing widget instance'; } else { // we push a new sticky note array_push($this->widgetSettings[$note_instance], $sticky_note); $result_log = 'adding a new note to the existing widget instance'; } } else { // this is a new instance of the widget, which has settings already - push the new instance and the new sticky note array_push($this->widgetSettings, array($sticky_note)); $result_log = 'pushing a new instance and note to the settings'; $new_instance_response = '[instance=' . (count($this->widgetSettings) - 1) . ']'; } // update widget's settings $this->updateSettings(json_encode($this->widgetSettings)); echo 'e4j.ok' . (!empty($result_log) ? '(' . $result_log . ')' : '') . $new_instance_response; } /** * Custom method for this widget only to delete one sticky note. * The method is called by the admin controller through an AJAX request. * The visibility should be public, it should not exit the process, and * any content sent to output will be returned to the AJAX response. */ public function deleteStickyNote() { $note_instance = VikRequest::getInt('note_instance', -1, 'request'); $note_index = VikRequest::getInt('note_index', 0, 'request'); // make sure the settings of the widget are an array if (!is_array($this->widgetSettings)) { echo 'e4j.error.no settings found'; return; } // make sure the instance of the widget exists if (!isset($this->widgetSettings[$note_instance]) || !is_array($this->widgetSettings[$note_instance])) { echo 'e4j.error.instance not found'; return; } // make sure the index of the note exists if (!isset($this->widgetSettings[$note_instance][$note_index])) { echo 'e4j.error.note index not found, maybe it was never updated before'; return; } // splice the array to remove the requested note array_splice($this->widgetSettings[$note_instance], $note_index, 1); // update widget's settings $this->updateSettings(json_encode($this->widgetSettings)); echo 'e4j.ok'; } /** * Custom method for this widget only to sort one sticky note. * The method is called by the admin controller through an AJAX request. * The visibility should be public, it should not exit the process, and * any content sent to output will be returned to the AJAX response. */ public function sortStickyNote() { $note_instance = VikRequest::getInt('note_instance', -1, 'request'); $note_index_new = VikRequest::getInt('note_index_new', 0, 'request'); $note_index_old = VikRequest::getInt('note_index_old', 0, 'request'); if ($note_index_new == $note_index_old) { // nothing to do, as notes can be sorted only within the same instance echo 'e4j.error.same position given for sticky note'; return; } // make sure the settings of the widget are an array if (!is_array($this->widgetSettings)) { echo 'e4j.error.no settings found'; return; } // make sure the instance of the widget exists if (!isset($this->widgetSettings[$note_instance]) || !is_array($this->widgetSettings[$note_instance])) { echo 'e4j.error.instance not found'; return; } // make sure the old index of the note exists if (!isset($this->widgetSettings[$note_instance][$note_index_old])) { // check if this note was moved before getting saved, so as soon as it was added if (count($this->widgetSettings[$note_instance]) == $note_index_old) { // append the newly created (empty) note before moving it $this->updateStickyNote($note_index_old, $note_instance); // reload the widget's settings after they have been updated with the new note $this->widgetSettings = $this->loadSettings(); // proceed below with moving the sticky note } else { // unable to proceed echo 'e4j.error.original note index not found, maybe it was never updated before'; return; } } // make sure the new index can fit if ($note_index_new > (count($this->widgetSettings[$note_instance]) - 1)) { echo 'e4j.error.new index exceeds the highest position available'; return; } // move the sticky note from the old index to the new index $extracted = array_splice($this->widgetSettings[$note_instance], $note_index_old, 1); array_splice($this->widgetSettings[$note_instance], $note_index_new, 0, $extracted); // update widget's settings $this->updateSettings(json_encode($this->widgetSettings)); echo 'e4j.ok'; } /** * Preload the necessary assets. * * @return void */ public function preload() { // JS lang def JText::script('VBO_STICKYN_TITLE'); JText::script('VBO_STICKYN_TEXT'); JText::script('VBO_STICKYN_TEXT2'); JText::script('VBO_STICKYN_CUSTOMURI'); JText::script('VBO_WIDGETS_CONFRMELEM'); } public function render(?VBOMultitaskData $data = null) { // increase widget's instance counter static::$instance_counter++; // check whether the widget is being rendered via AJAX when adding it through the customizer $is_ajax = $this->isAjaxRendering(); // check whether we are in the multitask panel $is_multitask = $this->isMultitaskRendering(); // check whether the widget requires settings $needs_settings = !$is_ajax; $data_instance = !$is_ajax ? static::$instance_counter : '-1'; if ($is_multitask && $is_ajax) { $guess_inst_counter = $this->guessMultitaskStickyInstance($data); if ($guess_inst_counter !== false) { // force the loading of the guessed widget instance's settings static::$instance_counter = $guess_inst_counter; $data_instance = $guess_inst_counter; $needs_settings = true; } } // generate a unique ID for the sticky notes wrapper instance $wrapper_instance = !$is_ajax ? static::$instance_counter : rand(); $wrapper_id = 'vbo-widget-sticky-' . $wrapper_instance; // load the settings for this specific instance of the widget $instance_settings = array(); if ($needs_settings && is_array($this->widgetSettings) && isset($this->widgetSettings[static::$instance_counter])) { $instance_settings = is_array($this->widgetSettings[static::$instance_counter]) ? $this->widgetSettings[static::$instance_counter] : array(); } ?>
widgetSettings) || !count($this->widgetSettings)) { // nothing to guess if this widget has got no saved settings return false; } if (!is_object($data)) { // multitask data object must be set return false; } // the page must be set in the multitask object $vbo_page = $data->getPage(); if (empty($vbo_page)) { // nothing to guess if no current page set return false; } // get the map for the current page $page_map = VikBooking::getAdminWidgetsInstance()->getMultitaskingMap($vbo_page, $whole = false); if (!is_array($page_map) || !count($page_map)) { // the multitask panel of this page has got no widgets saved, return the first index for settings return 0; } // count how many widgets of this type are already on this page $guessed_index = 0; foreach ($page_map as $widget_type) { if ($widget_type == $this->getIdentifier()) { $guessed_index++; } } // return the guessed index, which will load the next hypothetical instance return $guessed_index; } }