Source of file ContactHelper.php
Size: 8,193 Bytes - Last Modified: 2021-12-23T10:24:26+00:00
/var/www/docs.ssmods.com/process/src/src/helpers/ContactHelper.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 | <?php namespace SilverCommerce\ContactAdmin\Helpers; use DateTime; use LogicException; use SilverStripe\Security\Member; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverCommerce\ContactAdmin\Model\Contact; use SilverStripe\Security\Group; class ContactHelper { use Injectable, Configurable; /** * The field that is shared between Members and Contacts. * * Defaults to "Email" * * @var string * @config */ private static $common_field = "Email"; /** * Fields that can be synced between members and contacts * * @var array */ private static $sync_fields = [ "FirstName", "Surname", "Company", "Phone", "Mobile", "Email" ]; /** * Automatically sync users and contacts on save and auto create contacts for existing users * * @var boolean */ private static $auto_sync = true; /** * Add codes for default groups linked user accounts are added to * * @var array */ private static $default_user_groups = [ 'contact_users' => 'Contact Users' ]; /** * @var Contact */ private $contact; /** * @var Member */ private $member; /** * Create a member from the provided contact * * @return Member */ public function findOrMakeMember() { $contact = $this->getContact(); if (empty($contact)) { throw new LogicException('Must set a Contact'); } // First off see if the member from the contact is available $member = $contact->Member(); $link = false; // If no member assigned, try to find one with matching field if (empty($member) || !$member->exists()) { $common_field = $this->config()->common_field; $member = Member::get()->find($common_field, $contact->{$common_field}); $link = true; } // Finally, if still no member found, then create a new one, link to the contact // and pass the relevent data if (empty($member)) { $member = Member::create(); self::pushFields($contact, $member); $member->write(); $link = true; } if ($link) { $contact->MemberID = $member->ID; $contact->write(); } return $member; } /** * Find or create a Contact from the provided member * * @return Contact */ public function findOrMakeContact() { $member = $this->getMember(); if (empty($member)) { throw new LogicException('Must set a Member'); } // First off see if the contact is available as relation $contact = $member->Contact(); $link = false; // If no member assigned, try to find one with matching field if (empty($contact) || !$contact->exists()) { $common_field = $this->config()->common_field; $contact = Contact::get()->find($common_field, $member->{$common_field}); $link = true; } // Finally, if still no member found, then create a new one, link to the contact // and pass the relevent data if (empty($contact)) { $contact = Contact::create(); self::pushFields($member, $contact); $link = true; } if ($link) { $contact->MemberID = $member->ID; $contact->write(); } return $contact; } /** * Update an associated member with the data from this contact * * @todo Currently sync is pretty basic (pushes data from one object to another). This could be more intilligent. * * @param bool $write Write the syncronised record * * @return void */ public function syncContactAndMember(bool $write = true) { $member = $this->getMember(); $contact = $this->getContact(); $changes = []; if (empty($member) || empty($contact)) { throw new LogicException('Must set a Member AND a Contact'); } // Find out which object just changed and sync in the correct direction $member_changed = $member->getChangedFields(); $contact_changed = $contact->getChangedFields(); if (count($contact_changed) > 0) { $changes = self::pushChangedFields($contact, $member); $obj_to_write = $contact; } elseif (count($member_changed) > 0) { $changes = self::pushChangedFields($member, $contact); $obj_to_write = $member; } if (count($changes) > 0) { $obj_to_write->write(); } return $this; } /** * Push any fields relevent fields changed on the origin obvject, to the destination, * if the destination is different. * * @var DataObject $origin * @var DataObject $destination * * @return array */ public static function pushChangedFields($origin, $destination) { $sync = self::config()->sync_fields; $changes = []; foreach ($origin->getChangedFields() as $field => $change) { if (in_array($field, $sync) && !empty($origin->$field) && $origin->$field != $destination->$field) { $destination->$field = $origin->$field; $changes[$field] = $origin->$field; } } return $changes; } /** * Push the field values from the origin object and the destination object * (if values do not match) * * Return a list of fields pushed * * @var DataObject $origin * @var DataObject $destination * * @return array */ public static function pushFields($origin, $destination) { $sync = self::config()->sync_fields; $changes = []; foreach ($sync as $field) { if ($origin->$field != $destination->$field) { $destination->$field = $origin->$field; $changes[$field] = $origin->$field; } } return $changes; } /** * Link the set member to all the groups specified via config * * Return the number of groups added * * @return int */ public function linkMemberToGroups() { $member = $this->getMember(); $groups = $this->config()->get('default_user_groups'); $count = 0; if (empty($member)) { throw new LogicException('Must set a Member'); } if (empty($groups)) { return $count; } foreach (array_keys($groups) as $code) { $group = Group::get()->filter('Code', $code)->first(); if (!empty($group)) { $member->Groups()->add($group); $count++; } } return $count; } /** * Get the value of contact. If not assigned directly, try to get from the member * * @return Contact */ public function getContact() { $contact = $this->contact; if (empty($contact) && !empty($this->getMember())) { $contact = $this->getMember()->Contact(); } return $contact; } /** * Set the value of contact * * @param Contact $contact * * @return self */ public function setContact(Contact $contact) { $this->contact = $contact; return $this; } /** * Get the value of member. If not assigned directly, try to get from Contact * * @return Member */ public function getMember() { $member = $this->member; if (empty($member) && !empty($this->getContact())) { $member = $this->getContact()->Member(); } return $member; } /** * Set the value of member * * @param Member $member * * @return self */ public function setMember(Member $member) { $this->member = $member; return $this; } } |