You are here

function coder_upgrade_upgrade_call_jquery_ui_add_alter in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/call.inc \coder_upgrade_upgrade_call_jquery_ui_add_alter()

Implements hook_upgrade_call_jquery_ui_add_alter().

File

coder_upgrade/conversions/call.inc, line 1785
Provides conversion routines applied to function calls.

Code

function coder_upgrade_upgrade_call_jquery_ui_add_alter(&$node, &$reader) {

  // Create helper objects.
  $editor = PGPEditor::getInstance();

  // Get the function call object.
  $item =& $node->data;

  // http://drupal.org/node/224333#jquery_ui
  // jquery_ui_add is now drupal_add_library which loads a single library.
  $name =& $item->name;
  $name['value'] = 'drupal_add_library';

  /*
   * Note: This is an interesting pattern that can be applied elsewhere.
   * If the parameter is a variable, then find the variable assignment and
   * replace the operand accordingly. Then go through the rest of the code.
   */
  $operand = $item
    ->getParameter()
    ->getElement();
  if ($item
    ->getParameter()
    ->isType(T_VARIABLE)) {

    // Parameter is a variable.
    $variable = $item
      ->getParameter()
      ->getElement()
      ->findNode('value');
    $parent = $item->parent;

    // TODO This single search won't find multiple assignments to array variable.
    $statement = $parent->container
      ->searchBackward('PGPAssignment', 'values', 0, $variable, $parent);
    if ($statement) {
      $operand =& $statement->values
        ->getElement()
        ->findNode('operand', 'backward');
      cdp($operand);
    }
  }

  // Examine the type of the operand in the parameter.
  if (is_array($operand)) {

    // This is a simple string.
    $value = trim($operand['value'], "'\"");
    $editor
      ->setParameter($item, 0, "'{$value}'");
  }
  elseif (get_class($operand) == 'PGPArray') {
    $current = $operand->values
      ->first();
    while ($current->next != NULL) {
      if ($current->type == 'value') {
        $values[] = $current->data
          ->toString();
      }
      $current = $current->next;
    }
    $editor
      ->setParameter($item, 0, array_pop($values));
    if ($values) {
      $parent =& $item->parent;
      $container =& $parent->container;
      foreach ($values as $value) {
        $temp = $editor
          ->textToStatements("drupal_add_library('system', {$value})")
          ->getElement();
        $container
          ->insertBefore($parent, $temp);
      }
    }
  }
  else {

    // elseif (get_class($operand) == 'PGPOperand') {
    // This would be a variable and should not occur.
    $statement = $editor
      ->commentToStatement("// TODO {$operand->toString()} needs to be a string or multiple calls to drupal_add_library need to be made.");
    $item
      ->insertStatementBefore($statement);
  }

  // Insert a new first parameter.
  $editor
    ->insertParameter($item, 0, "'system'");
}