materio_admin.module 16 KB

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