Skip to content

Commit

Permalink
MDL-12093 : cleanup & simplify code to get popup windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
scyrma committed Nov 29, 2007
1 parent 095c404 commit 84f7875
Showing 1 changed file with 65 additions and 52 deletions.
117 changes: 65 additions & 52 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,12 @@ function stripos($haystack, $needle, $offset=0) {
}

/**
* This function will create a HTML link that will work on both
* Javascript and non-javascript browsers.
* This function will print a button/link/etc. form element
* that will work on both Javascript and non-javascript browsers.
* Relies on the Javascript function openpopup in javascript.php
*
* All parameters default to null, only $type and $url are mandatory.
*
* $url must be relative to home page eg /mod/survey/stuff.php
* @param string $url Web link relative to home page
* @param string $name Name to be assigned to the popup window
Expand All @@ -681,77 +683,88 @@ function stripos($haystack, $needle, $offset=0) {
* @param int $width Height to assign to popup window
* @param string $title Text to be displayed as popup page title
* @param string $options List of additional options for popup window
* @todo Add code examples and list of some options that might be used.
* @param boolean $return Should the link to the popup window be returned as a string (true) or printed immediately (false)?
* @param string $return If true, return as a string, otherwise print
* @param string $id id added to the element
* @param string $class class added to the element
* @return string
* @uses $CFG
*/
function link_to_popup_window ($url, $name='popup', $linkname='click here',
$height=400, $width=500, $title='Popup window',
$options='none', $return=false) {
function element_to_popup_window ($type=null, $url=null, $name=null, $linkname=null,
$height=400, $width=500, $title=null,
$options=null, $return=false, $id=null, $class=null) {

if (is_null($url)) { error('There must be an url to the popup. Can\'t create popup window.'); }

global $CFG;

if ($options == 'none') {
$options = 'menubar=0,location=0,scrollbars,resizable,width='. $width .',height='. $height;
if ($options == 'none') { /* 'none' is legacy, should be removed in v2.0 */
$options = null;
}
$fullscreen = 0;

if (!(strpos($url,$CFG->wwwroot) === false)) { // some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there.
$url = substr($url, strlen($CFG->wwwroot));
// add some sane default options for popup windows
if (!$options) { $options = 'menubar=0,location=0,scrollbars,resizable'; }
if ($width) { $options .= ',width='. $width; }
if ($height) { $options .= ',height='. $height; }
if ($id) { $id = ' id="'.$id.'" '; }
if ($class) { $class = ' class="'.$class.'" '; }

// get some default string, using the localized version of legacy defaults
if (!$name) { $name = get_string('popup'); }
if (!$linkname) { $linkname = get_string('click here'); }
if (!$title) { $title = get_string('Popup window'); }

$fullscreen = 0; // must be passed to openpopup
$element = '';

switch ($type) {
case 'button' :
$element = '<input type="button" name="'. $name .'" title="'. $title .'" value="'. $linkname .'" '. $id . $class .
"onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\" />\n";
break;
case 'link' :
// some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there.
if (!(strpos($url,$CFG->wwwroot) === false)) {
$url = substr($url, strlen($CFG->wwwroot));
}
$element = '<a title="'. s(strip_tags($title)) .'" href="'. $CFG->wwwroot . $url .'" '.
"onclick=\"this.target='$name'; return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>";
break;
default :
error('Undefined element - can\'t create popup window.');
break;
}

$link = '<a title="'. s(strip_tags($title)) .'" href="'. $CFG->wwwroot . $url .'" '.
"onclick=\"this.target='$name'; return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>";
if ($return) {
return $link;
return $element;
} else {
echo $link;
echo $element;
}
}

/**
* This function will print a button submit form element
* that will work on both Javascript and non-javascript browsers.
* Relies on the Javascript function openpopup in javascript.php
* Creates and displays (or returns) a link to a popup window, using element_to_popup_window function.
*
* $url must be relative to home page eg /mod/survey/stuff.php
* @param string $url Web link relative to home page
* @param string $name Name to be assigned to the popup window
* @param string $linkname Text to be displayed as web link
* @param int $height Height to assign to popup window
* @param int $width Height to assign to popup window
* @param string $title Text to be displayed as popup page title
* @param string $options List of additional options for popup window
* @param string $return If true, return as a string, otherwise print
* @return string
* @uses $CFG
* @return string html code to display a link to a popup window.
* @see element_to_popup_window()
*/
function button_to_popup_window ($url, $name='popup', $linkname='click here',
$height=400, $width=500, $title='Popup window', $options='none', $return=false,
$id='', $class='') {

global $CFG;
function link_to_popup_window ($url, $name=null, $linkname=null,
$height=400, $width=500, $title=null,
$options=null, $return=false) {

if ($options == 'none') {
$options = 'menubar=0,location=0,scrollbars,resizable,width='. $width .',height='. $height;
}
return element_to_popup_window('link', $url, $name, $linkname, $height, $width, $title, $options, $return, null, null);
}

if ($id) {
$id = ' id="'.$id.'" ';
}
if ($class) {
$class = ' class="'.$class.'" ';
}
$fullscreen = 0;
/**
* Creates and displays (or returns) a buttons to a popup window, using element_to_popup_window function.
*
* @return string html code to display a button to a popup window.
* @see element_to_popup_window()
*/
function button_to_popup_window ($url, $name=null, $linkname=null,
$height=400, $width=500, $title=null, $options=null, $return=false,
$id=null, $class=null) {

$button = '<input type="button" name="'.$name.'" title="'. $title .'" value="'. $linkname .' ..." '.$id.$class.
"onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\" />\n";
if ($return) {
return $button;
} else {
echo $button;
}
return element_to_popup_window('button', $url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class);
}


Expand Down

0 comments on commit 84f7875

Please sign in to comment.