Html::getConfirmationOnActionScript PHP Method

getConfirmationOnActionScript() static public method

Get confirmation on button or link before action
static public getConfirmationOnActionScript ( $string, $additionalactions = '' ) : confirmation
$string string to display or array of string for using multilines
$additionalactions string additional actions to do on success confirmation (default '')
return confirmation script
    static function getConfirmationOnActionScript($string, $additionalactions = '')
    {
        if (!is_array($string)) {
            $string = array($string);
        }
        $string = Toolbox::addslashes_deep($string);
        $additionalactions = trim($additionalactions);
        $out = "";
        $multiple = false;
        $close_string = '';
        // Manage multiple confirmation
        foreach ($string as $tab) {
            if (is_array($tab)) {
                $multiple = true;
                $out .= "if (window.confirm('";
                $out .= implode('\\n', $tab);
                $out .= "')){ ";
                $close_string .= "return true;} else { return false;}";
            }
        }
        // manage simple confirmation
        if (!$multiple) {
            $out .= "if (window.confirm('";
            $out .= implode('\\n', $string);
            $out .= "')){ ";
            $close_string .= "return true;} else { return false;}";
        }
        $out .= $additionalactions . (substr($additionalactions, -1) != ';' ? ';' : '') . $close_string;
        return $out;
    }

Usage Example

Example #1
0
 /**
  * Creates a submit button element. This method will generate input elements that
  * can be used to submit, and reset forms by using $options. Image submits can be created by supplying an
  * image option
  *
  * @since version 0.85
  *
  * @param $caption          caption of the input
  * @param $options    Array of options.
  *     - image : will use a submit image input
  *     - `confirm` JavaScript confirmation message.
  *     - `confirmaction` optional action to do on confirmation
  *
  * @return string A HTML submit button
  **/
 static function submit($caption, $options = array())
 {
     $image = false;
     if (isset($options['image'])) {
         if (preg_match('/\\.(jpg|jpe|jpeg|gif|png|ico)$/', $options['image'])) {
             $image = $options['image'];
         }
         unset($options['image']);
     }
     // Set default class to submit
     if (!isset($options['class'])) {
         $options['class'] = 'submit';
     }
     if (isset($options['confirm'])) {
         if (!empty($options['confirm'])) {
             $confirmMessage = $options['confirm'];
             $confirmAction = '';
             if (isset($options['confirmaction'])) {
                 if (!empty($options['confirmaction'])) {
                     $confirmAction = $options['confirmaction'];
                 }
                 unset($options['confirmaction']);
             }
             $options['onclick'] = Html::getConfirmationOnActionScript($options['confirm'], $confirmAction);
         }
         unset($options['confirm']);
     }
     if ($image) {
         $options['title'] = $caption;
         $options['alt'] = $caption;
         return sprintf('<input type="image" src="%s" %s>', Html::cleanInputText($image), Html::parseAttributes($options));
     }
     return sprintf('<input type="submit" value="%s" %s>', Html::cleanInputText($caption), Html::parseAttributes($options));
 }
Html