Source of file EditableFieldGroupEnd.php

Size: 2,530 Bytes - Last Modified: 2017-09-25T23:24:40+00:00

/var/www/docs.ssmods.com/process/src/code/Model/EditableFormField/EditableFieldGroupEnd.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
<?php

namespace SilverStripe\UserForms\Model\EditableFormField;

use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LabelField;
use SilverStripe\Security\Group;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;

/**
 * Specifies that this ends a group of fields
 */
class EditableFieldGroupEnd extends EditableFormField
{
    private static $belongs_to = [
        'Group' => EditableFieldGroup::class
    ];

    /**
     * Disable selection of group class
     *
     * @config
     * @var bool
     */
    private static $hidden = true;

    /**
     * Non-data type
     *
     * @config
     * @var bool
     */
    private static $literal = true;

    private static $table_name = 'EditableFieldGroupEnd';

    public function getCMSTitle()
    {
        $group = $this->Group();
        return _t(
            __CLASS__.'.FIELD_GROUP_END',
            '{group} end',
            [
                'group' => ($group && $group->exists()) ? $group->CMSTitle : Group::class
            ]
        );
    }

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->removeByName(['MergeField', 'Default', 'Validation', 'DisplayRules']);
        return $fields;
    }

    public function getInlineClassnameField($column, $fieldClasses)
    {
        return LabelField::create($column, $this->CMSTitle);
    }

    public function getInlineTitleField($column)
    {
        return HiddenField::create($column);
    }

    public function getFormField()
    {
        return null;
    }

    public function showInReports()
    {
        return false;
    }

    public function onAfterWrite()
    {
        parent::onAfterWrite();

        // If this is not attached to a group, find the first group prior to this
        // with no end attached
        $group = $this->Group();
        if (!($group && $group->exists()) && $this->ParentID) {
            $group = EditableFieldGroup::get()
                ->filter([
                    'ParentID' => $this->ParentID,
                    'Sort:LessThanOrEqual' => $this->Sort
                ])
                ->where('"EditableFieldGroup"."EndID" IS NULL OR "EditableFieldGroup"."EndID" = 0')
                ->sort('"Sort" DESC')
                ->first();

            // When a group is found, attach it to this end
            if ($group) {
                $group->EndID = $this->ID;
                $group->write();
            }
        }
    }
}