Skip to content

Commit f4ecfd8

Browse files
committed
Merge pull request #6 from phproberto/jm-contact
[imp] check for existing contacts and increment name & alias if required
2 parents 54e7e3c + 8086cf6 commit f4ecfd8

File tree

4 files changed

+128
-62
lines changed

4 files changed

+128
-62
lines changed

administrator/components/com_contact/models/contact.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,8 @@ protected function prepareTable($table)
586586
$user = JFactory::getUser();
587587

588588
$table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
589-
$table->alias = JApplication::stringURLSafe($table->alias);
590589

591-
if (empty($table->alias))
592-
{
593-
$table->alias = JApplication::stringURLSafe($table->name);
594-
}
590+
$table->generateAlias();
595591

596592
if (empty($table->id))
597593
{

administrator/components/com_contact/tables/contact.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,9 @@ public function check()
160160
return false;
161161
}
162162

163-
if (empty($this->alias))
164-
{
165-
$this->alias = $this->name;
166-
}
167-
$this->alias = JApplication::stringURLSafe($this->alias);
168-
if (trim(str_replace('-', '', $this->alias)) == '')
169-
{
170-
$this->alias = JFactory::getDate()->format("Y-m-d-H-i-s");
171-
}
163+
// Generate a valid alias
164+
$this->generateAlias();
165+
172166
/** check for valid category */
173167
if (trim($this->catid) == '')
174168
{
@@ -214,4 +208,27 @@ public function check()
214208

215209
return true;
216210
}
211+
212+
/**
213+
* Generate a valid alias from title / date.
214+
* Remains public to be able to check for duplicated alias before saving
215+
*
216+
* @return string
217+
*/
218+
public function generateAlias()
219+
{
220+
if (empty($this->alias))
221+
{
222+
$this->alias = $this->name;
223+
}
224+
225+
$this->alias = JApplication::stringURLSafe($this->alias);
226+
227+
if (trim(str_replace('-', '', $this->alias)) == '')
228+
{
229+
$this->alias = JFactory::getDate()->format("Y-m-d-H-i-s");
230+
}
231+
232+
return $this->alias;
233+
}
217234
}

administrator/language/en-GB/en-GB.plg_user_contactcreator.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
44
; Note : All ini files need to be saved as UTF-8 - No BOM
55

6-
PLG_CONTACTCREATOR_ERR_FAILED_CREATING_CONTACT="Contact automatic creation failed because another contact from this category has the same alias. Please contact a site administrator."
6+
PLG_CONTACTCREATOR_ERR_FAILED_CREATING_CONTACT="Automatic contact creation failed. Please contact a site administrator."
77
PLG_CONTACTCREATOR_ERR_NO_CATEGORY="Contact automatic creation failed because contact category is not set!"
88
PLG_CONTACTCREATOR_FIELD_AUTOMATIC_WEBPAGE_DESC="A formatted string to automatically generate a contact's web page. [name] is replaced with the name, [username] is replaced with the username, [userid] is replaced with the user ID and [email] is replaced with the email"
99
PLG_CONTACTCREATOR_FIELD_AUTOMATIC_WEBPAGE_LABEL="Automatic Webpage"

plugins/user/contactcreator/contactcreator.php

Lines changed: 100 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,88 +28,141 @@ class PlgUserContactCreator extends JPlugin
2828
*/
2929
protected $autoloadLanguage = true;
3030

31+
/**
32+
* Utility method to act on a user after it has been saved.
33+
*
34+
* This method creates a contact for the saved user
35+
*
36+
* @param array $user Holds the new user data.
37+
* @param boolean $isnew True if a new user is stored.
38+
* @param boolean $success True if user was succesfully stored in the database.
39+
* @param string $msg Message.
40+
*
41+
* @return void
42+
*
43+
* @since 1.6
44+
*/
3145
public function onUserAfterSave($user, $isnew, $success, $msg)
3246
{
47+
// If the user wasn't stored we don't resync
3348
if (!$success)
3449
{
35-
return false; // if the user wasn't stored we don't resync
50+
return false;
3651
}
3752

53+
// If the user isn't new we don't sync
3854
if (!$isnew)
3955
{
40-
return false; // if the user isn't new we don't sync
56+
return false;
4157
}
4258

43-
// ensure the user id is really an int
59+
// Ensure the user id is really an int
4460
$user_id = (int) $user['id'];
4561

62+
// If the user id appears invalid then bail out just in case
4663
if (empty($user_id))
4764
{
48-
return false; // if the user id appears invalid then bail out just in case
65+
return false;
4966
}
5067

51-
$category = $this->params->get('category', 0);
68+
$categoryId = $this->params->get('category', 0);
5269

53-
if (empty($category))
70+
if (empty($categoryId))
5471
{
5572
JError::raiseWarning('', JText::_('PLG_CONTACTCREATOR_ERR_NO_CATEGORY'));
5673

5774
return false;
5875
}
5976

60-
$db = JFactory::getDbo();
61-
// grab the contact ID for this user; note $user_id is cleaned above
62-
$db->setQuery('SELECT id FROM #__contact_details WHERE user_id = '. $user_id);
63-
$id = $db->loadResult();
77+
if ($contact = $this->getContactTable())
78+
{
79+
/**
80+
* Try to pre-load a contact for this user. Apparently only possible if other plugin creates it
81+
* Note: $user_id is cleaned above
82+
*/
83+
if (!$contact->load(array('user_id' => (int) $user_id)))
84+
{
85+
$contact->published = $this->params->get('autopublish', 0);
86+
}
6487

65-
JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_contact/tables');
66-
$contact = JTable::getInstance('contact', 'ContactTable');
88+
$contact->name = $user['name'];
89+
$contact->user_id = $user_id;
90+
$contact->email_to = $user['email'];
91+
$contact->catid = $categoryId;
92+
$contact->language = '*';
93+
$contact->generateAlias();
6794

68-
if (!$contact)
69-
{
70-
return false;
71-
}
95+
// Check if the contact already exists to generate new name & alias if required
96+
if ($contact->id == 0)
97+
{
98+
list($name, $alias) = $this->generateAliasAndName($contact->alias, $contact->name, $categoryId);
7299

73-
if ($id)
74-
{
75-
$contact->load($id);
76-
}
77-
elseif ($this->params->get('autopublish', 0))
78-
{
79-
$contact->published = 1;
80-
}
100+
$contact->name = $name;
101+
$contact->alias = $alias;
102+
}
81103

82-
$contact->name = $user['name'];
83-
$contact->user_id = $user_id;
84-
$contact->email_to = $user['email'];
85-
$contact->catid = $category;
86-
$contact->language = '*';
104+
$autowebpage = $this->params->get('autowebpage', '');
87105

88-
$autowebpage = $this->params->get('autowebpage', '');
106+
if (!empty($autowebpage))
107+
{
108+
// Search terms
109+
$search_array = array('[name]', '[username]', '[userid]', '[email]');
89110

90-
if (!empty($autowebpage))
91-
{
92-
// search terms
93-
$search_array = array('[name]', '[username]', '[userid]', '[email]');
94-
// replacement terms, urlencoded
95-
$replace_array = array_map('urlencode', array($user['name'], $user['username'], $user['id'], $user['email']));
96-
// now replace it in together
97-
$contact->webpage = str_replace($search_array, $replace_array, $autowebpage);
98-
}
111+
// Replacement terms, urlencoded
112+
$replace_array = array_map('urlencode', array($user['name'], $user['username'], $user['id'], $user['email']));
99113

100-
if ($contact->check())
101-
{
102-
$result = $contact->store();
114+
// Now replace it in together
115+
$contact->webpage = str_replace($search_array, $replace_array, $autowebpage);
116+
}
117+
118+
if ($contact->check() && $contact->store())
119+
{
120+
return true;
121+
}
103122
}
104123

105-
if (!(isset($result)) || !$result)
124+
JError::raiseWarning('', JText::_('PLG_CONTACTCREATOR_ERR_FAILED_CREATING_CONTACT'));
125+
126+
return false;
127+
}
128+
129+
/**
130+
* Method to change the name & alias if alias is already in use
131+
*
132+
* @param string $alias The alias.
133+
* @param string $name The name.
134+
* @param integer $categoryId Category identifier
135+
*
136+
* @return array Contains the modified title and alias.
137+
*
138+
* @since 3.3
139+
*/
140+
protected function generateAliasAndName($alias, $name, $categoryId)
141+
{
142+
$table = $this->getContactTable();
143+
144+
while ($table->load(array('alias' => $alias, 'catid' => $categoryId)))
106145
{
107-
if ($contact->load(array('alias' => $contact->alias, 'catid' => $category)) && ($contact->id != $id || $id == 0))
146+
if ($name == $table->name)
108147
{
109-
JError::raiseWarning('', JText::_('PLG_CONTACTCREATOR_ERR_FAILED_CREATING_CONTACT'));
110-
111-
return false;
148+
$name = JString::increment($name);
112149
}
150+
151+
$alias = JString::increment($alias, 'dash');
113152
}
153+
154+
return array($name, $alias);
155+
}
156+
157+
/**
158+
* Get an instance of the contact table
159+
*
160+
* @return ContactTableContact
161+
*/
162+
protected function getContactTable()
163+
{
164+
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_contact/tables');
165+
166+
return JTable::getInstance('contact', 'ContactTable');
114167
}
115168
}

0 commit comments

Comments
 (0)