Source of file ContactMessage.php
Size: 10,059 Bytes - Last Modified: 2021-12-23T10:36:44+00:00
/var/www/docs.ssmods.com/process/src/src/Model/ContactMessage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 | <?php /** * This file is part of SilverWare. * * PHP version >=5.6.0 * * For full copyright and license information, please view the * LICENSE.md file that was distributed with this source code. * * @package SilverWare\Contact\Model * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-contact */ namespace SilverWare\Contact\Model; use SilverStripe\Control\Email\Email; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\TabSet; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataObject; use SilverWare\Security\CMSMainPermissions; use Page; /** * An extension of the data object class for a contact message. * * @package SilverWare\Contact\Model * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-contact */ class ContactMessage extends DataObject { use CMSMainPermissions; /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = 'Message'; /** * Human-readable plural name. * * @var string * @config */ private static $plural_name = 'Messages'; /** * Defines the default sort field and order for this object. * * @var string * @config */ private static $default_sort = '"Created" DESC'; /** * Defines the table name to use for this object. * * @var string * @config */ private static $table_name = 'SilverWare_ContactMessage'; /** * Maps field names to field types for this object. * * @var array * @config */ private static $db = [ 'FirstName' => 'Varchar(128)', 'LastName' => 'Varchar(128)', 'Email' => 'Varchar(255)', 'Phone' => 'Varchar(64)', 'Subject' => 'Varchar(255)', 'Message' => 'Text', 'Read' => 'Boolean' ]; /** * Defines the has-one associations for this object. * * @var array * @config */ private static $has_one = [ 'Parent' => Page::class ]; /** * Defines the many-many associations for this object. * * @var array * @config */ private static $many_many = [ 'Recipients' => ContactRecipient::class ]; /** * Defines the default values for the fields of this object. * * @var array * @config */ private static $defaults = [ 'Read' => 0 ]; /** * Defines the summary fields of this object. * * @var array * @config */ private static $summary_fields = [ 'RecipientNames', 'ReceivedFrom', 'Received' ]; /** * Defines the HTML template to use for emails. * * @var string */ protected $HTMLTemplate = self::class; /** * Answers a list of field objects for the CMS interface. * * @return FieldList */ public function getCMSFields() { // Mark Message as Read: $this->markAsRead(); // Create Field Tab Set: $fields = FieldList::create(TabSet::create('Root')); // Create Main Fields: $fields->addFieldsToTab( 'Root.Main', [ TextField::create( 'RecipientNames', $this->fieldLabel('RecipientNames') ), TextField::create( 'ReceivedFrom', $this->fieldLabel('ReceivedFrom') ), TextField::create( 'Received', $this->fieldLabel('Received') ), LiteralField::create( 'FormattedMessage', sprintf( '<div class="formatted-message">%s</div>', $this->dbObject('Message') ) ) ] ); // Create Phone Field (if required): if ($this->Phone) { $fields->insertAfter( TextField::create( 'Phone', $this->fieldLabel('Phone') ), 'ReceivedFrom' ); } // Create Subject Field (if required): if ($this->Subject) { $fields->insertBefore( TextField::create( 'Subject', $this->fieldLabel('Subject') ), 'FormattedMessage' ); } // Extend Field Objects: $this->extend('updateCMSFields', $fields); // Answer Field Objects: return $fields; } /** * Answers the labels for the fields of the receiver. * * @param boolean $includerelations Include labels for relations. * * @return array */ public function fieldLabels($includerelations = true) { // Obtain Field Labels (from parent): $labels = parent::fieldLabels($includerelations); // Define Field Labels: $labels['Phone'] = _t(__CLASS__ . '.PHONE', 'Phone'); $labels['Subject'] = _t(__CLASS__ . '.SUBJECT', 'Subject'); $labels['Received'] = _t(__CLASS__ . '.RECEIVED', 'Received'); $labels['ReceivedFrom'] = _t(__CLASS__ . '.FROM', 'From'); $labels['RecipientNames'] = _t(__CLASS__ . '.RECIPIENTS', 'Recipient(s)'); // Answer Field Labels: return $labels; } /** * Answers the title of the receiver for the CMS interface. * * @return string */ public function getTitle() { return sprintf( _t(__CLASS__ . 'CMSTITLE', 'Message from %s received %s'), $this->getFullName(), $this->getReceived() ); } /** * Answers the full name of the contact. * * @return string */ public function getFullName() { $names = []; if ($this->FirstName) { $names[] = $this->FirstName; } if ($this->LastName) { $names[] = $this->LastName; } return implode(' ', $names); } /** * Answers a nicely formatted string showing the date the message was received. * * @return string */ public function getReceived() { return $this->dbObject('Created')->Nice(); } /** * Answers a string containing the name and email address of the contact. * * @return string */ public function getReceivedFrom() { return $this->FullName ? sprintf('%s <%s>', $this->FullName, $this->Email) : $this->Email; } /** * Answers a string containing the recipient names of the message. * * @return string */ public function getRecipientNames() { if (!$this->Recipients()->count()) { return _t(__CLASS__ . 'NONE', 'None'); } $recipients = []; foreach ($this->Recipients() as $recipient) { $recipients[] = $recipient->Name; } return implode(', ', $recipients); } /** * Adds the given array of recipients to the receiver. * * @param array|ArrayAccess $recipients * * @return $this */ public function addRecipients($recipients) { $this->Recipients()->addMany($recipients); return $this; } /** * Defines the parent page for the receiver. * * @param Page $page * * @return $this */ public function setParent(Page $page) { $this->ParentID = $page->ID; return $this; } /** * Defines the value of the HTMLTemplate attribute. * * @param string $HTMLTemplate * * @return $this */ public function setHTMLTemplate($HTMLTemplate) { $this->HTMLTemplate = (string) $HTMLTemplate; return $this; } /** * Answers the value of the HTMLTemplate attribute. * * @return string */ public function getHTMLTemplate() { return $this->HTMLTemplate; } /** * Answers true if the message is marked as read. * * @return boolean */ public function isRead() { return (boolean) $this->Read; } /** * Sends the message as an email to the associated recipients. * * @return void */ public function send() { foreach ($this->Recipients() as $recipient) { $recipient->receive($this->toEmail()); } } /** * Marks the message as read. * * @return $this */ public function markAsRead() { if (!$this->isRead()) { $this->Read = 1; $this->write(); } return $this; } /** * Converts the message to an email object. * * @return Email */ public function toEmail() { // Create Email Object: $email = Email::create(); // Define Email Object: $email->setSubject($this->Subject); $email->setHTMLTemplate($this->HTMLTemplate); // Define Email Template Data: $email->setData([ 'Message' => $this ]); // Answer Email Object: return $email; } } |