materio_commerce.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // use Drupal\Core\Form\FormBuilder;
  3. // use Drupal\Core\Url;
  4. use \Drupal\Core\Link;
  5. // use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Entity\EntityFormInterface;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\user\EntityOwnerInterface;
  9. function materio_commerce_form_alter(&$form, &$form_state, $form_id) {
  10. if ($form_id == 'commerce_checkout_flow_multistep_default') {
  11. if ($form["#step_id"] == "review") {
  12. // $form['buttons']['continue']['#value'] = t('Desired Custom Text Here');
  13. $form['actions']['next']['#value'] = t('Place your order');
  14. }
  15. }
  16. }
  17. function materio_commerce_form_commerce_order_add_form_alter(&$form, &$form_state, $form_id) {
  18. $currentUser = \Drupal::currentUser();
  19. $roles = $currentUser->getRoles();
  20. if (in_array("admin", $roles)){
  21. $form['type']['#default_value'] = 'materio_order_type';
  22. $form['type']['#disabled'] = true;
  23. $form['customer']['uid']['#selection_handler'] = 'default:user_by_email';
  24. }
  25. }
  26. function materio_commerce_form_role_delegation_role_assign_form_alter(&$form, &$form_state, $form_id) {
  27. // /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  28. // $form_object = $form_state->getFormObject();
  29. // if (!($form_object instanceof EntityFormInterface)) {
  30. // // We're only interested in forms for an entity.
  31. // return;
  32. // }
  33. // $entity = $form_object->getEntity();
  34. // $entity_type_id = $entity->getEntityTypeId();
  35. // if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
  36. // // We only act on entities that have an owner, and user entities.
  37. // return;
  38. // }
  39. /** @var \Drupal\user\UserInterface $account */
  40. $account = $form_state->getBuildInfo()['args'][0];
  41. $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
  42. $licenses = $license_storage->loadByProperties(['uid' => $account->id()]);
  43. if ($licenses) {
  44. // reorganise licenses by role
  45. // and record by role if a license is active
  46. $licenses_by_role = [];
  47. foreach ($licenses as $lid => $license) {
  48. $licensed_role_id = $license->license_role->target_id;
  49. $licenses_by_role[$licensed_role_id]['licenses'][] = $license;
  50. $license_state = $license->getState()->getValue()['value'];
  51. if($license_state === 'active') {
  52. $licenses_by_role[$licensed_role_id]['license_active'] = true;
  53. }
  54. }
  55. // loop throught roles then licenses by role
  56. foreach ($licenses_by_role as $rid => $role_licenses) {
  57. $license_active = isset($role_licenses['license_active']) ? true : false;
  58. $description = "";
  59. if($license_active) {
  60. $form["account"]["role_change"][$rid]['#disabled'] = TRUE;
  61. $form["account"]["role_change"][$rid]['#default_value'] = TRUE;
  62. $description = t("This role is granted by a license. It cannot be removed manually.<br/>");
  63. }
  64. // reverse the licenses to display the last first
  65. $reversed_licenses = array_reverse($role_licenses['licenses']);
  66. foreach ($reversed_licenses as $license) {
  67. $license_state = $license->getState()->getValue()['value'];
  68. $product_variation = $license->getPurchasedEntity();
  69. $product = null;
  70. if ($product_variation){
  71. $product = $product_variation->getProduct();
  72. }
  73. $granted = $license->getGrantedTime();
  74. $expires = $license->getExpiresTime();
  75. $license_edit_url = $license->toUrl();
  76. $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
  77. $order = $license->getOriginatingOrder();
  78. if ($order) {
  79. $order_url = $order->toUrl();
  80. $order_state = $order->get('state')->first()->getValue()['value'];
  81. $order_number = $order->get('order_number')->first()->getValue()['value'];
  82. $order_link = Link::fromTextAndUrl(t("order @num (@state)", array(
  83. '@num' => $order_number,
  84. '@state' => $order_state
  85. )), $order_url);
  86. }
  87. $description .= t("-- @product: @variation | license state: <b>@license_state</b> | granted: @granted | expires: @expires | @license_link | @order_link<br/>", array(
  88. '@product' => $product ? $product->getTitle() : t('no product'),
  89. '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'),
  90. '@license_state' => $license_state,
  91. '@granted' => date('d-m-Y', $granted),
  92. '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
  93. '@license_link' => $license_link->toString(),
  94. '@order_link' => $order ? $order_link->toString() : t('no order')
  95. ));
  96. }
  97. $form["account"]["role_change"][$rid]['#description'] = $description;
  98. }
  99. }
  100. }
  101. function materio_commerce_form_user_form_alter(&$form, &$form_state, $form_id) {
  102. if ($form_id !== 'user_form') return;
  103. // WE USE FORM OBJECT TO GET THE ENTITY instead of followings
  104. // $mail = $form['account']['mail']['#default_value'];
  105. // $user_storage = \Drupal::entityTypeManager()->getStorage('user');
  106. // $users = $user_storage->loadByProperties(['mail' => $mail]);
  107. // $user = reset($users);
  108. // $roles = $user->getRoles();
  109. /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  110. $form_object = $form_state->getFormObject();
  111. if (!($form_object instanceof EntityFormInterface)) {
  112. // We're only interested in forms for an entity.
  113. return;
  114. }
  115. $entity = $form_object->getEntity();
  116. $entity_type_id = $entity->getEntityTypeId();
  117. if (!($entity instanceof EntityOwnerInterface) && $entity_type_id != 'user') {
  118. // We only act on entities that have an owner, and user entities.
  119. return;
  120. }
  121. $license_storage = \Drupal::entityTypeManager()->getStorage('commerce_license');
  122. $licenses = $license_storage->loadByProperties(['uid' => $entity->id()]);
  123. if ($licenses) {
  124. // reorganise licenses by role
  125. // and record by role if a license is active
  126. $licenses_by_role = [];
  127. foreach ($licenses as $lid => $license) {
  128. $licensed_role_id = $license->license_role->target_id;
  129. $licenses_by_role[$licensed_role_id]['licenses'][] = $license;
  130. $license_state = $license->getState()->getValue()['value'];
  131. if($license_state === 'active') {
  132. $licenses_by_role[$licensed_role_id]['license_active'] = true;
  133. }
  134. }
  135. // we need to validate the form as disabled cekbox are not sent on submit
  136. // as it remove the disabled roles
  137. $form["#validate"][] = 'materio_commerce_form_user_form_validate';
  138. // hidden licensed role will be re-injected in 'role_change' field values during our validate
  139. $form['licensed_roles'] = array(
  140. '#type'=>"container",
  141. '#tree' => true
  142. );
  143. // loop throught roles then licenses by role
  144. $form["role_change"]["widget"]['#options_attributes'] = [];
  145. foreach ($licenses_by_role as $rid => $role_licenses) {
  146. $license_active = isset($role_licenses['license_active']) ? true : false;
  147. $description = "";
  148. if($license_active) {
  149. $form["role_change"]["widget"][$rid]['#disabled'] = TRUE;
  150. // $form["role_change"][$rid]['#default_value'] = TRUE;
  151. $description = t("This role is granted by a license. It cannot be removed manually.<br/>");
  152. // record the role in hidden element, as disabled checkboxes are not posted
  153. // we'll re-establish the right values in validate function
  154. $form['licensed_roles'][$rid] = array(
  155. '#type' => 'hidden',
  156. '#value' => $rid
  157. );
  158. }
  159. // reverse the licenses to display the last first
  160. $reversed_licenses = array_reverse($role_licenses['licenses']);
  161. foreach ($reversed_licenses as $license) {
  162. $license_state = $license->getState()->getValue()['value'];
  163. $product_variation = $license->getPurchasedEntity();
  164. $product = null;
  165. if ($product_variation){
  166. $product = $product_variation->getProduct();
  167. }
  168. $granted = $license->getGrantedTime();
  169. $expires = $license->getExpiresTime();
  170. $license_edit_url = $license->toUrl();
  171. $license_link = Link::fromTextAndUrl(t('edit license'), $license_edit_url);
  172. $order = $license->getOriginatingOrder();
  173. if ($order) {
  174. // payments
  175. $payment_storage = \Drupal::entityTypeManager()->getStorage('commerce_payment');
  176. $payments = $payment_storage->loadByProperties(['order_id' => $order->id()]);
  177. if (!empty($payments)) {
  178. $first_payment = array_shift($payments);
  179. $gateway = $first_payment->get('payment_gateway')->first()->getValue()['target_id'];
  180. }
  181. // order info and link
  182. $order_url = $order->toUrl();
  183. $order_state = $order->get('state')->first()->getValue()['value'];
  184. // $order_number = $order->get('order_number')->first()->getValue()['value'];
  185. $order_link = Link::fromTextAndUrl(t("order (@gateway @state)", array(
  186. // '@num' => $order_number,
  187. '@state' => $order_state,
  188. '@gateway' => isset($gateway) ? $gateway : ''
  189. )), $order_url);
  190. // susbcriptions
  191. $susbcription_storage = \Drupal::entityTypeManager()->getStorage('commerce_subscription');
  192. $subsriptions = $susbcription_storage->loadByProperties(['initial_order' => $order->id()]);
  193. if (!empty($subsriptions)) {
  194. $first_subscription = array_shift($subsriptions);
  195. $subscription_url = $first_subscription->toUrl();
  196. $subscription_title = $first_subscription->getTitle();
  197. $subscription_state = $first_subscription->get('state')->first()->getValue()['value'];
  198. $subscription_payment_method_label = "none";
  199. if ( $subscription_payment_method = $first_subscription->getPaymentMethod() ){
  200. $subscription_payment_method_label = $subscription_payment_method->label()->render();
  201. }
  202. $subscription_link = Link::fromTextAndUrl(t("subscription @title @state [@pm_label]", array(
  203. '@title' => $subscription_title,
  204. '@state' => $subscription_state,
  205. '@pm_label' => $subscription_payment_method_label
  206. )), $subscription_url);
  207. }
  208. }
  209. $description .= t("-- @product: @variation | license state: <b>@license_state</b> | granted: @granted | expires: @expires | @license_link | @subscription_link | @order_link<br/>", array(
  210. '@product' => $product ? $product->getTitle() : t('no product'),
  211. '@variation' => $product_variation ? $product_variation->getTitle() : t('no variation'),
  212. '@license_state' => $license_state,
  213. '@granted' => date('d-m-Y', $granted),
  214. '@expires' => $expires === "0" ? 'never' : date('d-m-Y', $expires),
  215. '@license_link' => $license_link->toString(),
  216. '@subscription_link' => isset($subscription_link) ? $subscription_link->toString() : t('no subscription'),
  217. '@order_link' => isset($order) ? $order_link->toString() : t('no order')
  218. ));
  219. }
  220. $form["role_change"]["widget"][$rid]['#description'] = $description;
  221. }
  222. }
  223. }
  224. function materio_commerce_form_user_form_validate($form, FormStateInterface &$form_state) {
  225. $values = $form_state->getValues();
  226. if (!empty($values['licensed_roles'])) {
  227. foreach ($values['licensed_roles'] as $rid => $value) {
  228. $values['role_change'][] = array("target_id"=>$value);
  229. }
  230. $form_state->setValues($values);
  231. }
  232. }