. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ require_once('mica_core.features.inc'); require_once('mica_core.menu.utils.inc'); require_once(drupal_get_path('module', 'taxonomy_csv') . '/import/taxonomy_csv.import.api.inc'); /** * Implements hook_form(). */ function mica_core_form($node, &$form_state) { return node_content_form($node, $form_state); } function mica_core_node_title_exists($node_type, $title) { $q = db_select('node', 'n'); $q->addField('n', 'nid'); $q->condition('n.type', $node_type); $q->condition('n.title', $title); $q->range(0, 1); $result = $q->execute(); return $result->fetchField() ? TRUE : FALSE; } /** * Implements hook_pathauto_punctuation_chars_alter */ function mica_core_pathauto_punctuation_chars_alter(array &$punctuation) { $punctuation['raquo'] = array('value' => 'ยป', 'name' => t('Right double angle quotes')); } /** * Implements hook_menu(). */ function mica_core_menu() { $items = array(); $items['mica/%ctools_js/node/%node'] = array( 'page callback' => 'mica_core_node_modal_page_view', 'page arguments' => array(1, 3), 'access callback' => 'node_access', 'access arguments' => array('view', 3), ); return $items; } // // Modal helper functions // /** * Declare styles for Mica modal dialogs. * @param $styles array of styles: name (required) and width, height (required for a dialog with a fixed size) */ function mica_core_modal_prepare($styles) { // Include the CTools tools that we need. ctools_include('ajax'); ctools_include('modal'); // Add CTools' javascript to the page. ctools_modal_add_js(); $style_settings = array(); // Create our own javascript that will be used to theme a modal. foreach ($styles as $style) { $settings = array(); if (!empty($style['width']) && !empty($style['height'])) { $settings['modalSize'] = array( 'type' => 'fixed', 'width' => $style['width'], 'height' => $style['height'], 'addWidth' => 20, 'addHeight' => 15, ); } else { $settings['modalSize'] = array( 'type' => 'scale', 'addWidth' => 20, 'addHeight' => 15, ); } $settings['modalOptions'] = array( 'opacity' => .5, 'background-color' => '#000', ); $settings['animation'] = 'fadeIn'; $settings['modalTheme'] = 'MicaModal'; $style_settings[$style['name']] = $settings; } drupal_add_js($style_settings, 'setting'); // Since we have our js, css and images in well-known named directories, // CTools makes it easy for us to just use them without worrying about using drupal_get_path() and all that ugliness. ctools_add_js('mica_core_modal', 'mica_core'); ctools_add_css('mica_core_modal', 'mica_core'); } /** * Renders the form identified by $form_id in a modal dialog, with provided title. Additional arguments are added to the $form_state. * @param $title * @param $form_id */ function mica_core_modal_form_render($title, $form_id) { ctools_include('ajax'); ctools_include('modal'); ctools_add_js('ajax-responder'); $args = func_get_args(); // Remove $title and $form_id from the arguments. array_shift($args); array_shift($args); $form_state = array( 'ajax' => TRUE, 'title' => $title, ); $form_state['build_info']['args'] = $args; $commands = ctools_modal_form_wrapper($form_id, $form_state); // If cancelling or complete, return to the activity. if (!empty($form_state['complete'])) { $commands = array(); $commands[] = ctools_modal_command_dismiss(); if (!empty($form_state['redirect'])) { $commands[] = ctools_ajax_command_redirect($form_state['redirect']); } } elseif (!empty($form_state['cancel'])) { $commands = array(); $commands[] = ctools_modal_command_dismiss(); } print ajax_render($commands); } /** * Renders the node page in a modal window. Use mica_core_modal_prepare() to set up a link to the node page. * @param $js * @param $node */ function mica_core_node_modal_page_view($js = NULL, $node) { require_once(drupal_get_path('module', 'ctools') . '/page_manager/plugins/tasks/node_view.inc'); if ($js) { ctools_include('ajax'); ctools_include('modal'); ctools_modal_render($node->title, page_manager_node_view_page($node)); } else { return page_manager_node_view_page($node); } } /** * Implements hook_form_alter() to fix the issue where selected facets were lost when executing a full text search. * See http://drupal.org/sandbox/pl2/1438442 */ function mica_core_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'views_exposed_form': // Full text search form id foreach ($form_state['input'] as $input_key => $value) { // If there is an argument in input that is not in the keys of the form // then that argument must be added as a hidden field to the form. if (!isset($form[$input_key])) { _mica_core_persist_url_params_param_from_array($form, $input_key, $input_key, $value); } } break; } } /** * Converts an array that was previously parsed from the url arguments * to an array of hidden values that will be added to the form so these arguments * are not lost in form submission. * See http://drupal.org/sandbox/pl2/1438442 */ function _mica_core_persist_url_params_param_from_array(&$form, $root_key, $key, $value, $array_key = '') { if (!is_array($value)) { $form[$key] = array( '#type' => 'hidden', '#value' => $value, '#name' => "$root_key$array_key", ); } else { $form[$key] = array(); foreach ($value as $val_key => $val_value) { _mica_core_persist_url_params_param_from_array($form[$key], $root_key, $val_key, $val_value, $array_key . "[$val_key]"); } } } /** * Implements hook_field_formatter_info(). */ function mica_core_field_formatter_info() { return array( // This formatter just displays the link to pubmed article from its id. 'mica_pubmed_url' => array( 'label' => t('Pubmed Link'), 'field types' => array('text', 'number_integer'), 'multiple values' => FIELD_BEHAVIOR_DEFAULT, ), 'node_reference_url_or_plain' => array( 'label' => t('Title (link for published item or plain text for unpublished)'), 'description' => t('Display the title of the referenced node as a link to the node page for published items or as plain text for unpublished. Does not check node access!'), 'field types' => array('node_reference'), ), ); } /** * Implements hook_field_formatter_view(). */ function mica_core_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); switch ($display['type']) { case 'mica_pubmed_url': foreach ($items as $delta => $item) { $element[$delta] = array('#markup' => l('PUBMED ' . $item['value'], 'http://www.ncbi.nlm.nih.gov/pubmed/' . $item['value'])); } break; case 'node_reference_url_or_plain': foreach ($items as $delta => $item) { $node = node_load($item['nid']); $label = entity_label('node', $node); if ($item['access']) { $uri = entity_uri('node', $node); $element[$delta] = array( '#type' => 'link', '#title' => $label, '#href' => $uri['path'], '#options' => $uri['options'], ); } else { $element[$delta] = array( '#markup' => check_plain($label), ); } } break; } return $element; } /** * Implements hook_entity_info_alter(). */ function mica_core_entity_info_alter(&$entity_info) { _mica_core_add_view_mode($entity_info, 'banner_area', 'Banner Area'); _mica_core_add_view_mode($entity_info, 'left_area', 'Left Area'); _mica_core_add_view_mode($entity_info, 'right_area', 'Right Area'); _mica_core_add_view_mode($entity_info, 'center_area', 'Center Area'); _mica_core_add_view_mode($entity_info, 'footer_area', 'Footer Area'); _mica_core_add_view_mode($entity_info, 'summary_area', 'Summary'); _mica_core_add_view_mode($entity_info, 'summary_banner_area', 'Summary Banner Area'); _mica_core_add_view_mode($entity_info, 'summary_left_area', 'Summary Left Area'); _mica_core_add_view_mode($entity_info, 'summary_right_area', 'Summary Right Area'); _mica_core_add_view_mode($entity_info, 'summary_center_area', 'Summary Center Area'); _mica_core_add_view_mode($entity_info, 'summary_footer_area', 'Summary Footer Area'); } function _mica_core_add_view_mode(&$entity_info, $mode, $label) { if (!isset($entity_info['node']['view modes'][$mode])) { $entity_info['node']['view modes'][$mode] = array( 'label' => t($label), 'custom settings' => FALSE, ); } } /** * Implements hook_feeds_plugins(). */ function mica_core_feeds_plugins() { $info = array(); $info['FeedsRestFetcher'] = array( 'name' => 'RESTful HTTP Fetcher', 'description' => 'Download content from a RESTful web service.', 'handler' => array( 'parent' => 'FeedsFetcher', 'class' => 'FeedsRestFetcher', 'file' => 'FeedsRestFetcher.inc', 'path' => drupal_get_path('module', 'mica_core') . '/includes/feeds', ), ); return $info; } /** * Cannot export global default_feeds_tamper via strongarm in each modules, so do it manually */ function mica_core_configure_feeds_tamper($module_name) { $disabled = variable_get('default_feeds_tamper', array()); $info = drupal_parse_info_file(drupal_get_path('module', $module_name) . '/' . $module_name . '.info'); foreach ($info['features']['feeds_tamper'] as $feeds_tamper) { $disabled[$feeds_tamper] = FALSE; } variable_set('default_feeds_tamper', $disabled); } function _mica_core_taxonomy_create($filename, $vocabulary_name) { // use filename (without extension) for vocabulary machine name $machine_name = substr($filename, 0, strlen($filename) - 4); $vocabulary = (object) array( 'name' => $vocabulary_name, 'machine_name' => $machine_name, 'vid' => NULL, ); $taxonomies = taxonomy_vocabulary_get_names(); if (array_key_exists($machine_name, $taxonomies)) { $vocabulary->vid = $taxonomies[$machine_name]->vid; } else { taxonomy_vocabulary_save($vocabulary); } return $vocabulary; } /** * Create taxonomy from a csv file */ function mica_core_taxonomy_import($filename, $base_path, $vocabulary_name) { $vocabulary = _mica_core_taxonomy_create($filename, $vocabulary_name); $path = $base_path . $filename; $result = taxonomy_csv_import(array( 'import_format' => 'tree', 'delimiter' => ',', 'enclosure' => '"', 'keep_order' => TRUE, 'file' => (object) array( 'filename' => $filename, 'filepath' => $path, 'filesize' => filesize($path) ), 'vocabulary_target' => 'existing', 'vocabulary_id' => $vocabulary->vid, 'source_choice' => 'file', )); watchdog('mica', 'Imported taxonomy %file to vocabulary %vocabulary: %result', array( '%file' => $path, '%vocabulary' => $vocabulary_name, '%result' => $result, ), WATCHDOG_INFO); } function mica_core_taxonomy_term_save($vocabulary_id, $name, $parent_id = NULL, $weight = NULL) { $term = new stdClass(); $term->vid = $vocabulary_id; $term->name = $name; if (!empty($parent_id)) { $term->parent = $parent_id; } if (!empty($weight)) { $term->weight = $weight; } taxonomy_term_save($term); return $term; } function mica_core_add_page($title, $body_html, $alias = NULL) { $page_wrapper = entity_property_values_create_entity( 'node', array( 'type' => 'page', 'title' => $title, 'body' => array( 'value' => $body_html, 'format' => 'full_html', ), 'language' => LANGUAGE_NONE, 'comment' => COMMENT_NODE_CLOSED, ) )->save(); if (isset($alias)) { $path = array('alias' => $alias); $path['source'] = 'node/' . $page_wrapper->nid->value(); $path['language'] = LANGUAGE_NONE; path_save($path); } return $page_wrapper->value(); }