3, 'path' => drupal_get_path('module', 'drupal_extra') . '/includes', ); } /** * Implementation of hook_views_query_alter * @param type $view * @param type $query */ function drupal_extra_views_query_alter(&$view, &$query) { if (is_array($view->argument)) { $count = 0; foreach ($view->argument as $k => $v) { __drupal_extra_views_query_alter($view, $query, $v, $count++); } } if (is_array($view->filter)) { $count = 0; foreach ($view->filter as $k => $v) { __drupal_extra_views_query_alter($view, $query, $v, $count++); } } if (is_array($view->relationship)) { $count = 0; foreach ($view->relationship as $k => $v) { // dextra|table:field_all_turns FEATURE __drupal_extra_views_query_alter($view, $query, $v, $count++); } } } function __drupal_extra_views_query_alter(&$view, &$query, $v, $argument_key) { if (isset($v->options['ui_name']) && $v->options['ui_name'] && substr($v->options['ui_name'], 0, 6) == 'dextra') { $options = drupal_extra_to_options($v->options['ui_name']); if (isset($options['equal']) || isset($options['contain'])) { $field = $v->table_alias . '.' . $v->field; foreach ($query->where as $key => $cond) { if (!isset($cond['conditions'])) { continue ; } foreach ($cond['conditions'] as $subkey => $subcond) { if (!isset($subcond['field'])) { continue ; } if ($subcond['field'] == $field && $subcond['operator'] == 'IS NOT NULL') { $query->where[$key]['conditions'][$subkey]['operator'] = 'LIKE'; $pos = isset($options['join'][3]) ? $options['join'][3] : 0; $date = drupal_extra_views_get_argument_smart($view, 'date', $pos); $query->where[$key]['conditions'][$subkey]['value'] = '%' . $date; } } } } if (isset($options['table'])) { $table = $v->table;// Consider to use $v->alias Could buggy here if (isset($query->table_queue[$table])) { $query->table_queue[$table]['join']->table = $query->table_queue[$table]['table'] = $options['table']; } } if (isset($options['morejoin']) || isset($options['converttosingletable'])) { $remove_data_field = isset($v->first_alias) ? $v->first_alias : 'field_data_field_agent'; unset($query->table_queue[$remove_data_field]); $table = $v->alias;// Consider to use $v->alias Could buggy here if (isset($query->table_queue[$table])) { $query->table_queue[$table]['join']->table = $query->table_queue[$table]['table'] = 'eck_daily_report_agents'; $query->table_queue[$table]['join']->left_table = 'users'; $query->table_queue[$table]['join']->left_field = 'uid'; $query->table_queue[$table]['join']->field = 'field_agent_target_id'; } } if (isset($options['join'])) { $table = $v->alias; $pos = isset($options['join'][3]) ? $options['join'][3] : 0; $date = drupal_extra_views_get_argument_smart($view, 'date', $pos); $query->table_queue[$table]['join']->extra = array('' . $table . '.title LIKE \'%'. $date .'\''); } if (isset($options['fieldcompare'])) { _scan_conditions($query, $v, '__fieldcompare_valid', '__fieldcompare_execute'); } if (isset($options['addor'])) { _scan_table_queue($query, $v, '__addor_table_valid', '__addor_table_execute', $options); _scan_conditions($query, $v, '__addor_valid', '__addor_execute', $options); } } } function __addor_table_valid($subcond, $field) { return $subcond['table'] == $field->table; } function __addor_table_execute(&$subcond, $field, $key, $options) { $subcond['join']->extra[] = array( 'field' => $options['addor'][1], 'value' => $options['addor'][2], 'numeric' => TRUE, ); } function __addor_valid($subcond, $field) { $key = $field->table_alias . '.' . $field->real_field; return (strpos($subcond['field'], $key) !== FALSE); } function __addor_execute(&$subcond, $field) { $key = $field->table_alias . '.' . $field->real_field; $subcond['field'] = '('. $key .' IS NULL) OR ('. $subcond['field'] .')'; } function __fieldcompare_valid($subcond) { return is_string($subcond["field"]) && strpos($subcond["field"], ".field_people_category_tid") !== FALSE; } function __fieldcompare_execute(&$subcond) { if ($subcond["operator"] == "formula") { if (is_array($subcond["value"])) { foreach ($subcond["value"] as $key => $val_arr) { if (!is_array($val_arr)) { $val_arr = array($val_arr); } foreach ($val_arr as &$val) { $val = "(". $val .")"; } $subcond["value"][$key] = implode("|", $val_arr); } } $subcond["field"] = str_replace(".field_people_category_tid",".field_people_category_merged", $subcond["field"]); $subcond["field"] = str_replace("("," ", $subcond["field"]); $subcond["field"] = str_replace(")","", $subcond["field"]); $subcond["field"] = str_replace(" ="," REGEXP", $subcond["field"]); $subcond["field"] = str_replace(" !="," NOT REGEXP", $subcond["field"]); $subcond["field"] = str_replace(" IN"," REGEXP", $subcond["field"]); return ; } $subcond["field"] = str_replace(".field_people_category_tid",".field_people_category_merged", $subcond["field"]); $subcond["value"] = "%(". $subcond["value"] .")%"; $subcond["operator"] = "LIKE"; } function _scan_table_queue($query, $field, $matchcallback, $callback, $options = array()) { foreach ($query->table_queue as $key => $cond) { if (!isset($cond['join'])) { continue ; } if ($matchcallback($cond, $field, $options)) { $callback($query->table_queue[$key], $field, $key, $options); } } } function _scan_conditions($query, $field, $matchcallback, $callback, $options = array()) { foreach ($query->where as $key => $cond) { if (!isset($cond['conditions'])) { continue ; } foreach ($cond['conditions'] as $subkey => $subcond) { if (!isset($subcond['field'])) { continue ; } if ($matchcallback($subcond, $field)) { $callback($query->where[$key]['conditions'][$subkey], $field, $key, $subkey); } } } } function drupal_extra_views_get_argument_smart($view, $key, $argument_key = 0) { $date = isset($view->args[$argument_key]) ? $view->args[$argument_key] : ''; if (isset($view->exposed_input[$key])) { $date = $view->exposed_input[$key]; } else if (isset($_GET[$key])) { $date = $_GET[$key]; } return $date; } function drupal_extra_to_options($string) { list(, $options) = strpos($string, "|") !== FALSE ? explode("|", $string) : array(0, $string); $options = explode(",", $options); $options = array_map("trim", $options); $options = array_reduce($options, function ($result, $item) { $item = explode(":", $item); $result[$item[0]] = isset($item[2]) ? $item : $item[1]; return $result; }, array()); return $options; } /** * Implement of hook_conditional_fields_states_alter */ function drupal_extra_conditional_fields_states_alter(&$states) { $states['hideselectedoption'] = t('Hide selected option'); $states['!hideselectedoption'] = t('Hide others but selected option'); } /** * Implement of hook_conditional_fields_conditions_alter */ function drupal_extra_conditional_fields_conditions_alter(&$conditions) { $conditions['valuechange'] = t('Value is changed'); // Like states, conditions get a converse automatically. $conditions['!valuechange'] = t('Value is not changed'); } /** * Implement of hook_js_alter */ function drupal_extra_js_alter(&$javascript) { _help_add_js($javascript, "misc/ajax.js", drupal_get_path("module", "drupal_extra") . "/ajax.extra.js"); _help_add_js($javascript, drupal_get_path("module", "conditional_fields") . "/js/conditional_fields.js", drupal_get_path("module", "drupal_extra") . "/conditional_fields_extra.js"); } /** * Helper hook_js_alter */ function _help_add_js(&$javascript, $depend, $ajax_extra) { if (array_key_exists($depend, $javascript)) { $javascript[$ajax_extra] = $javascript[$depend]; $javascript[$ajax_extra]['data'] = $ajax_extra; $javascript[$ajax_extra]['weight']++; } } function computed_field_recalculate_field_simple($entity, $type, $field) { static $info; $info = isset($info) ? $info : entity_get_info(); return computed_field_recalculate_field($info[$type]['base table'], $type, $entity, $field); } // computed_field_recalculate_field("users", "user", user_load(1), 'field_balance'); function computed_field_recalculate_field($table, $type, $entity, $field_name) { static $calculated; $info = entity_get_info(); $idkey = $info[$type]['entity keys']['id']; $bundlekey = $info[$type]['entity keys']['bundle']; if (!isset($entity->{$bundlekey})) { $bundle = $type; } else { $bundle = $entity->{$bundlekey}; } $entities_by_type = array($type => array($bundle)); $entities_per_run = 1; $itemkey = $table . $type . $entity->{$idkey} . $field_name; if (isset($calculated[$itemkey])) { return ; } $calculated[$itemkey] = TRUE; $context = array(); $context['sandbox']['offsets_by_type'] = array(); $context['sandbox']['offsets_by_type'][$type]['offset'] = _help_offset_entity($table, $type, $entity, $field_name, $bundle); $context['sandbox']['offsets_by_type'][$type]['types'] = array($bundle); $context['sandbox']['max_ids'] = array($type => $entity->{$idkey}); $context['results']['total_entities_touched'] = 0; $context['results']['start'] = REQUEST_TIME; $context['results']['end'] = 0; $context['results']['total_id_count'] = 1; _computed_field_tools_batch_recompute($entities_by_type, $field_name, $entities_per_run, $context); } function _help_offset_entity($table, $type, $entity, $field_name, $bundle) { if ($type != 'taxonomy_term') { return 0; } $vids = db_select('taxonomy_vocabulary', 'tv') ->fields('tv', array('vid')) ->condition('machine_name', array($bundle), 'IN') ->execute() ->fetchCol(); $query = db_select('taxonomy_term_data', 'n') ->condition('n.tid', $entity->tid, '<') ->condition('vid', $vids, 'IN') ->orderBy('n.tid', 'ASC'); return $query->countQuery()->execute()->fetchField(); } function drupal_extra_create_field_all_view($fieldname, $key) { return db_query("CREATE VIEW field_data_{$fieldname}_all AS SELECT *, GROUP_CONCAT(DISTINCT CONCAT('(', {$fieldname}_{$key}, ')') SEPARATOR '+') as {$fieldname}_merged FROM {field_data_{$fieldname}} GROUP BY entity_id"); } function __get_results_key($view) { $key_data = array( 'get_exposed_input' => $view->get_exposed_input(), 'args' => $view->args, ); return $view->name . ':' . $view->current_display . ':results:' . md5(serialize($key_data)); } function _de_views_get_view_result_first_cached_data($key, $setdata = FALSE, $reset = FALSE) { $data = &drupal_static(__FUNCTION__); $uniquekey = "dr2:" . $key; // If this is a reset if ($reset) { unset($data[$uniquekey]); cache_clear_all($uniquekey, 'cache'); } // If this is a GET from cache if ($setdata === FALSE) { // Get from db if (!isset($data) || !isset($data[$uniquekey])) { if (($cache = cache_get($uniquekey)) && ((!$cache->expire) || REQUEST_TIME < $cache->expire)) { $data[$uniquekey] = $cache->data; } } return isset($data[$uniquekey]) ? $data[$uniquekey] : ""; } // If this is a SET $data[$uniquekey] = $setdata; cache_set($uniquekey, $setdata); return FALSE; } function de_views_get_view_result_first_cached($key, $name, $display_id = NULL, $exposed_filters = array()) { $args = func_get_args(); array_shift($args); array_shift($args); // remove $key, $name if (count($args)) { array_shift($args); // remove $display_id array_shift($args); // remove $exposed_filters } // Performance debug $view = views_get_view($name); if (count($args)) { $view->set_arguments($args); } $view->set_display($display_id); $view->set_exposed_input($exposed_filters); $cached_key = __get_results_key($view); $result = _de_views_get_view_result_first_cached_data($cached_key); if (isset($_GET['debug']) || drupal_is_cli()) { global $debugged; if (!isset($debugged) || $debugged <= 2) { $key_data = array( 'get_exposed_input' => $view->get_exposed_input(), 'args' => $view->args, 'result' => $result ); $debugged = !isset($debugged) ? 1 : $debugged + 1; dsm($view->result); dsm($key_data); //-----------DEBUGGING---------- watchdog('DEBUG', __FUNCTION__ . print_r($key_data, TRUE)); //-----------END DEBUGGING---------- } } if ($result === "") { $view->pre_execute(); $view->execute(); _de_views_get_view_result_first_cached_data($cached_key, $view->result); if (isset($view->result) && $view->result) { $result = $view->result; } } if ($result) { $tmp = current($result); return $key ? $tmp->{$key} : current($tmp); } return 0; } function de_views_get_view_result_first($key, $name, $display_id = NULL, $exposed_filters = array()) { $args = func_get_args(); array_shift($args); $r = call_user_func_array("de_views_get_view_result", $args); if ($r) { $tmp = current($r); return $key ? $tmp->{$key} : current($tmp); } return 0; } function de_views_get_view_result($name, $display_id = NULL, $exposed_filters = array()) { $args = func_get_args(); array_shift($args); // remove $name if (count($args)) { array_shift($args); // remove $display_id array_shift($args); // remove $exposed_filters } $view = views_get_view($name); if (is_object($view)) { if (is_array($args)) { $view->set_arguments($args); } if (is_string($display_id)) { $view->set_display($display_id); } else { $view->init_display(); } $view->set_exposed_input($exposed_filters); $view->pre_execute(); $view->execute(); return $view->result; } else { return array( ); } } function de_views_embed_view($name, $display_id = 'default', $items_per_page = '') { $args = func_get_args(); array_shift($args); // remove $name if (count($args)) { array_shift($args); // remove $display_id } // remove $items_per_page if (count($args)) { array_shift($args); } $view = views_get_view($name); if (!$view || !$view->access($display_id)) { return; } if ($items_per_page) { // $view->set_display($display_id); // $view->display_handler->set_option('items_per_page', $items_per_page); $view->set_items_per_page($items_per_page); } return $view->preview($display_id, $args); }