View source
<?php
function uc_recurring_subscription_ca_entity() {
$entities = array();
$entities['uc_recurring_subscription'] = array(
'#title' => t('Recurring subscription'),
'#type' => 'object',
);
return $entities;
}
function uc_recurring_subscription_ca_condition() {
$condition = array();
return $conditions;
}
function uc_recurring_subscription_ca_action() {
$actions['uc_recurring_subscription_grant_role'] = array(
'#title' => t('Grant a role on an order.'),
'#category' => t('Role'),
'#callback' => 'uc_recurring_subscription_action_grant_role',
'#arguments' => array(
'order' => array(
'#entity' => 'uc_order',
'#title' => t('Order'),
),
),
);
$actions['uc_recurring_subscription_revoke_role'] = array(
'#title' => t('Revoke a role on an order.'),
'#category' => t('Role'),
'#callback' => 'uc_recurring_subscription_action_revoke_role',
'#arguments' => array(
'order' => array(
'#entity' => 'uc_order',
'#title' => t('Order'),
),
),
);
if (module_exists('og_actions')) {
$actions['uc_recurring_subscription_grant_og'] = array(
'#title' => t('Grant organic group membership on order.'),
'#category' => t('Organic Group'),
'#callback' => 'uc_recurring_subscription_action_grant_og',
'#arguments' => array(
'order' => array(
'#entity' => 'uc_order',
'#title' => t('Order'),
),
),
);
$actions['uc_recurring_subscription_revoke_og'] = array(
'#title' => t('Revoke organic group membership on order.'),
'#category' => t('Organic Group'),
'#callback' => 'uc_recurring_subscription_action_revoke_og',
'#arguments' => array(
'order' => array(
'#entity' => 'uc_order',
'#title' => t('Order'),
),
),
);
}
return $actions;
}
function uc_recurring_subscription_ca_trigger() {
$triggers = array();
return $triggers;
}
function uc_recurring_subscription_ca_predicate() {
$predicates = array();
$predicates['uc_recurring_subscription_grant_roles'] = array(
'#title' => t('Grant user roles on subscription created.'),
'#trigger' => 'uc_order_status_update',
'#class' => 'roles',
'#status' => 1,
'#weight' => 1,
'#conditions' => array(
'#operator' => 'AND',
'#conditions' => array(
array(
'#name' => 'uc_order_status_condition',
'#title' => t('If order status is completes.'),
'#argument_map' => array(
'order' => 'updated_order',
),
'#settings' => array(
'negate' => FALSE,
'order_status' => 'completed',
),
),
),
),
'#actions' => array(
array(
'#name' => 'uc_recurring_subscription_grant_role',
'#title' => t('Grant user role(s).'),
'#argument_map' => array(
'order' => 'order',
),
'#settings' => array(
'role_option' => 'subscribe_grant',
'role' => array(),
),
),
),
);
$predicates['uc_recurring_subscription_expired_grant_roles'] = array(
'#title' => t('Grant user roles on subscription expiration.'),
'#trigger' => 'uc_recurring_renewal_expired',
'#class' => 'roles',
'#status' => 1,
'#weight' => 1,
'#conditions' => array(
'#operator' => 'AND',
'#conditions' => array(),
),
'#actions' => array(
array(
'#name' => 'uc_recurring_subscription_grant_role',
'#title' => t('Grant user role(s).'),
'#argument_map' => array(
'order' => 'order',
),
'#settings' => array(
'role_option' => 'expire_grant',
'role' => array(),
),
),
),
);
$predicates['uc_recurring_subscription_expired_revoke_roles'] = array(
'#title' => t('Revoke user roles on subscription expiration.'),
'#trigger' => 'uc_recurring_renewal_expired',
'#class' => 'roles',
'#status' => 1,
'#weight' => 1,
'#conditions' => array(
'#operator' => 'AND',
'#conditions' => array(),
),
'#actions' => array(
array(
'#name' => 'uc_recurring_subscription_revoke_role',
'#title' => t('Revoke user role(s).'),
'#argument_map' => array(
'order' => 'order',
),
'#settings' => array(
'role_option' => 'expire_revoke',
'role' => array(),
),
),
),
);
if (module_exists('og_actions')) {
$predicates['uc_recurring_subscription_grant_og'] = array(
'#title' => t('Grant organic groups on subscription created.'),
'#trigger' => 'uc_order_status_update',
'#class' => 'og',
'#status' => 1,
'#weight' => 1,
'#conditions' => array(
'#operator' => 'AND',
'#conditions' => array(
array(
'#name' => 'uc_order_status_condition',
'#title' => t('If order status is completes.'),
'#argument_map' => array(
'order' => 'updated_order',
),
'#settings' => array(
'negate' => FALSE,
'order_status' => 'completed',
),
),
),
),
'#actions' => array(
array(
'#name' => 'uc_recurring_subscription_grant_og',
'#title' => t('Grant groups on subscribe.'),
'#argument_map' => array(
'order' => 'order',
),
'#settings' => array(
'group_option' => 'subscribe_grant_og',
'groups' => array(),
),
),
),
);
$predicates['uc_recurring_subscription_expired_grant_og'] = array(
'#title' => t('Grant organic groups on subscription expiration.'),
'#trigger' => 'uc_recurring_renewal_expired',
'#class' => 'og',
'#status' => 1,
'#weight' => 1,
'#conditions' => array(
'#operator' => 'AND',
'#conditions' => array(),
),
'#actions' => array(
array(
'#name' => 'uc_recurring_subscription_grant_og',
'#title' => t('Grant groups on expire.'),
'#argument_map' => array(
'order' => 'order',
),
'#settings' => array(
'group_option' => '',
'groups' => array(),
),
),
),
);
$predicates['uc_recurring_subscription_expired_revoke_og'] = array(
'#title' => t('Revoke organic groups on subscription expiration.'),
'#trigger' => 'uc_recurring_renewal_expired',
'#class' => 'og',
'#status' => 1,
'#weight' => 1,
'#conditions' => array(
'#operator' => 'AND',
'#conditions' => array(),
),
'#actions' => array(
array(
'#name' => 'uc_recurring_subscription_revoke_og',
'#title' => t('Revoke groups on expire.'),
'#argument_map' => array(
'order' => 'order',
),
'#settings' => array(
'group_option' => 'expire_revoke_og',
'groups' => array(),
),
),
),
);
}
return $predicates;
}
function uc_recurring_subscription_action_grant_role($order, $settings) {
$account = user_load($order->uid);
$roles = user_roles(TRUE);
if ($settings['role_option'] == 'custom') {
$account->roles += $settings['role'];
watchdog('uc_recurring', 'Granted !role role to !user', array(
'!role' => $roles[$rid],
'!user' => $account->name,
));
}
else {
foreach ($order->products as $pid => $product) {
$subscription = uc_recurring_subscription_load($product->nid);
if (!empty($subscription->access[$settings['role_option']])) {
$account->roles += $subscription->access[$settings['role_option']];
}
}
}
user_save($account, array(
'roles' => $account->roles,
));
}
function uc_recurring_subscription_action_grant_role_form($form_state, $settings = array()) {
drupal_add_css(drupal_get_path('module', 'uc_recurring_subscription') . '/uc_recurring_subscription.css');
$form['role_option'] = array(
'#type' => 'radios',
'#title' => t('Where are the role(s) defined?'),
'#attributes' => array(
'class' => 'ca-role-select-option',
),
'#options' => array(
'subscribe_grant' => t('Apply the roles set by the Subscription Manager on creation.'),
'expire_grant' => t('Apply the roles set by the Subscription Manager on expiration.'),
'expire_revoke' => t('Revoke the roles set by the Subscription Manager on expiration.'),
'custom' => t('Custom selected role (select from below)'),
),
'#default_value' => $settings['role_option'],
);
$roles = user_roles(TRUE);
unset($roles[DRUPAL_AUTHENTICATED_RID]);
if (empty($roles)) {
$form['role_error'] = array(
'#value' => t('There are no roles available to select. <a href="!url">Add a role</a>.', array(
'!url' => url('admin/user/roles'),
)),
);
}
else {
$form['role'] = array(
'#type' => 'checkboxes',
'#title' => t('Custom roles'),
'#attributes' => array(
'class' => 'ca-role-option',
),
'#options' => $roles,
'#default_value' => $settings['role'],
);
}
return $form;
}
function uc_recurring_subscription_action_revoke_role($order, $settings) {
$account = user_load($order->uid);
$roles = user_roles(TRUE);
if ($settings['role_option'] == 'custom') {
foreach ($settings['role'] as $rid => $role) {
unset($account->roles[$rid]);
watchdog('uc_recurring', 'Revoked !role role from !user', array(
'!role' => $roles[$rid],
'!user' => $account->name,
));
}
}
else {
foreach ($order->products as $pid => $product) {
$subscription = uc_recurring_subscription_load($product->nid);
if (!empty($subscription->access[$settings['role_option']])) {
foreach ($subscription->access[$settings['role_option']] as $rid => $role) {
unset($account->roles[$rid]);
}
}
}
}
user_save($account, array(
'roles' => $account->roles,
));
}
function uc_recurring_subscription_action_revoke_role_form($form_state, $settings = array()) {
return uc_recurring_subscription_action_grant_role_form($form_state, $settings);
}
function uc_recurring_subscription_action_grant_og_form($form_state, $settings = array()) {
drupal_add_css(drupal_get_path('module', 'uc_recurring_subscription') . '/uc_recurring_subscription.css');
$form['group_option'] = array(
'#type' => 'radios',
'#title' => t('Where are the groups(s) defined?'),
'#attributes' => array(
'class' => 'ca-role-select-option',
),
'#options' => array(
'subscribe_grant_og' => t('Apply the groups set by the Subscription Manager on creation.'),
'expire_grant_og' => t('Apply the groups set by the Subscription Manager on expiration.'),
'expire_revoke_og' => t('Revoke the groups set by the Subscription Manager on expiration.'),
'custom' => t('Custom selected groups (select from below)'),
),
'#default_value' => $settings['group_option'],
);
$form += og_subscribe_user_action_form($settings);
$form['groups']['#required'] = FALSE;
$form['groups']['#description'] = t('Hold CTRL key and left mouse click to select multiple groups.');
return $form;
}
function uc_recurring_subscription_action_revoke_og_form($form_state, $settings = array()) {
return uc_recurring_subscription_action_grant_og_form($form_state, $settings);
}
function uc_recurring_subscription_action_grant_og($order, $settings) {
if (!$order->uid || !($account = user_load($order->uid))) {
return;
}
if ($settings['group_option'] == 'custom' && is_array($settings['groups']) && !empty($settings['groups'])) {
og_subscribe_user_action($account, $settings['groups']);
}
else {
foreach ($order->products as $pid => $product) {
$subscription = uc_recurring_subscription_load($product->nid);
$context['groups'] = $subscription->access[$settings['group_option']];
if (is_array($context['groups']) && !empty($context['groups'])) {
og_subscribe_user_action($account, $context);
$comment = t('User access granted to group(s): %group.', array(
'%group' => implode(',', $context['groups']),
));
uc_order_comment_save($order->order_id, $account->uid, $comment);
}
}
}
}
function uc_recurring_subscription_action_revoke_og($order, $settings) {
if (!$order->uid || !($account = user_load($order->uid))) {
return;
}
if ($settings['group_option'] == 'custom' && is_array($settings['groups']) && !empty($settings['groups'])) {
og_unsubscribe_user_action($account, $settings['groups']);
}
else {
foreach ($order->products as $pid => $product) {
$subscription = uc_recurring_subscription_load($product->nid);
$context['groups'] = $subscription->access[$settings['group_option']];
if (is_array($context['groups']) && !empty($context['groups'])) {
og_unsubscribe_user_action($account, $context);
$comment = t('User access revoked for group(s): %group.', array(
'%group' => implode(',', $context['groups']),
));
uc_order_comment_save($order->order_id, $account->uid, $comment);
}
}
}
}