Ergebnis 1 bis 7 von 7

Thema: Eigenes Widget, wie keyValueWizard

  1. #1
    Contao-Nutzer
    Registriert seit
    24.01.2016.
    Beiträge
    8

    Standard Eigenes Widget, wie keyValueWizard

    Hallo liebe Contaofreunde,

    es soll ein eigenes Widget erstellt werden, welches dem keyValueWizard ähnlich ist. Es sollen insgesamt 5 Inputfelder sein. Die eingegebenen Daten sollen in der Datenbank abgespeichert werden.

    Ich habe die keyValueWizard.php kopiert und verändert. Sie sieht folgendermaßen aus:

    PHP-Code:
    <?php

    class MyWizard extends \Widget
    {

        
    /**
         * Submit user input
         * @var boolean
         */
        
    protected $blnSubmitInput true;

        
    /**
         * Template
         * @var string
         */
        
    protected $strTemplate 'be_widget';


        
    /**
         * Add specific attributes
         *
         * @param string $strKey
         * @param mixed  $varValue
         */
        
    public function __set($strKey$varValue)
        {
            switch (
    $strKey)
            {
                case 
    'maxlength':
                    if (
    $varValue 0)
                    {
                        
    $this->arrAttributes['maxlength'] = $varValue;
                    }
                    break;

                default:
                    
    parent::__set($strKey$varValue);
                    break;
            }
        }


        
    /**
         * Validate the input and set the value
         */
        
    public function validate()
        {
            
    $mandatory $this->mandatory;
            
    $options $this->getPost($this->strName);

            
    // Check keys only (values can be empty)
            
    if (is_array($options))
            {
                foreach (
    $options as $key=>$option)
                {
                    
    // Unset empty rows
                    
    if ($option['key'] == '')
                    {
                        unset(
    $options[$key]);
                        continue;
                    }

                    
    $options[$key]['name'] = trim($option['key']);
                    
    $options[$key]['unit'] = trim($option['value']);
                    
    $options[$key]['unit_price'] = trim($option['value']);
                    
    $options[$key]['tax'] = trim($option['value']);
                    
    $options[$key]['total'] = trim($option['value']);

                    if (
    $options[$key]['key'] != '')
                    {
                        
    $this->mandatory false;
                    }
                }
            }

            
    $options array_values($options);
            
    $varInput $this->validator($options);

            if (!
    $this->hasErrors())
            {
                
    $this->varValue $varInput;
            }

            
    // Reset the property
            
    if ($mandatory)
            {
                
    $this->mandatory true;
            }
        }


        
    /**
         * Generate the widget and return it as string
         *
         * @return string
         */
        
    public function generate()
        {
            
    $arrButtons = array('copy''drag''up''down''delete');
            
    $strCommand 'cmd_' $this->strField;

            
    // Change the order
            
    if (\Input::get($strCommand) && is_numeric(\Input::get('cid')) && \Input::get('id') == $this->currentRecord)
            {
                
    $this->import('Database');

                switch (\
    Input::get($strCommand))
                {
                    case 
    'copy':
                        
    // $this->Database->prepare("UPDATE tl_my_modul SET title='new title' WHERE id=5")->execute();
                        
    array_insert($this->varValue, \Input::get('cid'), array($this->varValue[\Input::get('cid')]));
                        break;

                    case 
    'up':
                        
    $this->varValue array_move_up($this->varValue, \Input::get('cid'));
                        break;

                    case 
    'down':
                        
    $this->varValue array_move_down($this->varValue, \Input::get('cid'));
                        break;

                    case 
    'delete':
                        
    $this->varValue array_delete($this->varValue, \Input::get('cid'));
                        break;
                }

                
    $this->Database->prepare("UPDATE " $this->strTable " SET " $this->strField "=? WHERE id=?")
                               ->
    execute(serialize($this->varValue), $this->currentRecord);

                
    $this->redirect(preg_replace('/&(amp;)?cid=[^&]*/i'''preg_replace('/&(amp;)?' preg_quote($strCommand'/') . '=[^&]*/i''', \Environment::get('request'))));
            }

            
    // Make sure there is at least an empty array
            
    if (!is_array($this->varValue) || !$this->varValue[0])
            {
                
    $this->varValue = array(array(''));
            }

            
    // Initialize the tab index
            
    if (!\Cache::has('tabindex'))
            {
                \
    Cache::set('tabindex'1);
            }

            
    $tabindex = \Cache::get('tabindex');

            
    // Begin the table
            // $return = '<table class="tl_optionwizard" id="ctrl_'.$this->strId.'">
            
    $return '<table id="ctrl_'.$this->strId.'">
      <thead>
        <tr>'
    ;
            
    $titles $GLOBALS['TL_LANG']['tl_my_modul']['bill_title'];
            for(
    $i 0$i count($titles); $i++) 
            {
                
    $return .= '<th>' $titles[$i] . '</th>'
            }
            
    $return .= '
        </tr>
      </thead>
      <tbody class="sortable" data-tabindex="'
    .$tabindex.'">';

            
    // Add fields
            
    for ($i=0$c=count($this->varValue); $i<$c$i++)
            {
                
    $return .= '
        <tr>
          <td><input type="text" name="'
    .$this->strId.'['.$i.'][name]" id="'.$this->strId.'_key_'.$i.'" class="tl_text_2" tabindex="'.$tabindex++.'" value="'.specialchars($this->varValue[$i]['name']).'"'.$this->getAttributes().'></td>
          <td><input type="text" name="'
    .$this->strId.'['.$i.'][unit]" id="'.$this->strId.'_value_'.$i.'" class="tl_text_2" tabindex="'.$tabindex++.'" value="'.specialchars($this->varValue[$i]['unit']).'"'.$this->getAttributes().'></td>
          <td><input type="text" name="'
    .$this->strId.'['.$i.'][unit_price]" id="'.$this->strId.'_value_'.$i.'" class="tl_text_2" tabindex="'.$tabindex++.'" value="'.specialchars($this->varValue[$i]['unit_price']).'"'.$this->getAttributes().'></td>
          <td><input type="text" name="'
    .$this->strId.'['.$i.'][tax]" id="'.$this->strId.'_value_'.$i.'" class="tl_text_2" tabindex="'.$tabindex++.'" value="'.specialchars($this->varValue[$i]['tax']).'"'.$this->getAttributes().'></td>
          <td><input type="text" name="'
    .$this->strId.'['.$i.'][total]" id="'.$this->strId.'_value_'.$i.'" class="tl_text_2" tabindex="'.$tabindex++.'" value="'.specialchars($this->varValue[$i]['total']).'"'.$this->getAttributes().'></td>';

                
    // Add row buttons
                
    $return .= '
          <td style="white-space:nowrap;padding-left:3px">'
    ;

                foreach (
    $arrButtons as $button)
                {
                    
    $class = ($button == 'up' || $button == 'down') ? ' class="button-move"' '';

                    if (
    $button == 'drag')
                    {
                        
    $return .= \Image::getHtml('drag.gif''''class="drag-handle" title="' sprintf($GLOBALS['TL_LANG']['MSC']['move']) . '"');
                    }
                    else
                    {
                        
    $return .= '<a href="'.$this->addToUrl('&amp;'.$strCommand.'='.$button.'&amp;cid='.$i.'&amp;id='.$this->currentRecord).'"' $class ' title="'.specialchars($GLOBALS['TL_LANG']['MSC']['ow_'.$button]).'" onclick="Backend.keyValueWizard(this,\''.$button.'\',\'ctrl_'.$this->strId.'\');return false">'.\Image::getHtml($button.'.gif'$GLOBALS['TL_LANG']['MSC']['ow_'.$button]).'</a> ';
                    }
                }

                
    $return .= '</td>
        </tr>'
    ;
            }

            
    // Store the tab index
            
    \Cache::set('tabindex'$tabindex);

            return 
    $return.'
      </tbody>
      </table>'
    ;
        }
    }
    Die Inputfelder lauten name, unit, unit_price, tax, total.

    DCA

    PHP-Code:
    'bill_table' => array
    (
        
    'label'     => &$GLOBALS['TL_LANG']['tl_content']['text'],
        
    'inputType' => 'MyWizard',
        
    'exclude'   => true,
        
    'sql'       => "text NULL"

    Das Widget wird im Modul dargestellt, leider werden die Daten nicht abgespeichert.

    1. Wie kommen denn die Werte, die man in die Inputfelder eingeben hat in die Datenbank?
    2. Geht das über onload_callback ?
    3. Was kann man tun, damit die Daten abgespeichert werden?

    Vielleicht gibt es einen einfacheren Weg, dieses Widget zu erstellen. Zwei Dinge müssen erfüllt sein:

    1. Es müssen 5 Inputfelder sein
    2. Man muss duplizieren und löschen können. (Die Reihenfolge zu ändern ist nicht notwendig)

    (Bitte keine weiterführende Links senden, die nicht zum Ziel führen. Bitte auch keine Erweiterungsvorschläge.)

    Danke schon einmal im Voraus für jede hilfreiche Antwort!

    Viele Grüße,
    Peter

  2. #2
    Contao-Urgestein Avatar von folkfreund
    Registriert seit
    09.04.2010.
    Beiträge
    1.928

    Standard

    Hallo Pet,

    willkommen im Forum!

    Aus deiner Frage geht nicht wirklich hervor, was du alles gemacht hast. Wohin hast du z.B. die .php kopiert und wie bindest du sie ein?
    Zitat Zitat von Pet Beitrag anzeigen
    Bitte keine weiterführende Links senden, die nicht zum Ziel führen. Bitte auch keine Erweiterungsvorschläge.
    Diese Einschränkung ist überflüssig, denn
    • es ist nicht immer klar, ob ein Link 'zum Ziel führt'
    • es könnte ja eine Erweiterung geben, die deine Anforderung erfüllt oder bei der du abgucken könntest...

  3. #3
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    34.392
    Partner-ID
    10107

    Standard

    Hm, gibt es einen bestimmten Grund, warum du nicht einfach den MultiColumnWizard benutzt?

  4. #4
    Contao-Nutzer
    Registriert seit
    24.01.2016.
    Beiträge
    8

    Standard

    Hallo Spooky,

    danke für diesen Hinweis. Das ist genau, was ich brauche! Mithilfe von http://de.contaowiki.org/MultiColumnWizard

    steht in der DCA nun (erstmal zu Testzwecken):

    PHP-Code:
    'bill_table' => array
    (
        
    'label'     => &$GLOBALS['TL_LANG']['tl_my_modul']['text'],
        
    'inputType' => 'multiColumnWizard',
        
    'eval' => array
        (
            
    'columnFields' => array
            (
                
    'bt_name' => array 
                (
                    
    'label'  => &$GLOBALS['TL_LANG']['tl_content']['bt_name'],
                    
    'inputType' => 'text',
                    
    'eval' => array('style' => 'width:40px')
                ),
                
    'bt_unit' => array
                (
                    
    'label'  => &$GLOBALS['TL_LANG']['tl_content']['bt_unit'],
                    
    'inputType' => 'text',
                    
    'eval' => array('style' => 'width:40px')
                ),
                
    'bt_unit_price' => array
                (
                    
    'label'  => &$GLOBALS['TL_LANG']['tl_content']['bt_unit_price'],
                    
    'inputType' => 'text',
                    
    'eval' => array('style' => 'width:40px')
                ),
                
    'bt_tax' => array
                (
                    
    'label'  => &$GLOBALS['TL_LANG']['tl_content']['bt_tax'],
                    
    'inputType' => 'text',
                    
    'eval' => array('style' => 'width:40px')
                ),
                
    'bt_total' => array 
                (
                    
    'label'  => &$GLOBALS['TL_LANG']['tl_content']['bt_total'],
                    
    'inputType' => 'text',
                    
    'eval' => array('style' => 'width:40px')
                )
            )
        ),
        
    'sql'       => "text NULL"

    Nun kommt eine Fehlermeldung:
    Code:
    Warning: Illegal string offset 'bt_name' in composer/vendor/menatwork/contao-multicolumnwizard/system/modules/multicolumnwizard/MultiColumnWizard.php on line 479
    
    #0 composer/vendor/menatwork/contao-multicolumnwizard/system/modules/multicolumnwizard/MultiColumnWizard.php(479): __error(2, 'Illegal string ...', '/Applications/X...', 479, Array)
    #1 system/modules/core/library/Contao/Widget.php(684): MultiColumnWizard->generate()
    #2 system/modules/core/templates/backend/be_widget.html5(3): Contao\Widget->generateWithError(true)
    #3 system/modules/core/library/Contao/BaseTemplate.php(88): include('/Applications/X...')
    #4 system/modules/core/library/Contao/Widget.php(630): Contao\BaseTemplate->parse()
    #5 system/modules/core/classes/DataContainer.php(546): Contao\Widget->parse()
    #6 system/modules/core/drivers/DC_Table.php(1928): Contao\DataContainer->row('{title_legend},...')
    #7 system/modules/core/classes/Backend.php(650): Contao\DC_Table->edit()
    #8 system/modules/core/controllers/BackendMain.php(131): Contao\Backend->getBackendModule('my_modul')
    #9 contao/main.php(20): Contao\BackendMain->run()
    #10 {main}
    ...
    Hmm, komme nicht weiter. Verstehe in diesem Zusammenhang nicht 'Illegal string offset ...'.

    Wo ist das Problem? Und wie kann man es beheben?

    Danke schon einmal im Voraus für jede konstruktive Hilfe.

    Viele Grüße,
    Peter

  5. #5
    Community-Moderator
    Wandelndes Contao-Lexikon
    Avatar von Spooky
    Registriert seit
    12.04.2012.
    Ort
    Scotland
    Beiträge
    34.392
    Partner-ID
    10107

    Standard

    Welche Version von MCW hast du installiert?

  6. #6
    Contao-Fan Avatar von Thraile
    Registriert seit
    17.12.2009.
    Ort
    Detmold
    Beiträge
    501
    Partner-ID
    6306

    Standard

    Und welche Php Version benutzt du? Der MCW ist noch nicht PHP7 kompatible.

    Gesendet von meinem D5803 mit Tapatalk

  7. #7
    Contao-Nutzer
    Registriert seit
    24.01.2016.
    Beiträge
    8

    Standard

    Danke für die Hilfe!

    Es funktioniert jetzt alles. Das Problem lag an dem Feld 'bill_table', welches in der Datenbank (unter einem anderen Datentyp) abgespeichert war.
    Aus der Datenbank löschen und wieder neu anlegen!

    Viele Grüße,
    Peter
    Geändert von Pet (27.01.2016 um 06:56 Uhr)

Aktive Benutzer

Aktive Benutzer

Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)

Lesezeichen

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •