diff --git a/modules/commerce_license_role/commerce_license_role.module b/modules/commerce_license_role/commerce_license_role.module index 63827c9..def844a 100644 --- a/modules/commerce_license_role/commerce_license_role.module +++ b/modules/commerce_license_role/commerce_license_role.module @@ -125,3 +125,44 @@ function commerce_license_role_allowed_values($field, $instance, $entity_type, $ unset($roles[DRUPAL_AUTHENTICATED_RID]); return $roles; } + +/** + * Implements hook_entity_presave(). + */ +function commerce_license_role_entity_presave($entity, $type) { + // We use this presave hook to set future + // granted & expires dates for license renewals, + // in case the user has an active license granting + // the same roles as the currently saved license: + if ($type == 'commerce_license' && empty($entity->granted) && !empty($entity->product_id) && $entity->type == 'role') { + $query = new EntityFieldQuery(); + $result = $query + ->entityCondition('entity_type', 'commerce_license') + ->propertyCondition('uid', $entity->uid) + ->propertyCondition('status', COMMERCE_LICENSE_ACTIVE) + ->propertyCondition('type', 'role') + ->propertyOrderBy('expires', 'DESC') + ->range(0, 1) + ->execute(); + + if (isset($result['commerce_license'])) { + // Compare roles to see if we need to create + // a new license as usual, or modify the granted + // and expiring dates if this license according + // to an existing active license which grants + // the same roles as this one: + $result_license_ids = array_keys($result['commerce_license']); + $current_id = array_shift($result_license_ids); + $license = entity_load_single('commerce_license', $current_id); + $found_license_role = $license->wrapper->product->commerce_license_role->value(); + $this_license_role = $entity->wrapper->product->commerce_license_role->value(); + if ($found_license_role == $this_license_role) { + $entity->granted = $license->expires; + $duration = $entity->wrapper->product->commerce_license_duration->value(); + if ($duration > 0 && empty($entity->expires)) { + $entity->expires = $entity->granted + $duration; + } + } + } + } +} diff --git a/modules/commerce_license_role/plugins/license_type/CommerceLicenseRole.class.php b/modules/commerce_license_role/plugins/license_type/CommerceLicenseRole.class.php index 728431d..f3a5832 100644 --- a/modules/commerce_license_role/plugins/license_type/CommerceLicenseRole.class.php +++ b/modules/commerce_license_role/plugins/license_type/CommerceLicenseRole.class.php @@ -41,10 +41,48 @@ class CommerceLicenseRole extends CommerceLicenseBase { } } elseif ($this->status > COMMERCE_LICENSE_ACTIVE) { - // The owner of an inactive license must not have the role. - if (isset($owner->roles[$role])) { - unset($owner->roles[$role]); - $save_owner = TRUE; + // Check if there is another license still active. + // If so, compare the role this license gives + // with the role the other license provides - + // if they are equal - don't remove the role + // (this takes care of different licenses granting same role) + $query = new EntityFieldQuery(); + $query->entityCondition('entity_type', 'commerce_license') + ->propertyCondition('uid', $this->uid) + ->propertyCondition('status', COMMERCE_LICENSE_ACTIVE) + ->propertyCondition('type', 'role') + ->propertyCondition('expires', time(), '>') + ->propertyOrderBy('expires', 'DESC') + ->range(0, 1); + if (!empty($this->license_id)) { + $query->propertyCondition('license_id', $this->license_id, '<>'); + } + $result = $query->execute(); + if (!isset($result['commerce_license'])) { + // No existing other active licenses for this user. + // Wrapping if around existing code below + // The owner of an inactive license must not have the role. + if (isset($owner->roles[$role])) { + unset($owner->roles[$role]); + $save_owner = TRUE; + } + } + else { + // There are other active licenses - + // compare the roles: + $result_license_ids = array_keys($result['commerce_license']); + $current_id = array_shift($result_license_ids); + $license = entity_load_single('commerce_license', $current_id); + $found_license_role = $license->wrapper->product->commerce_license_role->value(); + $this_license_role = $this->wrapper->product->commerce_license_role->value(); + if ($this_license_role != $found_license_role) { + // if it's not the same role - + // unset the role... + if (isset($owner->roles[$role])) { + unset($owner->roles[$role]); + $save_owner = TRUE; + } + } } } @@ -53,7 +91,7 @@ class CommerceLicenseRole extends CommerceLicenseBase { user_save($owner); } } - parent::save(); } + }