materio_admin.module 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Implements hook_permission().
  4. */
  5. function materio_admin_permission() {
  6. return array(
  7. 'access default users list' => array(
  8. 'title' => t('Access default users list'),
  9. 'description' => t('Access default users list.'),
  10. ),
  11. 'access default UC roles expiration list' => array(
  12. 'title' => t('access default UC roles expiration list'),
  13. 'description' => t('access default UC roles expiration list.'),
  14. ),
  15. 'access duplicate mails list' => array(
  16. 'title' => t('access duplicate mails list'),
  17. 'description' => t('access duplicate mails list.'),
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_menu().
  23. */
  24. function materio_admin_menu() {
  25. $items['admin/users/duplicatemails'] = array(
  26. 'title' => "Duplicate mails",
  27. 'page callback' => 'materio_duplicatemails',
  28. 'access callback' => 'user_access',
  29. 'access arguments' => array('access duplicate mails list'),
  30. 'type' => MENU_LOCAL_TASK
  31. );
  32. if(module_exists('simplenews')){
  33. $cats = simplenews_category_list();
  34. // dsm($cats, 'cats');
  35. foreach ($cats as $tid => $name) {
  36. // $items['node/%node/simplenews'] = array(
  37. // 'title' => 'Newsletter',
  38. // 'type' => MENU_LOCAL_TASK,
  39. // 'access callback' => 'simplenews_node_tab_access',
  40. // 'access arguments' => array(1),
  41. // 'page callback' => 'simplenews_node_tab_page',
  42. // 'page arguments' => array(1),
  43. // 'context' => MENU_LOCAL_TASK,
  44. // 'file' => 'includes/simplenews.admin.inc',
  45. // 'weight' => 2,
  46. // );
  47. $items['node/add/simplenews/'.$tid] = array(
  48. 'title' => $name,
  49. 'title callback' => 'check_plain',
  50. 'page callback' => 'node_add',
  51. 'page arguments' => array('simplenews'),
  52. 'access callback' => 'node_access',
  53. 'access arguments' => array('create', 'simplenews'),
  54. 'file path' => drupal_get_path('module', 'node'),
  55. 'file' => 'node.pages.inc',
  56. );
  57. }
  58. }
  59. return $items;
  60. }
  61. function materio_duplicatemails(){
  62. $mails = db_query('SELECT mail FROM {users} GROUP BY mail HAVING count(mail) > 1')->fetchCol();
  63. // Bail out early if there are no duplicates.
  64. if (!$mails) {
  65. return t('All accounts have unique email addresses.');
  66. }
  67. // Grab all the user data for accounts with addresses identified as
  68. // duplicates. This is a little convoluted, but it lets us grab all the user
  69. // data in one shot.
  70. $uids = db_select('users', 'u')
  71. ->fields('u', array('uid'))
  72. ->condition('mail', $mails, 'IN')
  73. ->orderBy('access', 'DESC')
  74. ->execute()
  75. ->fetchCol();
  76. $duplicate_users = user_load_multiple($uids);
  77. $duplicate_mails = array();
  78. foreach ($duplicate_users as $duplicate_user) {
  79. $duplicate_mails[$duplicate_user->mail][] = $duplicate_user;
  80. }
  81. // Turn the data we've got into markup.
  82. $output = t('Accounts with duplicate email address:') . '<br />';
  83. $output = "<ul>";
  84. foreach ($duplicate_mails as $mail => $users) {
  85. $output .= "<li> <strong>$mail</strong> : <br />";
  86. $accounts = array();
  87. foreach ($users as $duplicate_user) {
  88. $accounts[] = l($duplicate_user->name, "user/{$duplicate_user->uid}") . ' ('. date('Y-m-d', $duplicate_user->access) .')';
  89. }
  90. $output .= implode(', ', $accounts);
  91. $output .= '</li>';
  92. }
  93. $output .= "</ul>";
  94. return $output;
  95. }
  96. /**
  97. * Implements hook_menu_alter().
  98. */
  99. function materio_admin_menu_alter(&$items){
  100. // dsm($items, 'menu alter items');
  101. if(isset($items['admin/people'])){
  102. $items['admin/people']['access arguments'] = array('access default users list');
  103. // dsm($items['admin/people']);
  104. }
  105. if(isset($items['admin/people/expiration'])){
  106. $items['admin/people/expiration']['access arguments'] = array('access default UC roles expiration list');
  107. // dsm($items['admin/people/expiration']);
  108. }
  109. # deactivate default home page
  110. $items['node']['access callback'] = FALSE;
  111. // if(isset($items['admin/content/add/simplenews'])){
  112. // delete($items['admin/content/add/simplenews']);
  113. // $cats = simplenews_category_list();
  114. // dsm($cats, 'cats');
  115. // foreach ($cats as $tid => $name) {
  116. // $items['admin/content/add/simplenews/'.$tid]
  117. // }
  118. // }
  119. }
  120. /**
  121. * Implements hook_menu_local_tasks_alter().
  122. */
  123. function materio_admin_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  124. if ($root_path == 'admin/people/simplenews') {
  125. $item = menu_get_item('admin/content/simplenews');
  126. if ($item['access']) {
  127. $item['title'] = 'Go to '.$item['title'];
  128. $data['actions']['output'][] = array(
  129. '#theme' => 'menu_local_task',
  130. '#link' => $item,
  131. );
  132. }
  133. }
  134. if ($root_path == 'admin/content/simplenews') {
  135. $cats = simplenews_category_list();
  136. foreach ($cats as $tid => $name) {
  137. $item = menu_get_item('node/add/simplenews/'.$tid);
  138. $item['title'] = 'Add new '.$name;
  139. if ($item['access']) {
  140. $data['actions']['output'][] = array(
  141. '#theme' => 'menu_local_task',
  142. '#link' => $item,
  143. );
  144. }
  145. }
  146. $item = menu_get_item('admin/people/simplenews');
  147. if ($item['access']) {
  148. $item['title'] = 'Go to '.$item['title'];
  149. $data['actions']['output'][] = array(
  150. '#theme' => 'menu_local_task',
  151. '#link' => $item,
  152. );
  153. }
  154. }
  155. }
  156. /**
  157. * Implements hook_form_alter().
  158. */
  159. function materio_admin_form_simplenews_node_form_alter(&$form, &$form_state, $form_id) {
  160. // dsm($form_id, '$form_id');
  161. // dsm($form_state, '$form_state');
  162. // dsm($form, '$form');
  163. // dsm($_GET, 'GET');
  164. if(!$form['nid']['#value']){
  165. $cats = simplenews_category_list();
  166. $cats_tids = array_keys($cats);
  167. $q = parse_url($_GET['q']);
  168. $cat = array_pop(explode('/', $q['path']));
  169. // dsm($cat, 'cat');
  170. if(in_array($cat, $cats_tids)){
  171. // prepopulate type of news
  172. $form['field_simplenews_term']['und']['#default_value'] = $cat;
  173. $form['field_simplenews_term']['und']['#disabled'] = true;
  174. // change default template regarding type of news
  175. $form['body']['und'][0]['#default_value'] = materio_admin_getSimplenewsNodeBodyTemplate($cat);
  176. }
  177. }else{
  178. $form['field_simplenews_term']['und']['#disabled'] = true;
  179. }
  180. $form['body']['und'][0]['#rows'] = 50;
  181. }
  182. function materio_admin_getSimplenewsNodeBodyTemplate($cat){
  183. return file_get_contents(drupal_get_path('module', 'materio_admin').'/templates/simplenews_'.$cat.'_node.html');
  184. }
  185. // http://stackoverflow.com/questions/19202449/change-drupal-7-compiled-css-and-js-includes-to-use-https-as-opposed-to-http
  186. function materio_admin_process_html(&$vars){
  187. foreach (array('head', 'styles', 'scripts') as $replace) {
  188. if (!isset($vars[$replace])) {
  189. continue;
  190. }
  191. $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
  192. }
  193. }