This question has been answered by BobRay. See the first response.
$user = $modx->getObject('customUser', $userID);
$message = 'Someone viewed your profile!';
$user->sendNotification( 'online', $message );
$user->sendNotification( 'email', $message );
$user->sendNotification( 'mobileApp', $message );
$user->sendNotification( 'textMsg', $message );$profile = $modx->getObject('modUserProfile', array('internalKey' => $userID));
if ($profile) {
$extended = $profile->get('extended');
$extended['field1'] = 'someValue';
$extended['field2'] = 'someOtherValue';
// etc.
$profile->set('extended', $extended);
$profile->save();
}
BTW, do you really want to send four messages to the user?
[upddate] After re-reading your posts, I don't think you need to extend the user at all unless you need to store the user's preferred contact method. You can just put a snippet in the template of the user profile pages that sends the message. You could also use a plugin attached to something like OnWebPageComplete, but I think the snippet would be much easier.
<?php
class ExtendedUserMethods extends modUser {
public function __construct() {
parent::__construct();
}
private function sendNotification($platform, $message) {
// The code that sends the notification based on the user's preferred method
}
}I still don't understand why you need to extend modUser. Other extras also extend modUser, so there could be conflicts and it doesn't really gain you anything, if I'm understanding your use case. You'll have to write custom code to modify the class_key in the user object and you'll have to do that retroactively for any existing users. Any extra code that looks specifically at the class_key will ignore your users.
Even if you extend modUser, you'll still need a custom snippet or two to save the data and retrieve it (assuming that there's data to be saved).
Anywhere you have the user ID (which you're going to need anyway), you can write a function or method to notify the user without messing with the user class.
Even if you extend modUser, you'll still need a custom snippet or two to save the data and retrieve it (assuming that there's data to be saved).
You haven't said what data you'll be storing or where, but it's much more likely that you'll be storing it in the modUserProfile object. For either object, the save method will only save fields it knows about. If you're not using those, you'll need to modify the table (highly not recommended), or create a custom table.
What I'm trying to say (if I'm understanding you) is that at some point you want to send the user a notification. As long as you have the user ID at that point, it's trivially easy to just do it. Extending a MODX object to do it would be non-trivial, much more work, and overkill imo, though if you want to do it that way it's certainly possible.
https://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/extending-moduser
If you're worried about re-using the code, you can put it in a snippet and call it with $modx->runSnippet(), or put it in a class file, include it, and instantiate it in your code.
$targetUser = $modx->getObject( 'ExtUser', $profileID );
$profile = $targetUser->getOne('profile');
$browsingUser = $modx->getUser();
$targetUser->sendNotification( 'profile_view', $browsingUser->get('id') ); // the "profile_view" verbatim can be easily changed in the data field
// it's also easy to add, to the method's switch, another case, "profile_like", or another "profile_follow"
// also, since we are using a method we don't have to get the browsing user's profile here, because the method gets it and uses the "fullname" field
// all we need to do is specify the type of notification and the user it is in reference to
// if this is a system message from an administrator it can be sent without specifying the second parameter
$targetUser = $modx->getUser( $profileID );
$profile = $targetUser->getOne('profile');
$browsingUser = $modx->getUser();
// doing other stuff anyway like displaying the profile, so I already have the targetUser object
// wait, let's not use a method, let's annoy the coder and make them write four extra lines every time they want to add notifications to an event!
$message = 'Someone viewed your profile!'; // Now we have to find this to change the verbatim rather than change a field in the data
$arrayOfParameters['message'] = $message;
$arrayOfParameters['to'] = $targetUser->get('id');
$arrayOfParameters['from'] = $browsingUser->get('id');
$modx->runSnippet( 'sendNotification', $arrayOfParameters );
<p>Hello [[+username]] here is a notification (etc.)</p>
$fields = array(
'username' => $modx->user->get('username'),
);
$text = $modx->getChunk('SomeChunk', $fields);