@@ -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