[dokuwiki] [PATCHES] refactored media upload form to use new form class

  • From: Michael Klier <chi@xxxxxxxxxxx>
  • To: dokuwiki@xxxxxxxxxxxxx
  • Date: Mon, 29 Oct 2007 15:51:34 +0100

Hi everyone,

the attaChed patch-set refactores the media upload form to make use the
new form class introduced a while ago. This effectively allows to add
custom form fields via action plugins which could be helpful in regards
to the also lately added MEDIA_UPLOAD_FINISH event. It also introduces
the possibility to set the form enctype via the form class constructor.

Another thing: the form class code was recently changed because the CSS
class edit was hardcoded - but - the way it is now it seems that one
can't interfere with the CSS classes of certain field types either. 

Take this function, responsible for the text fields, as example:

function form_makeTextField($name, $value='', $label=null, $id='', $class='', 
$attrs=array()) {
  if (is_null($label)) $label = $name;
  $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
                'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
  return array_merge($elem, $attrs);

if I use this function like this:

    $form->addElement(form_makeTextField('id', '', $lang['txt_filename'].':', 
'upload__name'), '', array('class'=>'edit test'));

the CSS class of the input field does not, as expected, change to
class="edit test" instead the resulting class is still "edit".

I have to confess that I don't understand this - because according to
the php docs the value of 'class' which is hold in $elem should be
overwritten by the one provided in $attrs on the array_merge() call.

Maybe someone else can either confirm this behaviour?

Best Regards,

Michael Klier

mail:   chi@xxxxxxxxxxx
www:    http://www.chimeric.de
icq:    206179334
jabber: chi@xxxxxxxxxxxxxxxxxx
key:    http://downloads.chimeric.de/chi.asc
key-id: 0x8308F551
New patches:

[allow enctype setting in form class
Michael Klier <chi@xxxxxxxxxxx>**20071029134359] {
hunk ./inc/form.php 38
+  // Change for special forms only
+  var $enctype = '';
hunk ./inc/form.php 64
-  function Doku_Form($id, $action=false, $method=false) {
+  function Doku_Form($id, $action=false, $method=false, $enctype=false) {
hunk ./inc/form.php 68
+    if ($enctype) $this->enctype = $enctype;
hunk ./inc/form.php 245
+    if (!empty($this->enctype)) print ' enctype="'.$this->enctype.'"';

[refactored media upload form
Michael Klier <chi@xxxxxxxxxxx>**20071029134739] {
hunk ./inc/media.php 564
-    ?>
-    <div class="upload"><?php echo $lang['mediaupload']?></div>
-    <form action="<?php echo DOKU_BASE?>lib/exe/mediamanager.php"
-          method="post" enctype="multipart/form-data" class="upload">
-      <fieldset>
-        <legend class="hidden"><?php echo $lang['btn_upload']?></legend>
-        <input type="hidden" name="ns" value="<?php echo hsc($ns)?>" />
-        <?php formSecurityToken();?>
-        <p>
-          <label for="upload__file"><?php echo $lang['txt_upload']?>:</label>
-          <input type="file" name="upload" class="edit" id="upload__file" />
-        </p>
+    print '<div class="upload">' . $lang['mediaupload'] . '</div>';
+    $form = new Doku_Form('dw__upload', DOKU_BASE.'lib/exe/mediamanager.php', 
false, 'multipart/form-data');
+    $form->addElement(formSecurityToken());
+    $form->addHidden('ns', hsc($ns));
+    $form->addElement(form_makeOpenTag('p'));
+    $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 
+    $form->addElement(form_makeCloseTag('p'));
+    $form->addElement(form_makeOpenTag('p'));
+    $form->addElement(form_makeTextField('id', '', $lang['txt_filename'].':', 
+    $form->addElement(form_makeButton('submit', '', $lang['btn_upload']));
+    $form->addElement(form_makeCloseTag('p'));
hunk ./inc/media.php 576
-        <p>
-          <label for="upload__name"><?php echo $lang['txt_filename']?>:</label>
-          <span class="nowrap">
-          <input type="text" name="id" class="edit" id="upload__name" /><input
-                 type="submit" class="button" value="<?php echo 
-                 accesskey="s" />
-          </span>
-        </p>
+    if($auth >= AUTH_DELETE){
+      $form->addElement(form_makeOpenTag('p'));
+      $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 
'dw__ow', 'check'));
+      $form->addElement(form_makeCloseTag('p'));
+    }
hunk ./inc/media.php 582
-        <?php if($auth >= AUTH_DELETE){?>
-            <p>
-              <input type="checkbox" name="ow" value="1" id="dw__ow" 
class="check" />
-              <label for="dw__ow" class="check"><?php echo 
-            </p>
-        <?php }?>
-      </fieldset>
-    </form>
-    <?php
+    html_form('upload', $form);
hunk ./inc/media.php 585

[CSS update for media form
Michael Klier <chi@xxxxxxxxxxx>**20071029135607] {
hunk ./lib/tpl/default/media.css 137
-#media__content form.upload {
+#media__content form#dw__upload {
hunk ./lib/tpl/default/media.css 142
-#media__content form.upload fieldset {
+#media__content form#dw__upload fieldset {
hunk ./lib/tpl/default/media.css 148
-#media__content form.upload p {
-    clear: left;
+#media__content form#dw__upload p {
hunk ./lib/tpl/default/media.css 154
-#media__content form.upload label {
-    float: left;
-    width: 30%;
-#media__content form.upload label.check {
+#media__content form#dw__upload label.check {
hunk ./lib/tpl/default/media.css 157
-#media__content form.upload input.check {
-    margin-left: 30%;
+    margin-left: 11.5em;

[added makeFileField method to form class
Michael Klier <chi@xxxxxxxxxxx>**20071029141013] {
hunk ./inc/form.php 430
+ * form_makeFileField
+ *
+ * Create a form element for a file input element with label
+ * 
+ * @see     form_makeField
+ * @author  Michael Klier <chi@xxxxxxxxxxx>
+ */
+function form_makeFileField($name, $label=null, $id='', $class='', 
$attrs=array()) {
+  if (is_null($label)) $label = $name;
+  $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
+                'id'=>$id, 'name'=>$name, 'class'=>'edit');
+  return array_merge($elem, $attrs);
hunk ./inc/form.php 742
+    $s .= '<br />';
+  return $s;
+ * form_filefield
+ *
+ * Print the HTML for a file input field.
+ *   _class     : class attribute used on the label tag
+ *   _text      : Text to display before the input. Not escaped
+ *   _maxlength : Allowed size in byte
+ *   _accept    : Accepted mime-type
+ * Other attributes are passed to buildAttributes() for the input tag
+ *
+ * @author  Michael Klier <chi@xxxxxxxxxxx>
+ */
+function form_filefield($attrs) {
+  $s = '<label class="'.$attrs['_class'].'"';
+  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
+  $s .= '><span>'.$attrs['_text'].'</span> ';
+  $s .= '<input type="file" '.buildAttributes($attrs,true);
+  if (!empty($attrs['_maxlength'])) $s .= ' 
+  if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
+  $s .= '/></label>';
+  if (preg_match('/(^| )block($| )/', $attrs['_class']))


[adds a second class to action buttons
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071026210930] 
[Galician language update
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071026205405] 
[Turkish update
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071026185916] 
[Translatable JavaScript strings for plugins
Gabriel Birke <Gabriel.Birke@xxxxxxxxx>**20071026131130
 Strings to be used in plugin provided JavaScript can now be put into the 
 lang.php files. It has to be stored as subkeys of $lang['js']. Eg the 
following in
 $lang['js']['foo'] = "bar";
 will be available from JavaScript code as
[Brasilian Portuguese Update
Frederico Goncalves Guimaraes <frederico@xxxxxxxxxxx>**20071026110711] 
[Malagasy translation added
infogasy.net <contact@xxxxxxxxxxxx>**20071026105801] 
[Latvian language update
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071019063310] 
[Allow HTML from inline RSS (typo fix)
Tom N Harris <tnharris@xxxxxxxxxxxxx>**20071015225606] 
[Fix border condition on recent change update
Tom N Harris <tnharris@xxxxxxxxxxxxx>**20071015225711] 
[header support for renderer plugins
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071016185626
 Renderer plugins now can store HTTP header information in 'format <mode>' which
 will be used to send their output. Also fixes a problem with loading cache 
[fixed Thai romanization
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071015170603] 
[tiny compatibility fix for FeedParser
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071015121248
 This should make older plugins working with a SimplePie Beta wor with 
 current SimplePie version
[Reduce memory requirement for indexer
Tom N Harris <tnharris@xxxxxxxxxxxxx>**20071012000327] 
[fixed URLs in plugin and user manager
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071013191842
 For the new plugin repository, plugins should use a page named after their
 class at wiki.splitbrain.org
Dennis Ploeger <develop@xxxxxxxxxxxxxx>**20071012135930
 Adds the putpage-method to the xmlrpc-server-code
[X-Sendfile support for fetch.php
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071008185019
 This patch enables the use of the X-Sendfile extension offered by certain
 webservers to deliver static files after running a dynamic script. This
 combines the flexibility of a PHP file to check for authorization, caching
 and resizing with the low memory footprint and high performance of static
 file delivery of the webserver.
 See http://blog.lighttpd.net/articles/2006/07/02/x-sendfile for details
[esperanto update
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20071007191109] 
[corrections for earlier MAIL_MESSAGE_SEND patch :)
Chris Smith <chris@xxxxxxxxxxxxx>**20070930124603] 
[fixes for p_get_metadata() & p_set_metadata (incl. resolution of FS#1254)
Chris Smith <chris@xxxxxxxxxxxxx>**20070930022739
 - add page existence check to prevent attempts to render metadata for 
non-existent pages (FS#1254)
 - add key & subkey existence checks to avoid PHP warnings
[add page_exists function (inc/pageutils.php)
Chris Smith <chris@xxxxxxxxxxxxx>**20070930021040
 bool page_exists($id, $rev='', $clean=true)
 checks wiki page existence, returns true if page exists, false if it doesn't.  
Parameters are the same as for wikiFN()
 $id    -- page id
 $rev   -- page revision
 $clean -- flag indicating whether or not $id requires cleaning
[ptln parameter spelling correction
Chris Smith <chris@xxxxxxxxxxxxx>**20070930020815] 
[Add MAIL_MESSAGE_SEND event (resolution of FS#1007)
Chris Smith <chris@xxxxxxxxxxxxx>**20070930014728
 Wrapper around DokuWiki's mail_send() function.
 For full details refer 
 Also see discussion at 
[Avoid duplicate attributes in forms
Tom N Harris <tnharris@xxxxxxxxxxxxx>**20071006195720] 
[Remove extraneous print statement
Tom N Harris <tnharris@xxxxxxxxxxxxx>**20071001192639] 
[TAG develsnap 2007-10-01
Andreas Gohr <andi@xxxxxxxxxxxxxx>**20070930230001] 
Patch bundle hash:

Other related posts:

  • » [dokuwiki] [PATCHES] refactored media upload form to use new form class