You are here

function menu_icons_node_save in Menu Icons 7.3

Helper for hook_node_insert() and hook_node_update().

2 calls to menu_icons_node_save()
menu_icons_node_insert in ./menu_icons.module
Implements hook_node_insert().
menu_icons_node_update in ./menu_icons.module
Implements hook_node_update().

File

./menu_icons.module, line 277
Module to associate icons with menu items

Code

function menu_icons_node_save($node) {
  if (isset($node->menu['icon']) && $node->menu['icon']['enable']) {

    // Check the destination folder, attempt to create it if it does't exist
    $directory_path = menu_icons_directory_path();
    file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY);

    // Take the current icon path
    $path = $node->menu['icon']['path'];
    $file = FALSE;
    if (!empty($path)) {
      $file = image_load($path);
    }
    if ($file) {

      // Move temporary file to it's destination.
      $parts = pathinfo($file->source);
      $new_path = $directory_path . '/menu_icon_' . $node->menu['mlid'] . '.' . $parts['extension'];
      $node->menu['icon']['path'] = $new_path;
      if ($new_path != $path) {
        $new_file = file_unmanaged_copy($path, $new_path, FILE_EXISTS_REPLACE);

        // Delete temporary file.
        file_unmanaged_delete($path);
      }

      // Get link options from db.
      $options = unserialize(db_query('SELECT options FROM {menu_links} WHERE mlid = :mlid', array(
        ':mlid' => $node->menu['mlid'],
      ))
        ->fetchField());
      if (!isset($options['attributes'])) {
        $options['attributes'] = array();
      }
      if (!isset($options['attributes']['class'])) {
        $options['attributes']['class'] = array();
      }
      $classes = array();
      $classes[] = "menu_icon";
      $classes[] = "menu-" . $node->menu['mlid'];
      if (!empty($node->menu['icon']['path']) && file_exists($node->menu['icon']['path'])) {

        // Add our menu icon info to the options array
        $options['menu_icon'] = array(
          'enable' => $node->menu['icon']['enable'],
          'path' => $node->menu['icon']['path'],
          'image_style' => $node->menu['icon']['image_style'],
        );

        // Add new classes
        foreach ($classes as $class) {
          if (!in_array($class, $options['attributes']['class'])) {
            $options['attributes']['class'][] = $class;
          }
        }
        if (empty($options['attributes']['class'])) {
          unset($options['attributes']['class']);
        }

        // Update the link options
        db_update('menu_links')
          ->fields(array(
          'options' => serialize($options),
        ))
          ->condition('mlid', $node->menu['mlid'])
          ->execute();

        // Regenerate the css file
        menu_icons_css_generate();

        // @TODO The icon is not shown on first page load. We need to find a solution for this. Meanwhile:
        drupal_set_message(t('A new menu icon has been set for this node. Please refresh the page to view it.'));
      }
    }
  }
}