assign users to group modx - modx

How can i assign newly created user to the particular group in modx Programmaticaly ? Below is my code
if(isset($_POST) && count($_POST)){
$oUser = $modx->newObject('modUser');
$oUser->set('username', "test");
//$oUser->set('password', "test");
$oProfile = $modx->newObject('modUserProfile');
$oProfile->set('fullname', $_POST['fname']);
$oProfile->set('email', $_POST['email']);
$oUser->addOne($oProfile);
if($oUser->save()===false){
echo "Error";
}else
echo "Done";
}
I googled but all i find is graphical tutorial how to create groups and edit user and then assign roles, If you know any tutorial then also its fine.

Here is how I have been doing it, this is a posthook snippet that fires after a user registers [and the user is created]
<?php
$specialty = $hook->getValue('specialty');
$country = strtolower($hook->getValue('excountry'));
$username = $hook->getValue('username');
$staff = $hook->getValue('staff-or-resident'); //Staff | Resident
$joingroup = '';
$joinrole = '';
$blockuser = 'false';
switch ($specialty){
case 'Other' :
$joingroup = 15; // Other
$joinrole = 1; //member
$blockuser = 'true';
break;
// there are about 15 different groups and roles here...
default :
$joingroup = '0'; // none
$joinrole = '0'; // none
break;
}
if($joingroup > 0 && $joinrole > 0){
$user = $modx->getObject('modUser', array('username'=>$username));
$internalkey = $user->get('id');
$profile = $user->getOne('Profile',array('internalKey'=>$internalkey));
$user->joinGroup($joingroup, $joinrole);
if($blockuser == 'true'){ //block user if they belong to the "other" group
$profile->set('blocked',1);
}
if(!$user->save()){
return false;
};
}
return true;
The key is the: $user->joinGroup($joingroup, $joinrole); where joingroup is the group id ~ or name and the joinrole is the role id ~ or name. It's documented here: http://api.modx.com/revolution/2.1/_model_modx_moduser.class.html#%5CmodUser::joinGroup()

The best way to create/edit something in revo >2.2 this is use "Class-based Processors" - https://www.markhamstra.com/modx-blog/2012/2/getting-started-with-class-based-processors-2.2/ to add user to group use this processor https://github.com/modxcms/revolution/blob/develop/core/model/modx/processors/security/user/update.class.php with this -
http://rtfm.modx.com/display/revolution20/Using+runProcessor
$param = array(
'id' => 1, // user id
'groups' => array(
array(
"usergroup" => 1,
"name" => "Administrator",
"member" => 1,
"role" => 2,
"rolename" => "Super User",
"primary_group" => true,
"rank" => 0,
"menu" => null
),
array( .... )
)
);
$response = $modx->runProcessor('security/user/update',$param );
if ($response->isError()) {
return $response->getMessage();
}

Related

How to send customer renewal order to secondary email in Woocommerce subscription

I want to send the renewal order email to a secondary user email(which i have added in user-edit page using ACF).
I have tried many methods,woocommerce_subscription_payment_complete is also not working for me.
The following code i have tried:
add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 20, 2 );
function action_on_order_status_completed( $order_id, $order ){
$order = new WC_Order($order_id);
// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
$secondary_recipient = get_field('secondary_email', 'user_'.$user_id );
$subscriptions_ids = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) );
// We get all related subscriptions for this order
foreach( $subscriptions_ids as $subscription_id => $subscription_obj )
if($subscription_obj->order->id == $order_id) break; // Stop the loop
// $subscription_objc = wcs_get_subscription($subscription_id);
//$userid = $subscription_objc->get_user_id();
$wc_emails = WC()->mailer()->get_emails();
$wc_emails['WCS_Email_Processing_Renewal_Order']->recipient = $secondary_recipient;
$wc_emails['WCS_Email_Processing_Renewal_Order']->trigger($subscription_id);
// $to = $secondary_recipient;
// $subject = "hi";
// $body =$user_id."end".$order_id."hhh".$subscription_id;
// $headers = array('Content-Type: text/html; charset=UTF-8');
// //$headers[] = 'Cc: sarun#cloudspring.in';
// wp_mail( $to, $subject, $body, $headers );
}
FYI:Email is sending if i use the commented wp_mail function.
We can add a secondary email as the recipient, Try the below code tested and it worked.
add_filter( 'woocommerce_email_recipient_customer_completed_renewal_order', 'my_email_recipient_filter_function', 10, 2);
function my_email_recipient_filter_function( $recipient, $order ) {
$user_id = $order->get_user_id(); // or $order->get_customer_id();
$secondary_recipient = get_field('secondary_email', 'user_'.$user_id );
if(! empty($secondary_recipient)){
$recipient = $recipient . ', '. $secondary_recipient;
return $recipient;
}else {
return $recipient;
}
}

Multiple parents for one resource MODx Revo

I use pdoTools and I need to assign one resource to many parents, but physically it'll be in one parent category. What I already did:
Created a new TV "listParents", and in Input Options #eval return $modx->runSnippet('listParents');
Created snippet "listParents"
<?php
$criteria = $modx->newQuery('modResource');
$criteria->where(array(
'published' => 1,
'deleted' => 0,
'isfolder' => 1,
'template' => 6,
array('AND:id:!=' => 6)
));
$criteria->sortby('menuindex', 'ASC');
$collection = $modx->getCollection('modResource', $criteria);
$output = array();
foreach ($collection as $v) {
$output[] = !empty($v->get('menutitle')) ? $v->get('menutitle') : $v->get('pagetitle') . '==' . $v->get('id');
}
return implode('||', $output);
Now I can see all categories in resources: http://prntscr.com/gbq34i and can mark in which categories it should output too besides parent category http://prntscr.com/gbq9y3.
And if I want to go to one of checked categories, I should see this resource. But how can I output it via pdoTools?
I was needed to add small check:
if($isHideInMenu == '1' AND $modx->resource->get("id") != '1') {
$params['tvFilters'] = 'listParents==%' . $modx->resource->get("id") . '%';
}
The key is this line: $params['tvFilters'] = 'listParents==%' . $modx->resource->get("id") . '%';
It'll find all rows with id of current page, which stores in listParents.

Modx redirect user to a specific page after manager login

I've found this plugin code:
<!--?php
if ($modx--->event->name == 'OnManagerLogin') {
$path = $modx->getOption('base_url',null,MODX_BASE_URL);
$modx->sendRedirect($path.'manager/?a=69');
}
but I cannot manage to make it work with MODX Revolution 2.5.0-pl.
It seems that OnManagerLogin does not work at all.
Am I the only one? Is it related to this?
https://github.com/modxcms/revolution/issues/11848
I've also tried this:
$e = $modx->event;
switch( $e->name ) {
case 'OnManagerLogin':
$modx->sendRedirect('http://modx.com',array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
}
but it did not work. Can anyone help?
Solution is here
With this code :
$url = 'http://www.example.com/manager/';
$groups = array(
'admin_ceremonie' => 'http://www.example.com/manager/?a=resource/update&id=1138',
);
$userId = $user->get('id');
$searchCriteria = array(
'user' => $userId,
'key' => 'LoginResourceUrl',
);
$userSetting = $modx->getObject('modUserSetting', $searchCriteria);
if ($userSetting) {
$url = $userSetting->get('value');
}
else {
foreach($groups as $groupName => $pageUrl) {
if ($user->isMember($groupName)) {
$url = $pageUrl;
break;
}
}
}
$modx->sendRedirect($url);

How to show one documents (resource) information (pagetitle and TV's) with different templates?

I have two external links and I need to show information from document (pagetitle and TV's) with two different templates (standard, print mode). IS there any solution?
Thank you!
You need to create Symlinks for this resource with needed templates. https://rtfm.modx.com/revolution/2.x/making-sites-with-modx/structuring-your-site/resources/symlink/using-resource-symlinks
UPD:
<?php
$from_parent_id = 8; // Test 1 dir id
$to_parent_id = 9; // Test2 dir id
$to_template_id = 2; // Template id of newly created symlink
if ($modx->event->name == 'OnDocFormSave') {
if ( $mode == modSystemEvent::MODE_NEW ) {
$parent = $resource->get('parent');
if ($from_parent_id == $parent) {
$fields = array(
'template' => $to_template_id,
'content_type' => 1,
'class_key' => 'modSymLink',
'context_key' => 'web',
'parent' => $to_parent_id,
'richtext' => 0,
'hidemenu' => 0,
'published' => 1,
'searchable' => 0,
'cacheable' => 1,
'content' => $id,
);
$new_resource = $modx->newObject('modSymlink', $fields);
$new_resource->save();
}
}
}
Maybe the MODX extra SwitchTemplate is an option for your demand?
This extra changes the MODX resource template on the fly by an request parameter. It could be installed by Package Management in MODX backend.

Drupal hook_search function location

I can't for the life of me figure out where the hook_search function in drupal is located. Is it something I need to add to a file to access?
Hook functions don't exist by name -- they indicate a naming convention that can be followed to respond to that particular "hook"...
An example would be the node_search() function. When the search module calls module_invoke_all('search'), all functions named foo_search(), where foo is the name of an enabled module, will be called. The details of the search hook in particular are found on api.drupal.org.
function hook_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('content');
case 'reset':
variable_del('node_cron_last');
return;
case 'search':
$find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. node_access_join_sql() .' INNER JOIN {users} u ON n.uid = u.uid', 'n.status = 1 AND '. node_access_where_sql());
$results = array();
foreach ($find as $item) {
$node = node_load(array('nid' => $item));
$extra = node_invoke_nodeapi($node, 'search result');
$results[] = array('link' => url('node/'. $item),
'type' => node_invoke($node, 'node_name'),
'title' => $node->title,
'user' => theme('username', $node),
'date' => $node->changed,
'extra' => $extra,
'snippet' => search_excerpt($keys, check_output($node->body, $node->format)));
}
return $results;
}
}

Resources