materio_admin.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. 'materio admin fix term reference field' => array(
  20. 'title' => t('Materio admin fix term reference field'),
  21. 'description' => t('Materio admin fix term reference field.'),
  22. ),
  23. );
  24. }
  25. /**
  26. * Implements hook_menu().
  27. */
  28. function materio_admin_menu() {
  29. $items['admin/people/duplicatemails'] = array(
  30. 'title' => "Duplicate mails",
  31. 'page callback' => 'materio_duplicatemails',
  32. 'access callback' => 'user_access',
  33. 'access arguments' => array('access duplicate mails list'),
  34. 'type' => MENU_LOCAL_TASK
  35. );
  36. if(module_exists('simplenews')){
  37. $cats = simplenews_category_list();
  38. // dsm($cats, 'cats');
  39. foreach ($cats as $tid => $name) {
  40. $items['node/add/simplenews/'.$tid] = array(
  41. 'title' => $name,
  42. 'title callback' => 'check_plain',
  43. 'page callback' => 'node_add',
  44. 'page arguments' => array('simplenews'),
  45. 'access callback' => 'node_access',
  46. 'access arguments' => array('create', 'simplenews'),
  47. 'file path' => drupal_get_path('module', 'node'),
  48. 'file' => 'node.pages.inc',
  49. );
  50. }
  51. }
  52. // fix term ref field lost with taxonomy
  53. $items['admin/config/content/materio'] = array(
  54. 'title' => 'Materio',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('materio_admin_fix_termref_fields_form'),
  57. 'access arguments' => array('materio admin fix term reference field'),
  58. 'type' => MENU_NORMAL_ITEM,
  59. );
  60. $items['admin/config/content/materio/fix_termref_fields'] = array(
  61. 'title' => 'Fix term reference field from taxonomy',
  62. 'type' => MENU_DEFAULT_LOCAL_TASK,
  63. );
  64. return $items;
  65. }
  66. function materio_duplicatemails(){
  67. $mails = db_query('SELECT mail FROM {users} GROUP BY mail HAVING count(mail) > 1')->fetchCol();
  68. // Bail out early if there are no duplicates.
  69. if (!$mails) {
  70. return t('All accounts have unique email addresses.');
  71. }
  72. // Grab all the user data for accounts with addresses identified as
  73. // duplicates. This is a little convoluted, but it lets us grab all the user
  74. // data in one shot.
  75. $uids = db_select('users', 'u')
  76. ->fields('u', array('uid'))
  77. ->condition('mail', $mails, 'IN')
  78. ->orderBy('access', 'DESC')
  79. ->execute()
  80. ->fetchCol();
  81. $duplicate_users = user_load_multiple($uids);
  82. $duplicate_mails = array();
  83. foreach ($duplicate_users as $duplicate_user) {
  84. $duplicate_mails[$duplicate_user->mail][] = $duplicate_user;
  85. }
  86. // Turn the data we've got into markup.
  87. $output = t('Accounts with duplicate email address:') . '<br />';
  88. $output = "<ul>";
  89. foreach ($duplicate_mails as $mail => $users) {
  90. $output .= "<li> <strong>$mail</strong> : <br />";
  91. $accounts = array();
  92. foreach ($users as $duplicate_user) {
  93. $accounts[] = l($duplicate_user->name, "user/{$duplicate_user->uid}") . ' ('. date('Y-m-d', $duplicate_user->access) .')';
  94. }
  95. $output .= implode(', ', $accounts);
  96. $output .= '</li>';
  97. }
  98. $output .= "</ul>";
  99. return $output;
  100. }
  101. function materio_admin_fix_termref_fields_form(){
  102. drupal_set_title('Fix term reference fields', PASS_THROUGH);
  103. $types = node_type_get_types();
  104. // dsm($types, 'types');
  105. $nt_options = array();
  106. foreach ($types as $mn => $type) {
  107. $nt_options[$mn] = $type->name;
  108. }
  109. $node_type = variable_get('materio_admin_fix_termref_node_type', null);
  110. // source field (must be a text field)
  111. $form['node_type'] = array(
  112. '#type'=>'select',
  113. '#options'=>$nt_options,
  114. '#default_value' => $node_type,
  115. '#title' => t('Content type'),
  116. '#multiple' => false,
  117. );
  118. if($node_type){
  119. $fieldsmap = field_info_field_map();
  120. $target_options = array();
  121. foreach ($fieldsmap as $field_name => $field) {
  122. // dsm($field, $field_name);
  123. if ($field['type'] == 'taxonomy_term_reference'
  124. && isset($field['bundles']['node'])
  125. && in_array($node_type, $field['bundles']['node'])) {
  126. $target_options[$field_name] = $field_name;
  127. }
  128. }
  129. $target_field = variable_get('materio_admin_fix_termref_target_field', null);
  130. // target field (must be a entity_reference field)
  131. $form['target_field'] = array(
  132. '#type'=>'select',
  133. '#options'=>$target_options,
  134. '#default_value' => $target_field,
  135. '#title' => t('target field (must be a term_reference field)'),
  136. '#multiple' => false,
  137. );
  138. if($target_field){
  139. // retrive various field infos
  140. $field_info = field_info_field($target_field);
  141. // dsm($field_info, 'field_info');
  142. // $field_instance = field_info_instance('node', $target_field, $node_type);
  143. // dsm($field_instance, 'field_instance');
  144. $voc_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
  145. $vid = taxonomy_vocabulary_machine_name_load($voc_name)->vid;
  146. $voc = taxonomy_vocabulary_load($vid);
  147. $tree = taxonomy_get_tree($vid);
  148. // dsm($tree, 'tree');
  149. foreach ($tree as $key => $term) {
  150. $terms[$term->tid] = $term->name;
  151. }
  152. $src_term = variable_get('materio_admin_fix_termref_src_term', null);
  153. // src term
  154. $form['src_term'] = array(
  155. '#type'=>'select',
  156. '#options'=>$terms,
  157. '#default_value' => $src_term,
  158. '#title' => t('source term'),
  159. '#multiple' => false,
  160. );
  161. if($src_term){
  162. $form['txt_nids'] = array(
  163. '#type'=>'textarea',
  164. '#title' => t('nids to fix'),
  165. '#description' => t('comma separated nids list'),
  166. );
  167. $form['fixterms'] = array(
  168. '#type' => 'submit',
  169. '#value' => 'Fixterms',
  170. );
  171. // $nodes = taxonomy_select_nodes($src_term, false);
  172. // dsm($nodes, 'nodes');
  173. // $src_items = field_get_items('node', node_load(11858), $target_field);
  174. // dsm($src_items);
  175. }
  176. }
  177. }
  178. $form['submit'] = array(
  179. '#type' => 'submit',
  180. '#value' => 'Save',
  181. // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
  182. );
  183. return $form;
  184. }
  185. function materio_admin_fix_termref_fields_form_submit($form, &$form_state){
  186. // dsm($form_state, 'form_state');
  187. $node_type = $form_state['values']['node_type']; $context['results']['field_migrated']++;
  188. $target_field = $form_state['values']['target_field'];
  189. $src_term = $form_state['values']['src_term'];
  190. variable_set('materio_admin_fix_termref_node_type', $node_type);
  191. variable_set('materio_admin_fix_termref_target_field', $target_field);
  192. variable_set('materio_admin_fix_termref_src_term', $src_term);
  193. $nids = explode(',', $form_state['values']['txt_nids']);
  194. if ($form_state['values']['op'] == 'Fixterms' && !empty($nids)){
  195. _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_term, $nids);
  196. }
  197. }
  198. function _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_tid, $nids){
  199. // Reset counter for debug information.
  200. $_SESSION['http_request_count'] = 0;
  201. $batch = array(
  202. 'title' => t('Fixing ref terms ...'),
  203. 'operations' => array(),
  204. 'init_message' => t('Commencing'),
  205. 'progress_message' => t('Processed @current out of @total.'),
  206. 'error_message' => t('An error occurred during processing'),
  207. 'finished' => '_materio_admin_batch_fixterm_ref_finished',
  208. );
  209. foreach ($nids as $nid) {
  210. $nid = trim($nid);
  211. $batch['operations'][] = array(
  212. '_materio_admin_batch_fixterm_ref_op',
  213. array(
  214. $nid,
  215. $src_tid, $target_field,
  216. $node_type,
  217. )
  218. );
  219. }
  220. batch_set($batch);
  221. }
  222. function _materio_admin_batch_fixterm_ref_op($nid, $src_tid, $target_field, $node_type){
  223. $context['results']['field_migrated']++;
  224. $node = node_load($nid);
  225. if($node->type == $node_type){
  226. $items = field_get_items('node', $node, $target_field);
  227. foreach ($items as $key => $item) {
  228. $flat_items[] = $item['tid'];
  229. }
  230. if(!in_array($src_tid, $flat_items)){
  231. // $items[] = array('tid'=>$src_tid);
  232. $node->{$target_field}[LANGUAGE_NONE][] = array('tid'=>$src_tid);
  233. }
  234. node_save($node);
  235. }
  236. //Simply show the import row count.
  237. $context['message'] = t('Migrating node !c : %title (%nid)', array(
  238. '!c' => $context['results']['field_migrated'],
  239. '%title'=>$node->title,
  240. '%nid'=>$nid ));
  241. // In order to slow importing and debug better,
  242. // we can uncomment this line to make each import slightly slower.
  243. // usleep(2500);
  244. if ( false ) {
  245. $context['results']['failed_nodes'][] = $nid ;
  246. }
  247. _materio_admin_update_http_requests();
  248. }
  249. function _materio_admin_batch_fixterm_ref_finished($success, $results, $operations){
  250. // dsm($success, 'success');
  251. // dsm($results, 'results');
  252. // dsm($operations, 'operations');
  253. if ($success) {
  254. // Here we could do something meaningful with the results.
  255. // We just display the number of nodes we processed...
  256. drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_admin_get_http_requests())));
  257. drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
  258. }
  259. else {
  260. // An error occurred.
  261. // $operations contains the operations that remained unprocessed.
  262. drupal_set_message(
  263. t('operations : @args',
  264. array(
  265. '@args' => print_r(current($operations), TRUE),
  266. )
  267. ),
  268. 'error'
  269. );
  270. $error_operation = reset($operations);
  271. drupal_set_message(
  272. t('An error occurred while processing @operation with arguments : @args',
  273. array(
  274. '@operation' => $error_operation[0],
  275. '@args' => print_r($error_operation[0], TRUE),
  276. )
  277. ),
  278. 'error'
  279. );
  280. }
  281. }
  282. /**
  283. * Utility function to increment HTTP requests in a session variable.
  284. */
  285. function _materio_admin_update_http_requests() {
  286. $_SESSION['http_request_count']++;
  287. }
  288. /**
  289. * Utility function to count the HTTP requests in a session variable.
  290. *
  291. * @return int
  292. * Number of requests.
  293. */
  294. function _materio_admin_get_http_requests() {
  295. return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
  296. }
  297. /**
  298. * Implements hook_menu_alter().
  299. */
  300. function materio_admin_menu_alter(&$items){
  301. // dsm($items, 'menu alter items');
  302. if(isset($items['admin/people'])){
  303. $items['admin/people']['access arguments'] = array('access default users list');
  304. // dsm($items['admin/people']);
  305. }
  306. if(isset($items['admin/people/expiration'])){
  307. $items['admin/people/expiration']['access arguments'] = array('access default UC roles expiration list');
  308. // dsm($items['admin/people/expiration']);
  309. }
  310. # deactivate default home page
  311. $items['node']['access callback'] = FALSE;
  312. // if(isset($items['admin/content/add/simplenews'])){
  313. // delete($items['admin/content/add/simplenews']);
  314. // $cats = simplenews_category_list();
  315. // dsm($cats, 'cats');
  316. // foreach ($cats as $tid => $name) {
  317. // $items['admin/content/add/simplenews/'.$tid]
  318. // }
  319. // }
  320. }
  321. /**
  322. * Implements hook_menu_local_tasks_alter().
  323. */
  324. function materio_admin_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  325. if ($root_path == 'admin/people/simplenews') {
  326. $item = menu_get_item('admin/content/simplenews');
  327. if ($item['access']) {
  328. $item['title'] = 'Go to '.$item['title'];
  329. $data['actions']['output'][] = array(
  330. '#theme' => 'menu_local_task',
  331. '#link' => $item,
  332. );
  333. }
  334. }
  335. if ($root_path == 'admin/content/simplenews') {
  336. $cats = simplenews_category_list();
  337. foreach ($cats as $tid => $name) {
  338. $item = menu_get_item('node/add/simplenews/'.$tid);
  339. $item['title'] = 'Add new '.$name;
  340. if ($item['access']) {
  341. $data['actions']['output'][] = array(
  342. '#theme' => 'menu_local_task',
  343. '#link' => $item,
  344. );
  345. }
  346. }
  347. $item = menu_get_item('admin/people/simplenews');
  348. if ($item['access']) {
  349. $item['title'] = 'Go to '.$item['title'];
  350. $data['actions']['output'][] = array(
  351. '#theme' => 'menu_local_task',
  352. '#link' => $item,
  353. );
  354. }
  355. }
  356. }
  357. /**
  358. * Implements hook_form_alter().
  359. */
  360. function materio_admin_form_simplenews_node_form_alter(&$form, &$form_state, $form_id) {
  361. // dsm($form_id, '$form_id');
  362. // dsm($form_state, '$form_state');
  363. // dsm($form, '$form');
  364. // dsm($_GET, 'GET');
  365. if(!$form['nid']['#value']){
  366. $cats = simplenews_category_list();
  367. $cats_tids = array_keys($cats);
  368. $q = parse_url($_GET['q']);
  369. $cat = array_pop(explode('/', $q['path']));
  370. // dsm($cat, 'cat');
  371. if(in_array($cat, $cats_tids)){
  372. // prepopulate type of news
  373. $form['field_simplenews_term']['und']['#default_value'] = $cat;
  374. $form['field_simplenews_term']['und']['#disabled'] = true;
  375. // change default template regarding type of news
  376. $form['body']['und'][0]['#default_value'] = materio_admin_getSimplenewsNodeBodyTemplate($cat);
  377. }
  378. }else{
  379. $form['field_simplenews_term']['und']['#disabled'] = true;
  380. }
  381. $form['body']['und'][0]['#rows'] = 50;
  382. }
  383. function materio_admin_getSimplenewsNodeBodyTemplate($cat){
  384. return file_get_contents(drupal_get_path('module', 'materio_admin').'/templates/simplenews_'.$cat.'_node.html');
  385. }
  386. // http://stackoverflow.com/questions/19202449/change-drupal-7-compiled-css-and-js-includes-to-use-https-as-opposed-to-http
  387. function materio_admin_process_html(&$vars){
  388. foreach (array('head', 'styles', 'scripts') as $replace) {
  389. if (!isset($vars[$replace])) {
  390. continue;
  391. }
  392. $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
  393. }
  394. }