<?php namespace JasonWilliams\Feed\Components;

use Cms\Classes\ComponentBase;
use JasonWilliams\Feed\Models\Channels;

class ChannelList extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name'        => 'Channel List',
            'description' => 'Displays a list of feed channels.'
        ];
    }

    public function defineProperties()
    {
      return [
        'includeEmpty' => [
            'title'       => 'Include Empty?',
            'description' => 'Include channels with no posts?',
            'type'        => 'checkbox',
            'default'     => true
        ]
      ];
    }

    public function onRun()
    {
        $this->page['channels'] = Channels::orderBy('id')->withCount('feeditems')->get();

        // If applicable, remove empty channels
        if (!$this->property('includeEmpty'))
        {
          foreach($this->page['channels'] as $key => $value)
          {
            if (!$value->feeditems_count) unset($this->page['channels'][$key]);
          }
        }
    }
}