Contao-Camp 2024
Ergebnis 1 bis 4 von 4

Thema: tl_calendar_events um featured erweitern, List operations

  1. #1
    Contao-Nutzer
    Registriert seit
    08.01.2018.
    Beiträge
    9

    Standard tl_calendar_events um featured erweitern, List operations

    Hallo zusammen,

    ich möchte den Contao Kalender um das Feld "featured" analog zu den Contao News erweitern. Dazu habe ich das DCA und Language-File erweitert. Das Speichern über das Formular funktioniert einwandfrei. Was leider nicht funktioniert, ist die List operation, also das togglen des Featured Status über das Featured-Icon in der Event-Übersicht. Ich habe soweit alles von der tl_news übernommen und angepasst, das Icon wird mir auch angezeigt und der Ajax-Request gefeuert, leider wird der Status in der DB aber nicht aktualisiert. Der Request und die Response (die ja auch bei tl_news keine Ausgabe erzeugt) sieht exakt so aus wie bei tl_news. Rufe ich den Toggle-Link händisch als GET Request auf, z.B. http://my.domain.com/contao?do=calendar&table=tl_calendar_events&id=1&& fid=1&state=&rt=XXXXXXX&ref=zzzz_xx, funktioniert das Updaten des Datensatz. Nur beim Ajax-Request wird die Funktion toggleFeatured() nicht aufgerufen. Wenn ich darin als erstes eine json encodierte Ausgabe mache, wird nichts ausgegeben, anders als bei der tl_news wenn ich mir dort etwas ausgeben lasse. Ich habe alles mehrfach mit der tl_news verglichen und finde einfach keinen Ansatz mehr.
    Ich bin über jede Hilfe dankbar!

    Anbei noch meine tl_calendar_events.php:

    PHP-Code:
    <?php
    // Add fields to tl_module
    $GLOBALS['TL_DCA']['tl_calendar_events']['fields']['featured'] = array
    (
                
    'label'                   => &$GLOBALS['TL_LANG']['tl_calendar_events']['featured'],
                
    'exclude'                 => true,
                
    'filter'                  => true,
                
    'inputType'               => 'checkbox',
                
    'eval'                    => array('tl_class'=>'w50'),
                
    'sql'                     => "char(1) NOT NULL default ''"
    );

    $GLOBALS['TL_DCA']['tl_calendar_events']['list']['operations']['feature'] = array
    (
        
    'label'               => &$GLOBALS['TL_LANG']['tl_calendar_events']['feature'],
        
    'icon'                => 'featured.svg',
        
    'attributes'          => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleFeatured(this,%s)"',
        
    'button_callback'     => array('tl_calendar_events_um''iconFeatured')
    );

    $GLOBALS['TL_DCA']['tl_calendar_events']['palettes']['default'] = str_replace("{expert_legend:hide},cssClass,noComments""{expert_legend:hide},cssClass,noComments,featured"$GLOBALS['TL_DCA']['tl_calendar_events']['palettes']['default']);

    class 
    tl_calendar_events_um extends tl_calendar_events {


        
    /**
         * Return the "feature/unfeature element" button
         *
         * @param array  $row
         * @param string $href
         * @param string $label
         * @param string $title
         * @param string $icon
         * @param string $attributes
         *
         * @return string
         */
        
    public function iconFeatured($row$href$label$title$icon$attributes)
        {
            if (
    strlen(Input::get('fid')))
            {
                
    $this->toggleFeatured(Input::get('fid'), (Input::get('state') == 1), (@func_get_arg(12) ?: null));
                
    $this->redirect($this->getReferer());
            }

            
    // Check permissions AFTER checking the fid, so hacking attempts are logged
            
    if (!$this->User->hasAccess('tl_calendar_events::featured''alexf'))
            {
                return 
    '';
            }

            
    $href .= '&amp;fid=' $row['id'] . '&amp;state=' . ($row['featured'] ? '' 1);

            if (!
    $row['featured'])
            {
                
    $icon 'featured_.svg';
            }

            return 
    '<a href="' $this->addToUrl($href) . '" title="' StringUtil::specialchars($title) . '"' $attributes '>' Image::getHtml($icon$label'data-state="' . ($row['featured'] ? 0) . '"') . '</a> ';
        }

        
    /**
         * Feature/unfeature a news item
         *
         * @param integer       $intId
         * @param boolean       $blnVisible
         * @param DataContainer $dc
         *
         * @throws Contao\CoreBundle\Exception\AccessDeniedException
         */
        
    public function toggleFeatured($intId$blnVisibleDataContainer $dc=null)
        {

            
    // Check permissions to edit
            
    Input::setGet('id'$intId);
            
    Input::setGet('act''feature');
            
    $this->checkPermission();

            
    // Check permissions to feature
            
    if (!$this->User->hasAccess('tl_calendar_events::featured''alexf'))
            {
                throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to feature/unfeature news item ID ' $intId '.');
            }

            
    $objVersions = new Versions('tl_calendar_events'$intId);
            
    $objVersions->initialize();

            
    // Trigger the save_callback
            
    if (is_array($GLOBALS['TL_DCA']['tl_calendar_events']['fields']['featured']['save_callback']))
            {
                foreach (
    $GLOBALS['TL_DCA']['tl_calendar_events']['fields']['featured']['save_callback'] as $callback)
                {
                    if (
    is_array($callback))
                    {
                        
    $this->import($callback[0]);
                        
    $blnVisible $this->{$callback[0]}->{$callback[1]}($blnVisible$dc);
                    }
                    elseif (
    is_callable($callback))
                    {
                        
    $blnVisible $callback($blnVisible$this);
                    }
                }
            }

            
    // Update the database
            
    $this->Database->prepare("UPDATE tl_calendar_events SET tstamp=" time() . ", featured='" . ($blnVisible '') . "' WHERE id=?")
                           ->
    execute($intId);

            
    $objVersions->create();
        }

        
    /**
         * Check permissions to edit table tl_calendar_events
         *
         * @throws Contao\CoreBundle\Exception\AccessDeniedException
         */
        
    public function checkPermission()
        {
            
    $bundles System::getContainer()->getParameter('kernel.bundles');

            
    // HOOK: comments extension required
            
    if (!isset($bundles['ContaoCommentsBundle']))
            {
                
    $key array_search('allowComments'$GLOBALS['TL_DCA']['tl_calendar_events']['list']['sorting']['headerFields']);
                unset(
    $GLOBALS['TL_DCA']['tl_calendar_events']['list']['sorting']['headerFields'][$key]);
            }

            if (
    $this->User->isAdmin)
            {
                return;
            }

            
    // Set root IDs
            
    if (empty($this->User->calendars) || !is_array($this->User->calendars))
            {
                
    $root = array(0);
            }
            else
            {
                
    $root $this->User->calendars;
            }

            
    $id strlen(Input::get('id')) ? Input::get('id') : CURRENT_ID;

            
    // Check current action
            
    switch (Input::get('act'))
            {
                case 
    'paste':
                case 
    'select':
                    
    // Check CURRENT_ID here (see #247)
                    
    if (!in_array(CURRENT_ID$root))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access calendar ID ' $id '.');
                    }
                    break;

                case 
    'create':
                    if (!
    strlen(Input::get('pid')) || !in_array(Input::get('pid'), $root))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to create events in calendar ID ' Input::get('pid') . '.');
                    }
                    break;

                case 
    'cut':
                case 
    'copy':
                    if (!
    in_array(Input::get('pid'), $root))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' Input::get('act') . ' event ID ' $id ' to calendar ID ' Input::get('pid') . '.');
                    }
                    
    // no break

                
    case 'edit':
                case 
    'show':
                case 
    'delete':
                case 
    'toggle':
                case 
    'feature':
                    
    $objCalendar $this->Database->prepare("SELECT pid FROM tl_calendar_events WHERE id=?")
                                                  ->
    limit(1)
                                                  ->
    execute($id);

                    if (
    $objCalendar->numRows 1)
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Invalid event ID ' $id '.');
                    }

                    if (!
    in_array($objCalendar->pid$root))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' Input::get('act') . ' event ID ' $id ' of calendar ID ' $objCalendar->pid '.');
                    }
                    break;

                case 
    'editAll':
                case 
    'deleteAll':
                case 
    'overrideAll':
                case 
    'cutAll':
                case 
    'copyAll':
                    if (!
    in_array($id$root))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access calendar ID ' $id '.');
                    }

                    
    $objCalendar $this->Database->prepare("SELECT id FROM tl_calendar_events WHERE pid=?")
                                                  ->
    execute($id);

                    
    /** @var Symfony\Component\HttpFoundation\Session\SessionInterface $objSession */
                    
    $objSession System::getContainer()->get('session');

                    
    $session $objSession->all();
                    
    $session['CURRENT']['IDS'] = array_intersect((array) $session['CURRENT']['IDS'], $objCalendar->fetchEach('id'));
                    
    $objSession->replace($session);
                    break;

                default:
                    if (
    strlen(Input::get('act')))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Invalid command "' Input::get('act') . '".');
                    }

                    if (!
    in_array($id$root))
                    {
                        throw new 
    Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access calendar ID ' $id '.');
                    }
                    break;
            }
        }


    }
    LG derMatze
    Geändert von derMatze82 (29.04.2020 um 12:09 Uhr) Grund: Code hinzugefügt

  2. #2
    Administratorin Avatar von lucina
    Registriert seit
    19.06.2009.
    Ort
    Kiel (DE)
    Beiträge
    7.332
    Partner-ID
    152
    User beschenken
    Wunschliste
    Contao-Projekt unterstützen

    Support Contao

    Standard

    Du könntest Dir das Vorgehen allerdings auch bei der vorhanden Extension von Dennis abschauen:

    https://github.com/ErdmannFreunde/calendar_feature

    Das funktioniert dann so: https://github.com/ErdmannFreunde/ca...dar_events.php
    Geändert von lucina (29.04.2020 um 17:10 Uhr)

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

  4. #4
    Contao-Nutzer
    Registriert seit
    08.01.2018.
    Beiträge
    9

    Standard

    Danke, mit der haste_ajax_operation klappt es einwandfrei.

    Zitat Zitat von lucina Beitrag anzeigen
    Du könntest Dir das Vorgehen allerdings auch bei der vorhanden Extension von Dennis abschauen:

    https://github.com/ErdmannFreunde/calendar_feature

    Das funktioniert dann so: https://github.com/ErdmannFreunde/ca...dar_events.php

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
  •