[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Plugin 5 * @subpackage User.contactcreator 6 * 7 * @copyright (C) 2010 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 10 * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace 11 */ 12 13 use Joomla\CMS\Language\Text; 14 use Joomla\CMS\Plugin\CMSPlugin; 15 use Joomla\Component\Contact\Administrator\Table\ContactTable; 16 use Joomla\String\StringHelper; 17 18 // phpcs:disable PSR1.Files.SideEffects 19 \defined('_JEXEC') or die; 20 // phpcs:enable PSR1.Files.SideEffects 21 22 /** 23 * Class for Contact Creator 24 * 25 * A tool to automatically create and synchronise contacts with a user 26 * 27 * @since 1.6 28 */ 29 class PlgUserContactCreator extends CMSPlugin 30 { 31 /** 32 * Load the language file on instantiation. 33 * 34 * @var boolean 35 * @since 3.1 36 */ 37 protected $autoloadLanguage = true; 38 39 /** 40 * Application Instance 41 * 42 * @var \Joomla\CMS\Application\CMSApplication 43 * @since 4.0.0 44 */ 45 protected $app; 46 47 /** 48 * Database Driver Instance 49 * 50 * @var \Joomla\Database\DatabaseDriver 51 * @since 4.0.0 52 */ 53 protected $db; 54 55 /** 56 * Utility method to act on a user after it has been saved. 57 * 58 * This method creates a contact for the saved user 59 * 60 * @param array $user Holds the new user data. 61 * @param boolean $isnew True if a new user is stored. 62 * @param boolean $success True if user was successfully stored in the database. 63 * @param string $msg Message. 64 * 65 * @return void 66 * 67 * @since 1.6 68 */ 69 public function onUserAfterSave($user, $isnew, $success, $msg): void 70 { 71 // If the user wasn't stored we don't resync 72 if (!$success) { 73 return; 74 } 75 76 // If the user isn't new we don't sync 77 if (!$isnew) { 78 return; 79 } 80 81 // Ensure the user id is really an int 82 $user_id = (int) $user['id']; 83 84 // If the user id appears invalid then bail out just in case 85 if (empty($user_id)) { 86 return; 87 } 88 89 $categoryId = $this->params->get('category', 0); 90 91 if (empty($categoryId)) { 92 $this->app->enqueueMessage(Text::_('PLG_CONTACTCREATOR_ERR_NO_CATEGORY'), 'error'); 93 94 return; 95 } 96 97 if ($contact = $this->getContactTable()) { 98 /** 99 * Try to pre-load a contact for this user. Apparently only possible if other plugin creates it 100 * Note: $user_id is cleaned above 101 */ 102 if (!$contact->load(array('user_id' => (int) $user_id))) { 103 $contact->published = $this->params->get('autopublish', 0); 104 } 105 106 $contact->name = $user['name']; 107 $contact->user_id = $user_id; 108 $contact->email_to = $user['email']; 109 $contact->catid = $categoryId; 110 $contact->access = (int) $this->app->get('access'); 111 $contact->language = '*'; 112 $contact->generateAlias(); 113 114 // Check if the contact already exists to generate new name & alias if required 115 if ($contact->id == 0) { 116 list($name, $alias) = $this->generateAliasAndName($contact->alias, $contact->name, $categoryId); 117 118 $contact->name = $name; 119 $contact->alias = $alias; 120 } 121 122 $autowebpage = $this->params->get('autowebpage', ''); 123 124 if (!empty($autowebpage)) { 125 // Search terms 126 $search_array = array('[name]', '[username]', '[userid]', '[email]'); 127 128 // Replacement terms, urlencoded 129 $replace_array = array_map('urlencode', array($user['name'], $user['username'], $user['id'], $user['email'])); 130 131 // Now replace it in together 132 $contact->webpage = str_replace($search_array, $replace_array, $autowebpage); 133 } 134 135 if ($contact->check() && $contact->store()) { 136 return; 137 } 138 } 139 140 $this->app->enqueueMessage(Text::_('PLG_CONTACTCREATOR_ERR_FAILED_CREATING_CONTACT'), 'error'); 141 } 142 143 /** 144 * Method to change the name & alias if alias is already in use 145 * 146 * @param string $alias The alias. 147 * @param string $name The name. 148 * @param integer $categoryId Category identifier 149 * 150 * @return array Contains the modified title and alias. 151 * 152 * @since 3.2.3 153 */ 154 protected function generateAliasAndName($alias, $name, $categoryId) 155 { 156 $table = $this->getContactTable(); 157 158 while ($table->load(array('alias' => $alias, 'catid' => $categoryId))) { 159 if ($name === $table->name) { 160 $name = StringHelper::increment($name); 161 } 162 163 $alias = StringHelper::increment($alias, 'dash'); 164 } 165 166 return array($name, $alias); 167 } 168 169 /** 170 * Get an instance of the contact table 171 * 172 * @return ContactTable|null 173 * 174 * @since 3.2.3 175 */ 176 protected function getContactTable() 177 { 178 return $this->app->bootComponent('com_contact')->getMVCFactory()->createTable('Contact', 'Administrator', ['dbo' => $this->db]); 179 } 180 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |