123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- <?php
- /**
- * Implements hook_permission().
- */
- function materio_admin_permission() {
- return array(
- 'access default users list' => array(
- 'title' => t('Access default users list'),
- 'description' => t('Access default users list.'),
- ),
- 'access default UC roles expiration list' => array(
- 'title' => t('access default UC roles expiration list'),
- 'description' => t('access default UC roles expiration list.'),
- ),
- 'access duplicate mails list' => array(
- 'title' => t('access duplicate mails list'),
- 'description' => t('access duplicate mails list.'),
- ),
- 'access missing flags list' => array(
- 'title' => t('access missing flags list'),
- 'description' => t('access missing flags list.'),
- ),
- 'materio admin fix term reference field' => array(
- 'title' => t('Materio admin fix term reference field'),
- 'description' => t('Materio admin fix term reference field.'),
- ),
- );
- }
- /**
- * Implements hook_menu().
- */
- function materio_admin_menu() {
- $items['admin/people/duplicatemails'] = array(
- 'title' => "Duplicate mails",
- 'page callback' => 'materio_duplicatemails',
- 'access callback' => 'user_access',
- 'access arguments' => array('access duplicate mails list'),
- 'type' => MENU_LOCAL_TASK
- );
- if(module_exists('simplenews')){
- $cats = simplenews_category_list();
- // dsm($cats, 'cats');
- foreach ($cats as $tid => $name) {
- $items['node/add/simplenews/'.$tid] = array(
- 'title' => $name,
- 'title callback' => 'check_plain',
- 'page callback' => 'node_add',
- 'page arguments' => array('simplenews'),
- 'access callback' => 'node_access',
- 'access arguments' => array('create', 'simplenews'),
- 'file path' => drupal_get_path('module', 'node'),
- 'file' => 'node.pages.inc',
- );
- }
- }
- if(module_exists('flag') && module_exists('flag_lists')){
- $items['admin/people/missingflags'] = array(
- 'title' => "Missing Flags",
- 'page callback' => 'materio_missingflags',
- 'access callback' => 'user_access',
- 'access arguments' => array('access missing flags list'),
- 'type' => MENU_LOCAL_TASK
- );
- }
- // fix term ref field lost with taxonomy
- $items['admin/config/content/materio'] = array(
- 'title' => 'Materio',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('materio_admin_fix_termref_fields_form'),
- 'access arguments' => array('materio admin fix term reference field'),
- 'type' => MENU_NORMAL_ITEM,
- );
- $items['admin/config/content/materio/fix_termref_fields'] = array(
- 'title' => 'Fix term reference field from taxonomy',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- );
- return $items;
- }
- function materio_duplicatemails(){
- $mails = db_query('SELECT mail FROM {users} GROUP BY mail HAVING count(mail) > 1')->fetchCol();
- // Bail out early if there are no duplicates.
- if (!$mails) {
- return t('All accounts have unique email addresses.');
- }
- // Grab all the user data for accounts with addresses identified as
- // duplicates. This is a little convoluted, but it lets us grab all the user
- // data in one shot.
- $uids = db_select('users', 'u')
- ->fields('u', array('uid'))
- ->condition('mail', $mails, 'IN')
- ->orderBy('access', 'DESC')
- ->execute()
- ->fetchCol();
- $duplicate_users = user_load_multiple($uids);
- $duplicate_mails = array();
- foreach ($duplicate_users as $duplicate_user) {
- $duplicate_mails[$duplicate_user->mail][] = $duplicate_user;
- }
- // Turn the data we've got into markup.
- $output = t('Accounts with duplicate email address:') . '<br />';
- $output = "<ul>";
- foreach ($duplicate_mails as $mail => $users) {
- $output .= "<li> <strong>$mail</strong> : <br />";
- $accounts = array();
- foreach ($users as $duplicate_user) {
- $accounts[] = l($duplicate_user->name, "user/{$duplicate_user->uid}") . ' ('. date('Y-m-d', $duplicate_user->access) .')';
- }
- $output .= implode(', ', $accounts);
- $output .= '</li>';
- }
- $output .= "</ul>";
- return $output;
- }
- function materio_missingflags(){
- $flags = db_query('SELECT fcid,fid,entity_type,entity_id,uid FROM {flag_lists_content}');
- $missingentities = 0;
- foreach ($flags as $key => $value) {
- // $entity = entity_load($value->entity_type, array($value->entity_id));
- $query = new EntityFieldQuery();
- $query
- ->entityCondition('entity_type', $value->entity_type)
- ->propertyCondition('nid', $value->entity_id);
- $entity = $query->execute();
- if(!isset($entity[$value->entity_type])){
- // $missingentities[$value->fid] = $value;
- $missingentities++;
- db_delete('flag_lists_content')
- ->condition('fcid', $value->fcid)
- ->execute();
- watchdog('flag_lists', t('Deleted entry @fcid from flat_lists_content', array('@fcid' => $value->fcid)));
- }
- }
- // Bail out early if there are no duplicates.
- if (!$missingentities) {
- return t('No missing entities in flags.');
- }
- // else{
- // dsm($missingentities);
- // }
- // delete flags with missing entities
- // foreach ($missingentities as $fid => $value) {
- //
- // }
- // Turn the data we've got into markup.
- $output = t('@count missing entities in flag lists have been cleaned', array('@count'=> count($missingentities)));
- return $output;
- }
- function materio_admin_fix_termref_fields_form(){
- drupal_set_title('Fix term reference fields', PASS_THROUGH);
- $types = node_type_get_types();
- // dsm($types, 'types');
- $nt_options = array();
- foreach ($types as $mn => $type) {
- $nt_options[$mn] = $type->name;
- }
- $node_type = variable_get('materio_admin_fix_termref_node_type', null);
- // source field (must be a text field)
- $form['node_type'] = array(
- '#type'=>'select',
- '#options'=>$nt_options,
- '#default_value' => $node_type,
- '#title' => t('Content type'),
- '#multiple' => false,
- );
- if($node_type){
- $fieldsmap = field_info_field_map();
- $target_options = array();
- foreach ($fieldsmap as $field_name => $field) {
- // dsm($field, $field_name);
- if ($field['type'] == 'taxonomy_term_reference'
- && isset($field['bundles']['node'])
- && in_array($node_type, $field['bundles']['node'])) {
- $target_options[$field_name] = $field_name;
- }
- }
- $target_field = variable_get('materio_admin_fix_termref_target_field', null);
- // target field (must be a entity_reference field)
- $form['target_field'] = array(
- '#type'=>'select',
- '#options'=>$target_options,
- '#default_value' => $target_field,
- '#title' => t('target field (must be a term_reference field)'),
- '#multiple' => false,
- );
- if($target_field){
- // retrive various field infos
- $field_info = field_info_field($target_field);
- // dsm($field_info, 'field_info');
- // $field_instance = field_info_instance('node', $target_field, $node_type);
- // dsm($field_instance, 'field_instance');
- $voc_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
- $vid = taxonomy_vocabulary_machine_name_load($voc_name)->vid;
- $voc = taxonomy_vocabulary_load($vid);
- $tree = taxonomy_get_tree($vid);
- // dsm($tree, 'tree');
- foreach ($tree as $key => $term) {
- $terms[$term->tid] = $term->name;
- }
- $src_term = variable_get('materio_admin_fix_termref_src_term', null);
- // src term
- $form['src_term'] = array(
- '#type'=>'select',
- '#options'=>$terms,
- '#default_value' => $src_term,
- '#title' => t('source term'),
- '#multiple' => false,
- );
- if($src_term){
- $form['txt_nids'] = array(
- '#type'=>'textarea',
- '#title' => t('nids to fix'),
- '#description' => t('comma separated nids list'),
- );
- $form['fixterms'] = array(
- '#type' => 'submit',
- '#value' => 'Fixterms',
- );
- // $nodes = taxonomy_select_nodes($src_term, false);
- // dsm($nodes, 'nodes');
- // $src_items = field_get_items('node', node_load(11858), $target_field);
- // dsm($src_items);
- }
- }
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Save',
- // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
- );
- return $form;
- }
- function materio_admin_fix_termref_fields_form_submit($form, &$form_state){
- // dsm($form_state, 'form_state');
- $node_type = $form_state['values']['node_type']; $context['results']['field_migrated']++;
- $target_field = $form_state['values']['target_field'];
- $src_term = $form_state['values']['src_term'];
- variable_set('materio_admin_fix_termref_node_type', $node_type);
- variable_set('materio_admin_fix_termref_target_field', $target_field);
- variable_set('materio_admin_fix_termref_src_term', $src_term);
- $nids = explode(',', $form_state['values']['txt_nids']);
- if ($form_state['values']['op'] == 'Fixterms' && !empty($nids)){
- _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_term, $nids);
- }
- }
- function _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_tid, $nids){
- // Reset counter for debug information.
- $_SESSION['http_request_count'] = 0;
- $batch = array(
- 'title' => t('Fixing ref terms ...'),
- 'operations' => array(),
- 'init_message' => t('Commencing'),
- 'progress_message' => t('Processed @current out of @total.'),
- 'error_message' => t('An error occurred during processing'),
- 'finished' => '_materio_admin_batch_fixterm_ref_finished',
- );
- foreach ($nids as $nid) {
- $nid = trim($nid);
- $batch['operations'][] = array(
- '_materio_admin_batch_fixterm_ref_op',
- array(
- $nid,
- $src_tid, $target_field,
- $node_type,
- )
- );
- }
- batch_set($batch);
- }
- function _materio_admin_batch_fixterm_ref_op($nid, $src_tid, $target_field, $node_type){
- $context['results']['field_migrated']++;
- $node = node_load($nid);
- if($node->type == $node_type){
- $items = field_get_items('node', $node, $target_field);
- foreach ($items as $key => $item) {
- $flat_items[] = $item['tid'];
- }
- if(!in_array($src_tid, $flat_items)){
- // $items[] = array('tid'=>$src_tid);
- $node->{$target_field}[LANGUAGE_NONE][] = array('tid'=>$src_tid);
- }
- node_save($node);
- }
- //Simply show the import row count.
- $context['message'] = t('Migrating node !c : %title (%nid)', array(
- '!c' => $context['results']['field_migrated'],
- '%title'=>$node->title,
- '%nid'=>$nid ));
- // In order to slow importing and debug better,
- // we can uncomment this line to make each import slightly slower.
- // usleep(2500);
- if ( false ) {
- $context['results']['failed_nodes'][] = $nid ;
- }
- _materio_admin_update_http_requests();
- }
- function _materio_admin_batch_fixterm_ref_finished($success, $results, $operations){
- // dsm($success, 'success');
- // dsm($results, 'results');
- // dsm($operations, 'operations');
- if ($success) {
- // Here we could do something meaningful with the results.
- // We just display the number of nodes we processed...
- drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_admin_get_http_requests())));
- drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
- }
- else {
- // An error occurred.
- // $operations contains the operations that remained unprocessed.
- drupal_set_message(
- t('operations : @args',
- array(
- '@args' => print_r(current($operations), TRUE),
- )
- ),
- 'error'
- );
- $error_operation = reset($operations);
- drupal_set_message(
- t('An error occurred while processing @operation with arguments : @args',
- array(
- '@operation' => $error_operation[0],
- '@args' => print_r($error_operation[0], TRUE),
- )
- ),
- 'error'
- );
- }
- }
- /**
- * Utility function to increment HTTP requests in a session variable.
- */
- function _materio_admin_update_http_requests() {
- $_SESSION['http_request_count']++;
- }
- /**
- * Utility function to count the HTTP requests in a session variable.
- *
- * @return int
- * Number of requests.
- */
- function _materio_admin_get_http_requests() {
- return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
- }
- /**
- * Implements hook_menu_alter().
- */
- function materio_admin_menu_alter(&$items){
- // dsm($items, 'menu alter items');
- if(isset($items['admin/people'])){
- $items['admin/people']['access arguments'] = array('access default users list');
- // dsm($items['admin/people']);
- }
- if(isset($items['admin/people/expiration'])){
- $items['admin/people/expiration']['access arguments'] = array('access default UC roles expiration list');
- // dsm($items['admin/people/expiration']);
- }
- if(isset($items['user/register'])){
- $items['user/register']['access callback'] = FALSE;
- }
- # deactivate default home page
- $items['node']['access callback'] = FALSE;
- // if(isset($items['admin/content/add/simplenews'])){
- // delete($items['admin/content/add/simplenews']);
- // $cats = simplenews_category_list();
- // dsm($cats, 'cats');
- // foreach ($cats as $tid => $name) {
- // $items['admin/content/add/simplenews/'.$tid]
- // }
- // }
- }
- /**
- * Implements hook_menu_local_tasks_alter().
- */
- function materio_admin_menu_local_tasks_alter(&$data, $router_item, $root_path) {
- if ($root_path == 'admin/people/simplenews') {
- $item = menu_get_item('admin/content/simplenews');
- if ($item['access']) {
- $item['title'] = 'Go to '.$item['title'];
- $data['actions']['output'][] = array(
- '#theme' => 'menu_local_task',
- '#link' => $item,
- );
- }
- }
- if ($root_path == 'admin/content/simplenews') {
- $cats = simplenews_category_list();
- foreach ($cats as $tid => $name) {
- $item = menu_get_item('node/add/simplenews/'.$tid);
- $item['title'] = 'Add new '.$name;
- if ($item['access']) {
- $data['actions']['output'][] = array(
- '#theme' => 'menu_local_task',
- '#link' => $item,
- );
- }
- }
- $item = menu_get_item('admin/people/simplenews');
- if ($item['access']) {
- $item['title'] = 'Go to '.$item['title'];
- $data['actions']['output'][] = array(
- '#theme' => 'menu_local_task',
- '#link' => $item,
- );
- }
- }
- }
- /**
- * Implements hook_form_alter().
- */
- function materio_admin_form_simplenews_node_form_alter(&$form, &$form_state, $form_id) {
- // dsm($form_id, '$form_id');
- // dsm($form_state, '$form_state');
- // dsm($form, '$form');
- // dsm($_GET, 'GET');
- if(!$form['nid']['#value']){
- $cats = simplenews_category_list();
- $cats_tids = array_keys($cats);
- $q = parse_url($_GET['q']);
- $cat = array_pop(explode('/', $q['path']));
- // dsm($cat, 'cat');
- if(in_array($cat, $cats_tids)){
- // prepopulate type of news
- $form['field_simplenews_term']['und']['#default_value'] = $cat;
- $form['field_simplenews_term']['und']['#disabled'] = true;
- // change default template regarding type of news
- $form['body']['und'][0]['#default_value'] = materio_admin_getSimplenewsNodeBodyTemplate($cat);
- }
- }else{
- $form['field_simplenews_term']['und']['#disabled'] = true;
- }
- $form['body']['und'][0]['#rows'] = 50;
- }
- function materio_admin_getSimplenewsNodeBodyTemplate($cat){
- return file_get_contents(drupal_get_path('module', 'materio_admin').'/templates/simplenews_'.$cat.'_node.html');
- }
- // http://stackoverflow.com/questions/19202449/change-drupal-7-compiled-css-and-js-includes-to-use-https-as-opposed-to-http
- function materio_admin_process_html(&$vars){
- foreach (array('head', 'styles', 'scripts') as $replace) {
- if (!isset($vars[$replace])) {
- continue;
- }
- $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
- }
- }
|