View file upload/src/addons/AddonsLab/Core/Xf2/Admin/Field/AbstractRow.php

File size: 4.97Kb
<?php
/** 
This software is furnished under a license and may be used and copied
only  in  accordance  with  the  terms  of such  license and with the
inclusion of the above copyright notice.  This software  or any other
copies thereof may not be provided or otherwise made available to any
other person.  No title to and  ownership of the  software is  hereby
transferred.                                                         
                                                                     
You may not reverse  engineer, decompile, defeat  license  encryption
mechanisms, or  disassemble this software product or software product
license.  AddonsLab may terminate this license if you don't comply with
any of these terms and conditions.  In such event,  licensee  agrees 
to return licensor  or destroy  all copies of software  upon termination 
of the license.
*/


namespace AddonsLab\Core\Xf2\Admin\Field;

use AddonsLab\Core\Xf2\Admin\Form;

abstract class AbstractRow
{
    const type_checkbox = 'checkbox';
    const type_onoff = 'onoff';
    const type_onofftextbox = 'onofftextbox';
    const type_radio = 'radio';
    const type_select = 'select';
    const type_spinbox = 'spinbox';
    const type_textarea = 'textarea';
    const type_textbox = 'textbox';
    const type_datebox = 'datebox';
    const type_username = 'username';

    /**
     * @var Form
     */
    protected $form;

    /**
     * For rows that only have a label and custom content
     */
    const type_info = 'info';
    const type_custom = 'custom';

    protected $id;

    protected $type;

    protected $template_name;

    protected $title;

    protected $explain;

    protected $relation_name;

    protected $getter_name;

    protected $format_params = array();

    /**
     * AbstractRow constructor.
     * @param $type
     */
    public function __construct($id, $title = null, $explain = null)
    {
        $this->id = $id;
        $this->title = $title;
        $this->explain = $explain;
    }

    public function getFormatParam($paramName)
    {
        return isset($this->format_params[$paramName]) ? $this->format_params[$paramName] : '';
    }

    /**
     * @param string $relation_name
     * The name of entity in format "TestEntity" or "Provider\Id:TestEntity".
     * Will be used for saving info into a relation instead of the main entity
     */
    public function setRelationName($relation_name)
    {
        $this->relation_name = $relation_name;
        return $this;
    }

    /**
     * @param string $getter_name
     * @return AbstractRow
     * Full getter string that returns the current value for the field. Should include relation name if needed
     */
    public function setGetterName($getter_name)
    {
        $this->getter_name = $getter_name;
        return $this;
    }

    /**
     * @return string
     * The name of getter that returns the value of the field. Can be name of DB column,
     * or full getter name like Relation.getter_in_relation
     */
    public function getGetterName()
    {
        if ($this->getter_name) {
            $getter = $this->getter_name;
        } else {
            $getter = $this->id;
        }

        if ($this->relation_name && strpos($getter, '.') === false) {
            $getter = $this->relation_name . '.' . $getter;
        }

        return $getter;
    }

    public function getTemplate()
    {
        if ($this->type === self::type_custom) {
            if (!$this->template_name) {
                return $this->id; // by default we will look for a template with the same name as the ID of the field
            }

            return $this->template_name;
        }

        // the name of type and the template match
        return $this->type;
    }

    public function setTemplateName($templateName)
    {
        if ($this->type !== self::type_custom) {
            throw new \RuntimeException("Custom template name is supported only for custom field type.");
        }

        $this->template_name = $templateName;

        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param mixed $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * @return mixed
     */
    public function getExplain()
    {
        return $this->explain;
    }

    /**
     * @param mixed $explain
     */
    public function setExplain($explain)
    {
        $this->explain = $explain;
    }

    /**
     * @return string
     */
    public function getRelationName()
    {
        return $this->relation_name;
    }

    /**
     * @return Form
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * @param Form $form
     * @return AbstractRow
     */
    public function setForm($form)
    {
        $this->form = $form;
        return $this;
    }
    
    public function getType()
    {
        return $this->type;
    }
}