]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/htmloutputter.php
catch exceptions from snapshot
[quix0rs-gnu-social.git] / lib / htmloutputter.php
index b3b2a5ae4205eace525e5fab4bfc319e5fe0fb6e..2091c6e2ca4d60fd4020f2ca91cfbcb09558c909 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Laconica, the distributed open-source microblogging tool
+ * StatusNet, the distributed open-source microblogging tool
  *
  * Low-level generator for HTML
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Output
- * @package   Laconica
- * @author    Evan Prodromou <evan@controlyourself.ca>
- * @author    Sarven Capadisli <csarven@controlyourself.ca>
- * @copyright 2008 Control Yourself, Inc.
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @author    Sarven Capadisli <csarven@status.net>
+ * @copyright 2008 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://laconi.ca/
+ * @link      http://status.net/
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
 require_once INSTALLDIR.'/lib/xmloutputter.php';
 
-define('PAGE_TYPE_PREFS',
-       'text/html,application/xhtml+xml,'.
-       'application/xml;q=0.3,text/xml;q=0.2');
+// Can include XHTML options but these are too fragile in practice.
+define('PAGE_TYPE_PREFS', 'text/html');
 
 /**
  * Low-level generator for HTML
@@ -47,13 +46,14 @@ define('PAGE_TYPE_PREFS',
  * HTML-creation class.
  *
  * @category Output
- * @package  Laconica
- * @author   Evan Prodromou <evan@controlyourself.ca>
- * @author   Sarven Capadisli <csarven@controlyourself.ca>
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @author   Sarven Capadisli <csarven@status.net>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://laconi.ca/
+ * @link     http://status.net/
+ *
  * @see      Action
- * @see      HTMLOutputter
+ * @see      XMLOutputter
  */
 
 class HTMLOutputter extends XMLOutputter
@@ -67,7 +67,7 @@ class HTMLOutputter extends XMLOutputter
      * @param boolean $indent Whether to indent output, default true
      */
 
-    function __construct($output='php://output', $indent=true)
+    function __construct($output='php://output', $indent=null)
     {
         parent::__construct($output, $indent);
     }
@@ -100,27 +100,56 @@ class HTMLOutputter extends XMLOutputter
             $type = common_negotiate_type($cp, $sp);
 
             if (!$type) {
-                common_user_error(_('This page is not available in a '.
-                                    'media type you accept'), 406);
-                exit(0);
+                throw new ClientException(_('This page is not available in a '.
+                                            'media type you accept'), 406);
             }
         }
 
         header('Content-Type: '.$type);
 
-        $this->startXML('html',
-                        '-//W3C//DTD XHTML 1.0 Strict//EN',
-                        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
-
-        // FIXME: correct language for interface
+        $this->extraHeaders();
+        if (preg_match("/.*\/.*xml/", $type)) {
+            // Required for XML documents
+            $this->xw->startDocument('1.0', 'UTF-8');
+        }
+        $this->xw->writeDTD('html',
+                            '-//W3C//DTD XHTML 1.0 Strict//EN',
+                            'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
 
-        $language = common_language();
+        $language = $this->getLanguage();
 
         $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
                                           'xml:lang' => $language,
                                           'lang' => $language));
     }
 
+    function getLanguage()
+    {
+        // FIXME: correct language for interface
+        return common_language();
+    }
+
+    /**
+    *  Ends an HTML document
+    *
+    *  @return void
+    */
+    function endHTML()
+    {
+        $this->elementEnd('html');
+        $this->endXML();
+    }
+
+    /**
+    *  To specify additional HTTP headers for the action
+    *
+    *  @return void
+    */
+    function extraHeaders()
+    {
+        // Needs to be overloaded
+    }
+
     /**
      * Output an HTML text input element
      *
@@ -143,20 +172,17 @@ class HTMLOutputter extends XMLOutputter
 
     function input($id, $label, $value=null, $instructions=null)
     {
-        $this->elementStart('p');
         $this->element('label', array('for' => $id), $label);
         $attrs = array('name' => $id,
                        'type' => 'text',
-                       'class' => 'input_text',
                        'id' => $id);
         if ($value) {
-            $attrs['value'] = htmlspecialchars($value);
+            $attrs['value'] = $value;
         }
         $this->element('input', $attrs);
         if ($instructions) {
-            $this->element('span', 'input_instructions', $instructions);
+            $this->element('p', 'form_guide', $instructions);
         }
-        $this->elementEnd('p');
     }
 
     /**
@@ -180,13 +206,12 @@ class HTMLOutputter extends XMLOutputter
     function checkbox($id, $label, $checked=false, $instructions=null,
                       $value='true', $disabled=false)
     {
-        $this->elementStart('p');
         $attrs = array('name' => $id,
                        'type' => 'checkbox',
                        'class' => 'checkbox',
                        'id' => $id);
         if ($value) {
-            $attrs['value'] = htmlspecialchars($value);
+            $attrs['value'] = $value;
         }
         if ($checked) {
             $attrs['checked'] = 'checked';
@@ -196,14 +221,13 @@ class HTMLOutputter extends XMLOutputter
         }
         $this->element('input', $attrs);
         $this->text(' ');
-        $this->element('label', array('class' => 'checkbox_label',
+        $this->element('label', array('class' => 'checkbox',
                                       'for' => $id),
                        $label);
         $this->text(' ');
         if ($instructions) {
-            $this->element('span', 'input_instructions', $instructions);
+            $this->element('p', 'form_guide', $instructions);
         }
-        $this->elementEnd('p');
     }
 
     /**
@@ -228,7 +252,6 @@ class HTMLOutputter extends XMLOutputter
     function dropdown($id, $label, $content, $instructions=null,
                       $blank_select=false, $selected=null)
     {
-        $this->elementStart('p');
         $this->element('label', array('for' => $id), $label);
         $this->elementStart('select', array('id' => $id, 'name' => $id));
         if ($blank_select) {
@@ -237,7 +260,7 @@ class HTMLOutputter extends XMLOutputter
         foreach ($content as $value => $option) {
             if ($value == $selected) {
                 $this->element('option', array('value' => $value,
-                                               'selected' => $value),
+                                               'selected' => 'selected'),
                                $option);
             } else {
                 $this->element('option', array('value' => $value), $option);
@@ -245,9 +268,8 @@ class HTMLOutputter extends XMLOutputter
         }
         $this->elementEnd('select');
         if ($instructions) {
-            $this->element('span', 'input_instructions', $instructions);
+            $this->element('p', 'form_guide', $instructions);
         }
-        $this->elementEnd('p');
     }
 
     /**
@@ -257,15 +279,14 @@ class HTMLOutputter extends XMLOutputter
      *
      * @param string $id    element ID, must be unique on page
      * @param string $value hidden element value, default null
+     * @param string $name  name, if different than ID
      *
      * @return void
-     *
-     * @todo add a $name parameter
      */
 
-    function hidden($id, $value)
+    function hidden($id, $value, $name=null)
     {
-        $this->element('input', array('name' => $id,
+        $this->element('input', array('name' => ($name) ? $name : $id,
                                       'type' => 'hidden',
                                       'id' => $id,
                                       'value' => $value));
@@ -285,7 +306,6 @@ class HTMLOutputter extends XMLOutputter
 
     function password($id, $label, $instructions=null)
     {
-        $this->elementStart('p');
         $this->element('label', array('for' => $id), $label);
         $attrs = array('name' => $id,
                        'type' => 'password',
@@ -293,9 +313,8 @@ class HTMLOutputter extends XMLOutputter
                        'id' => $id);
         $this->element('input', $attrs);
         if ($instructions) {
-            $this->element('span', 'input_instructions', $instructions);
+            $this->element('p', 'form_guide', $instructions);
         }
-        $this->elementEnd('p');
     }
 
     /**
@@ -304,21 +323,121 @@ class HTMLOutputter extends XMLOutputter
      * @param string $id    element ID, must be unique on page
      * @param string $label text of the button
      * @param string $cls   class of the button, default 'submit'
+     * @param string $name  name, if different than ID
      *
      * @return void
      *
      * @todo add a $name parameter
      */
 
-    function submit($id, $label, $cls='submit')
+    function submit($id, $label, $cls='submit', $name=null, $title=null)
     {
-        $this->elementStart('p');
         $this->element('input', array('type' => 'submit',
                                       'id' => $id,
-                                      'name' => $id,
+                                      'name' => ($name) ? $name : $id,
                                       'class' => $cls,
-                                      'value' => $label));
-        $this->elementEnd('p');
+                                      'value' => $label,
+                                      'title' => $title));
+    }
+
+    /**
+     * output a script (almost always javascript) tag
+     *
+     * @param string $src          relative or absolute script path
+     * @param string $type         'type' attribute value of the tag
+     *
+     * @return void
+     */
+    function script($src, $type='text/javascript')
+    {
+        if(Event::handle('StartScriptElement', array($this,&$src,&$type))) {
+            $url = parse_url($src);
+            if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
+            {
+                $src = common_path($src) . '?version=' . STATUSNET_VERSION;
+            }
+            $this->element('script', array('type' => $type,
+                                                   'src' => $src),
+                                   ' ');
+            Event::handle('EndScriptElement', array($this,$src,$type));
+        }
+    }
+
+    /**
+     * output a script (almost always javascript) tag with inline
+     * code.
+     *
+     * @param string $code         code to put in the script tag
+     * @param string $type         'type' attribute value of the tag
+     *
+     * @return void
+     */
+
+    function inlineScript($code, $type='text/javascript')
+    {
+        if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
+            $this->elementStart('script', array('type' => $type));
+            if($type == 'text/javascript') {
+                $this->raw('/*<![CDATA[*/ '); // XHTML compat
+            }
+            $this->raw($code);
+            if($type == 'text/javascript') {
+                $this->raw(' /*]]>*/'); // XHTML compat
+            }
+            $this->elementEnd('script');
+            Event::handle('EndInlineScriptElement', array($this,$code,$type));
+        }
+    }
+
+    /**
+     * output a css link
+     *
+     * @param string $src     relative path within the theme directory, or an absolute path
+     * @param string $theme        'theme' that contains the stylesheet
+     * @param string media         'media' attribute of the tag
+     *
+     * @return void
+     */
+    function cssLink($src,$theme=null,$media=null)
+    {
+        if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
+            $url = parse_url($src);
+            if( empty($url->scheme) && empty($url->host) && empty($url->query) && empty($url->fragment))
+            {
+                if(file_exists(Theme::file($src,$theme))){
+                   $src = Theme::path($src, $theme);
+                }else{
+                   $src = common_path($src);
+                }
+                $src.= '?version=' . STATUSNET_VERSION;
+            }
+            $this->element('link', array('rel' => 'stylesheet',
+                                    'type' => 'text/css',
+                                    'href' => $src,
+                                    'media' => $media));
+            Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
+        }
+    }
+
+    /**
+     * output a style (almost always css) tag with inline
+     * code.
+     *
+     * @param string $code         code to put in the style tag
+     * @param string $type         'type' attribute value of the tag
+     * @param string $media        'media' attribute value of the tag
+     *
+     * @return void
+     */
+
+    function style($code, $type = 'text/css', $media = null)
+    {
+        if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
+            $this->elementStart('style', array('type' => $type, 'media' => $media));
+            $this->raw($code);
+            $this->elementEnd('style');
+            Event::handle('EndStyleElement', array($this,$code,$type,$media));
+        }
     }
 
     /**
@@ -338,7 +457,6 @@ class HTMLOutputter extends XMLOutputter
 
     function textarea($id, $label, $content=null, $instructions=null)
     {
-        $this->elementStart('p');
         $this->element('label', array('for' => $id), $label);
         $this->element('textarea', array('rows' => 3,
                                          'cols' => 40,
@@ -346,8 +464,24 @@ class HTMLOutputter extends XMLOutputter
                                          'id' => $id),
                        ($content) ? $content : '');
         if ($instructions) {
-            $this->element('span', 'input_instructions', $instructions);
+            $this->element('p', 'form_guide', $instructions);
         }
-        $this->elementEnd('p');
+    }
+
+    /**
+    * Internal script to autofocus the given element on page onload.
+    *
+    * @param string $id element ID, must refer to an existing element
+    *
+    * @return void
+    *
+    */
+    function autofocus($id)
+    {
+        $this->inlineScript(
+                   ' $(document).ready(function() {'.
+                   ' var el = $("#' . $id . '");'.
+                   ' if (el.length) { el.focus(); }'.
+                   ' });');
     }
 }