materio_admin.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. $missingentities = 0;
  117. foreach ($flags as $key => $value) {
  118. // $entity = entity_load($value->entity_type, array($value->entity_id));
  119. $query = new EntityFieldQuery();
  120. $query
  121. ->entityCondition('entity_type', $value->entity_type)
  122. ->propertyCondition('nid', $value->entity_id);
  123. $entity = $query->execute();
  124. if(!isset($entity[$value->entity_type])){
  125. // $missingentities[$value->fid] = $value;
  126. $missingentities++;
  127. db_delete('flag_lists_content')
  128. ->condition('fcid', $value->fcid)
  129. ->execute();
  130. watchdog('flag_lists', t('Deleted entry @fcid from flat_lists_content', array('@fcid' => $value->fcid)));
  131. }
  132. }
  133. // Bail out early if there are no duplicates.
  134. if (!$missingentities) {
  135. return t('No missing entities in flags.');
  136. }
  137. // else{
  138. // dsm($missingentities);
  139. // }
  140. // delete flags with missing entities
  141. // foreach ($missingentities as $fid => $value) {
  142. //
  143. // }
  144. // Turn the data we've got into markup.
  145. $output = t('@count missing entities in flag lists have been cleaned', array('@count'=> count($missingentities)));
  146. return $output;
  147. }
  148. function materio_admin_fix_termref_fields_form(){
  149. drupal_set_title('Fix term reference fields', PASS_THROUGH);
  150. $types = node_type_get_types();
  151. // dsm($types, 'types');
  152. $nt_options = array();
  153. foreach ($types as $mn => $type) {
  154. $nt_options[$mn] = $type->name;
  155. }
  156. $node_type = variable_get('materio_admin_fix_termref_node_type', null);
  157. // source field (must be a text field)
  158. $form['node_type'] = array(
  159. '#type'=>'select',
  160. '#options'=>$nt_options,
  161. '#default_value' => $node_type,
  162. '#title' => t('Content type'),
  163. '#multiple' => false,
  164. );
  165. if($node_type){
  166. $fieldsmap = field_info_field_map();
  167. $target_options = array();
  168. foreach ($fieldsmap as $field_name => $field) {
  169. // dsm($field, $field_name);
  170. if ($field['type'] == 'taxonomy_term_reference'
  171. && isset($field['bundles']['node'])
  172. && in_array($node_type, $field['bundles']['node'])) {
  173. $target_options[$field_name] = $field_name;
  174. }
  175. }
  176. $target_field = variable_get('materio_admin_fix_termref_target_field', null);
  177. // target field (must be a entity_reference field)
  178. $form['target_field'] = array(
  179. '#type'=>'select',
  180. '#options'=>$target_options,
  181. '#default_value' => $target_field,
  182. '#title' => t('target field (must be a term_reference field)'),
  183. '#multiple' => false,
  184. );
  185. if($target_field){
  186. // retrive various field infos
  187. $field_info = field_info_field($target_field);
  188. // dsm($field_info, 'field_info');
  189. // $field_instance = field_info_instance('node', $target_field, $node_type);
  190. // dsm($field_instance, 'field_instance');
  191. $voc_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
  192. $vid = taxonomy_vocabulary_machine_name_load($voc_name)->vid;
  193. $voc = taxonomy_vocabulary_load($vid);
  194. $tree = taxonomy_get_tree($vid);
  195. // dsm($tree, 'tree');
  196. foreach ($tree as $key => $term) {
  197. $terms[$term->tid] = $term->name;
  198. }
  199. $src_term = variable_get('materio_admin_fix_termref_src_term', null);
  200. // src term
  201. $form['src_term'] = array(
  202. '#type'=>'select',
  203. '#options'=>$terms,
  204. '#default_value' => $src_term,
  205. '#title' => t('source term'),
  206. '#multiple' => false,
  207. );
  208. if($src_term){
  209. $form['txt_nids'] = array(
  210. '#type'=>'textarea',
  211. '#title' => t('nids to fix'),
  212. '#description' => t('comma separated nids list'),
  213. );
  214. $form['fixterms'] = array(
  215. '#type' => 'submit',
  216. '#value' => 'Fixterms',
  217. );
  218. // $nodes = taxonomy_select_nodes($src_term, false);
  219. // dsm($nodes, 'nodes');
  220. // $src_items = field_get_items('node', node_load(11858), $target_field);
  221. // dsm($src_items);
  222. }
  223. }
  224. }
  225. $form['submit'] = array(
  226. '#type' => 'submit',
  227. '#value' => 'Save',
  228. // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
  229. );
  230. return $form;
  231. }
  232. function materio_admin_fix_termref_fields_form_submit($form, &$form_state){
  233. // dsm($form_state, 'form_state');
  234. $node_type = $form_state['values']['node_type']; $context['results']['field_migrated']++;
  235. $target_field = $form_state['values']['target_field'];
  236. $src_term = $form_state['values']['src_term'];
  237. variable_set('materio_admin_fix_termref_node_type', $node_type);
  238. variable_set('materio_admin_fix_termref_target_field', $target_field);
  239. variable_set('materio_admin_fix_termref_src_term', $src_term);
  240. $nids = explode(',', $form_state['values']['txt_nids']);
  241. if ($form_state['values']['op'] == 'Fixterms' && !empty($nids)){
  242. _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_term, $nids);
  243. }
  244. }
  245. function _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_tid, $nids){
  246. // Reset counter for debug information.
  247. $_SESSION['http_request_count'] = 0;
  248. $batch = array(
  249. 'title' => t('Fixing ref terms ...'),
  250. 'operations' => array(),
  251. 'init_message' => t('Commencing'),
  252. 'progress_message' => t('Processed @current out of @total.'),
  253. 'error_message' => t('An error occurred during processing'),
  254. 'finished' => '_materio_admin_batch_fixterm_ref_finished',
  255. );
  256. foreach ($nids as $nid) {
  257. $nid = trim($nid);
  258. $batch['operations'][] = array(
  259. '_materio_admin_batch_fixterm_ref_op',
  260. array(
  261. $nid,
  262. $src_tid, $target_field,
  263. $node_type,
  264. )
  265. );
  266. }
  267. batch_set($batch);
  268. }
  269. function _materio_admin_batch_fixterm_ref_op($nid, $src_tid, $target_field, $node_type){
  270. $context['results']['field_migrated']++;
  271. $node = node_load($nid);
  272. if($node->type == $node_type){
  273. $items = field_get_items('node', $node, $target_field);
  274. foreach ($items as $key => $item) {
  275. $flat_items[] = $item['tid'];
  276. }
  277. if(!in_array($src_tid, $flat_items)){
  278. // $items[] = array('tid'=>$src_tid);
  279. $node->{$target_field}[LANGUAGE_NONE][] = array('tid'=>$src_tid);
  280. }
  281. node_save($node);
  282. }
  283. //Simply show the import row count.
  284. $context['message'] = t('Migrating node !c : %title (%nid)', array(
  285. '!c' => $context['results']['field_migrated'],
  286. '%title'=>$node->title,
  287. '%nid'=>$nid ));
  288. // In order to slow importing and debug better,
  289. // we can uncomment this line to make each import slightly slower.
  290. // usleep(2500);
  291. if ( false ) {
  292. $context['results']['failed_nodes'][] = $nid ;
  293. }
  294. _materio_admin_update_http_requests();
  295. }
  296. function _materio_admin_batch_fixterm_ref_finished($success, $results, $operations){
  297. // dsm($success, 'success');
  298. // dsm($results, 'results');
  299. // dsm($operations, 'operations');
  300. if ($success) {
  301. // Here we could do something meaningful with the results.
  302. // We just display the number of nodes we processed...
  303. drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_admin_get_http_requests())));
  304. drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
  305. }
  306. else {
  307. // An error occurred.
  308. // $operations contains the operations that remained unprocessed.
  309. drupal_set_message(
  310. t('operations : @args',
  311. array(
  312. '@args' => print_r(current($operations), TRUE),
  313. )
  314. ),
  315. 'error'
  316. );
  317. $error_operation = reset($operations);
  318. drupal_set_message(
  319. t('An error occurred while processing @operation with arguments : @args',
  320. array(
  321. '@operation' => $error_operation[0],
  322. '@args' => print_r($error_operation[0], TRUE),
  323. )
  324. ),
  325. 'error'
  326. );
  327. }
  328. }
  329. /**
  330. * Utility function to increment HTTP requests in a session variable.
  331. */
  332. function _materio_admin_update_http_requests() {
  333. $_SESSION['http_request_count']++;
  334. }
  335. /**
  336. * Utility function to count the HTTP requests in a session variable.
  337. *
  338. * @return int
  339. * Number of requests.
  340. */
  341. function _materio_admin_get_http_requests() {
  342. return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
  343. }
  344. /**
  345. * Implements hook_menu_alter().
  346. */
  347. function materio_admin_menu_alter(&$items){
  348. // dsm($items, 'menu alter items');
  349. if(isset($items['admin/people'])){
  350. $items['admin/people']['access arguments'] = array('access default users list');
  351. // dsm($items['admin/people']);
  352. }
  353. if(isset($items['admin/people/expiration'])){
  354. $items['admin/people/expiration']['access arguments'] = array('access default UC roles expiration list');
  355. // dsm($items['admin/people/expiration']);
  356. }
  357. if(isset($items['user/register'])){
  358. $items['user/register']['access callback'] = FALSE;
  359. }
  360. # deactivate default home page
  361. $items['node']['access callback'] = FALSE;
  362. // if(isset($items['admin/content/add/simplenews'])){
  363. // delete($items['admin/content/add/simplenews']);
  364. // $cats = simplenews_category_list();
  365. // dsm($cats, 'cats');
  366. // foreach ($cats as $tid => $name) {
  367. // $items['admin/content/add/simplenews/'.$tid]
  368. // }
  369. // }
  370. }
  371. /**
  372. * Implements hook_menu_local_tasks_alter().
  373. */
  374. function materio_admin_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  375. if ($root_path == 'admin/people/simplenews') {
  376. $item = menu_get_item('admin/content/simplenews');
  377. if ($item['access']) {
  378. $item['title'] = 'Go to '.$item['title'];
  379. $data['actions']['output'][] = array(
  380. '#theme' => 'menu_local_task',
  381. '#link' => $item,
  382. );
  383. }
  384. }
  385. if ($root_path == 'admin/content/simplenews') {
  386. $cats = simplenews_category_list();
  387. foreach ($cats as $tid => $name) {
  388. $item = menu_get_item('node/add/simplenews/'.$tid);
  389. $item['title'] = 'Add new '.$name;
  390. if ($item['access']) {
  391. $data['actions']['output'][] = array(
  392. '#theme' => 'menu_local_task',
  393. '#link' => $item,
  394. );
  395. }
  396. }
  397. $item = menu_get_item('admin/people/simplenews');
  398. if ($item['access']) {
  399. $item['title'] = 'Go to '.$item['title'];
  400. $data['actions']['output'][] = array(
  401. '#theme' => 'menu_local_task',
  402. '#link' => $item,
  403. );
  404. }
  405. }
  406. }
  407. /**
  408. * Implements hook_form_alter().
  409. */
  410. function materio_admin_form_simplenews_node_form_alter(&$form, &$form_state, $form_id) {
  411. // dsm($form_id, '$form_id');
  412. // dsm($form_state, '$form_state');
  413. // dsm($form, '$form');
  414. // dsm($_GET, 'GET');
  415. if(!$form['nid']['#value']){
  416. $cats = simplenews_category_list();
  417. $cats_tids = array_keys($cats);
  418. $q = parse_url($_GET['q']);
  419. $cat = array_pop(explode('/', $q['path']));
  420. // dsm($cat, 'cat');
  421. if(in_array($cat, $cats_tids)){
  422. // prepopulate type of news
  423. $form['field_simplenews_term']['und']['#default_value'] = $cat;
  424. $form['field_simplenews_term']['und']['#disabled'] = true;
  425. // change default template regarding type of news
  426. $form['body']['und'][0]['#default_value'] = materio_admin_getSimplenewsNodeBodyTemplate($cat);
  427. }
  428. }else{
  429. $form['field_simplenews_term']['und']['#disabled'] = true;
  430. }
  431. $form['body']['und'][0]['#rows'] = 50;
  432. }
  433. function materio_admin_getSimplenewsNodeBodyTemplate($cat){
  434. return file_get_contents(drupal_get_path('module', 'materio_admin').'/templates/simplenews_'.$cat.'_node.html');
  435. }
  436. // http://stackoverflow.com/questions/19202449/change-drupal-7-compiled-css-and-js-includes-to-use-https-as-opposed-to-http
  437. function materio_admin_process_html(&$vars){
  438. foreach (array('head', 'styles', 'scripts') as $replace) {
  439. if (!isset($vars[$replace])) {
  440. continue;
  441. }
  442. $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
  443. }
  444. }