<?php

/**
 * User: akeyser
 */
class CheckboxTest extends PluginBase
{
    protected $storage = 'DbStorage';
    static protected $name = "Checkbox Test";
    static protected $description = "Test Plugin for Survey-Specific Checkbox Settings";

    /**
     * @param PluginManager $manager
     * @param int $id
     */
    public function __construct(PluginManager $manager, $id)
    {
        parent::__construct($manager, $id);
        $this->subscribe('beforeSurveySettings');
        $this->subscribe('newSurveySettings');
    }

    /**
     * This event is fired by the administration panel to gather extra settings
     * available for a survey.
     * The plugin should return setting meta data.
     * @internal param PluginEvent $event
     */
    public function beforeSurveySettings()
    {
        $event = $this->event;
        $event->set("surveysettings.{$this->id}", [
            'name' => get_class($this),
            'settings' => [
                'checkboxTest' => [
                    'type' => 'checkbox',
                    'label' => 'Checkbox',
                    'current' => $this->get('checkboxTest', 'Survey', $event->get('survey'))
                ]
            ]
        ]);
    }

    /**
     * Save the settings
     */
    public function newSurveySettings()
    {
        $event = $this->event;
        foreach ($event->get('settings') as $name => $value) {
            $this->set($name, $value, 'Survey', $event->get('survey'));
        }
    }
}
