Source of file DashboardPanel.php
Size: 8,179 Bytes - Last Modified: 2021-12-23T10:29:11+00:00
/var/www/docs.ssmods.com/process/src/src/panels/DashboardPanel.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 | <?php namespace UncleCheese\Dashboard; use SilverStripe\Control\Controller; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\TextField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; /** * Defines the DashboardPanel dataobject. All dashboard panels must descend from this class. * * @package dashboard * @author Uncle Cheese <unclecheese@leftandmain.com> */ class DashboardPanel extends DataObject { private static $table_name = 'DashboardPanel'; private static $db = [ 'Title' => 'Varchar(50)', 'PanelSize' => "Enum(array('small','normal','large'),'normal')", 'SortOrder' => 'Int' ]; private static $has_one = [ 'Member' => 'SilverStripe\Security\Member', 'SiteConfig' => 'SilverStripe\SiteConfig\SiteConfig' ]; private static $default_sort = "SortOrder ASC"; /** * @var string The size of the dashboard panel. Options: "small", "normal", and "large" */ private static $size = "normal"; /** * @var string The path to the icon image that represents this dashboard panel type */ private static $icon = "unclecheese/dashboard:images/dashboard-panel-default.png"; /** * @var int The "weight" of the dashboard panel when listed in the available panels. * Higher is lower in the list. */ private static $priority = 100; /** * @var bool Show the configure form after creating. Used for panels that require * configuration in order to show data */ private static $configure_on_create = false; /** * @var string The name of the template used for the contents of this panel. */ protected $template; /** * @var string the name of the template used for the wrapper of this panel */ protected $holderTemplate = "UncleCheese\Dashboard\DashboardPanel"; /** * @var string The name of the request handler class that the Dashboard controller * will use to communicate with a given panel */ protected $requestHandlerClass = DashboardPanelRequest::class; /** * Allows the panel to be added * * @return string */ public function registered() { if (is_bool(self::config()->enabled)) { return self::config()->enabled; } return true; } /** * Gets the template, falls back on a default value of the class name * * @return string */ protected function getTemplate() { return $this->template ? $this->template : static::class; } /** * Gets the holder template * * @return string */ public function getHolderTemplate() { return $this->holderTemplate; } /** * Gets the request handler class * * @return string */ public function getRequestHandlerClass() { return $this->requestHandlerClass; } /** * Essentially an abstract method. Every panel must have this method defined to provide * a title to the panel selection window * * @return string */ public function getLabel() { } /** * Essentially an abstract method. Every panel must have this method defined to provide * a description to the panel selection window * * @return string */ public function getDescription() { } /** * An accessor to the Dashboard controller * * @return Dashboard */ public function getDashboard() { return Injector::inst()->get(Dashboard::class); } /** * Renders the panel to its template * * @return \SilverStripe\ORM\FieldType\DBHTMLText */ public function render() { return $this->renderWith($this->holderTemplate); } /** * A template accessor for the icon of this panel * * @return string */ public function Icon() { return ModuleResourceLoader::resourceURL(static::config()->icon); } /** * Renders the inner contents of the panel. Similar to $Layout in pages. * * @return \SilverStripe\ORM\FieldType\DBHTMLText */ public function Content() { return $this->renderWith($this->getTemplate()); } /** * The link to this panel through the Dashboard controller * * @return string */ public function Link($action = null) { return Controller::join_links($this->getDashboard()->Link("panel/{$this->ID}"),$action); } /** * The link to delete this panel from the dashboard * * @return string */ public function DeleteLink() { return $this->Link("delete"); } /** * The link to create this panel on the dashboard * * @return string */ public function CreateLink() { return Controller::join_links($this->getDashboard()->Link("panel/new"), "?type=" . static::class); //TODO: Should the class name be escaped? At least Convert::raw2url() is not suitable because it removes backslashes completely, not escaping them. } /** * Template accessor for the $configure_on_create boolean * * @return boolean */ public function ShowConfigure() { return $this->config()->get('configure_on_create'); } /** * Gets the {@link FieldList} object that is used to configure the fields on this panel. * Similar to getCMSFields(). * * @return FieldList */ public function getConfiguration() { $default_size_title = ' '; //Cannot be an empty string because SilverStripe\i18n\i18n::_t() would yell that a default should be defined. So use a space as a workaround. return FieldList::create( DashboardButtonOptionsField::create("PanelSize",_t('UncleCheese\Dashboard\Dashboard.PANELSIZE', $default_size_title), [ 'small' => '', 'normal' => '', 'large' => '' ])->setSize("small"), TextField::create("Title", _t('Dashboard.TITLE','Title')) ); } /** * Gets the primary actions, which may appear in the top of the panel * * @return ArrayList */ public function getPrimaryActions() { return ArrayList::create([]); } /** * Gets the secondary actions, which may appear in the bottom of the panel * * @return ArrayList */ public function getSecondaryActions() { return ArrayList::create([]); } /** * Renders the entire panel. Similar to {@link FormField::FieldHolder()} * * @return \SilverStripe\ORM\FieldType\DBHTMLText */ public function PanelHolder() { return $this->renderWith($this->holderTemplate); } /** * For backward compatibility to the old static $size property. * * @return string */ public function Size() { return $this->PanelSize; } /** * Gets the configuration form for this panel * * @return Form */ public function Form() { return DashboardPanelRequest::create($this->getDashboard(), $this)->ConfigureForm(); } // /** // * Duplicates this panel. Drills down into the has_many relations // * // * We don't need this method anymore after upgrading to SS4. If any relations need to be duplicated, a DashboardPanel subclass should define a private static $cascade_duplicates config variable which should contain the relation names that should be duplicated. // * // * @return DashboardPanel // */ // public function duplicate($dowrite = true) { // $clone = parent::duplicate(true); // foreach($this->has_many() as $relationName => $relationClass) { // foreach($this->$relationName() as $relObject) { // $relClone = $relObject->duplicate(false); // $relClone->DashboardPanelID = $clone->ID; // $relClone->write(); // } // } // return $clone; // } public function canCreate($member = null, $context = []) { return Permission::check("CMS_ACCESS_DashboardAddPanels"); } public function canDelete($member = null) { $m = $member ? $member : Security::getCurrentUser(); return Permission::check("CMS_ACCESS_DashboardDeletePanels") && $this->MemberID == $m->ID; } public function canEdit($member = null) { $m = $member ? $member : Security::getCurrentUser(); return Permission::check("CMS_ACCESS_DashboardConfigurePanels") && $this->MemberID == $m->ID; } public function canView($member = null) { $m = $member ? $member : Security::getCurrentUser(); return Permission::check("CMS_ACCESS_Dashboard") && $this->MemberID == $m->ID; } public function IsConfigured() { return true; } } |