original_string: function &insertElementBefore(&$element, $nameAfter)
      {
          if (!empty($this->_duplicateIndex[$nameAfter])) {
              $error = self::raiseError(null, QUICKFORM_INVALID_ELEMENT_NAME, null, E_USER_WARNING, 'Several elements named "' . $nameAfter . '" exist in HTML_QuickForm::insertElementBefore().', 'HTML_QuickForm_Error', true);
              return $error;
          } elseif (!$this->elementExists($nameAfter)) {
              $error = self::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()", 'HTML_QuickForm_Error', true);
              return $error;
          }
          $elementName = $element->getName();
          $targetIdx   = $this->_elementIndex[$nameAfter];
          $duplicate   = false;
          // Like in addElement(), check that it's not an incompatible duplicate
          if (!empty($elementName) && isset($this->_elementIndex[$elementName])) {
              if ($this->_elements[$this->_elementIndex[$elementName]]->getType() != $element->getType()) {
                  $error = self::raiseError(null, QUICKFORM_INVALID_ELEMENT_NAME, null, E_USER_WARNING, "Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()", 'HTML_QuickForm_Error', true);
                  return $error;
              }
              $duplicate = true;
          }
          // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex
          $elKeys = array_keys($this->_elements);
          for ($i = end($elKeys); $i >= $targetIdx; $i--) {
              if (isset($this->_elements[$i])) {
                  $currentName = $this->_elements[$i]->getName();
                  $this->_elements[$i + 1] =& $this->_elements[$i];
                  if ($this->_elementIndex[$currentName] == $i) {
                      $this->_elementIndex[$currentName] = $i + 1;
                  } else {
                      if (!empty($currentName)) {
                          $dupIdx = array_search($i, $this->_duplicateIndex[$currentName]);
                          $this->_duplicateIndex[$currentName][$dupIdx] = $i + 1;
                      }
                  }
                  unset($this->_elements[$i]);
              }
          }
          // Put the element in place finally
          $this->_elements[$targetIdx] =& $element;
          if (!$duplicate) {
              $this->_elementIndex[$elementName] = $targetIdx;
          } else {
              $this->_duplicateIndex[$elementName][] = $targetIdx;
          }
          $element->onQuickFormEvent('updateValue', null, $this);
          if ($this->_freezeAll) {
              $element->freeze();
          }
          // If not done, the elements will appear in reverse order
          ksort($this->_elements);
          return $element;
      }
code: function &insertElementBefore(&$element, $nameAfter)
      {
          if (!empty($this->_duplicateIndex[$nameAfter])) {
              $error = self::raiseError(null, QUICKFORM_INVALID_ELEMENT_NAME, null, E_USER_WARNING, 'Several elements named "' . $nameAfter . '" exist in HTML_QuickForm::insertElementBefore().', 'HTML_QuickForm_Error', true);
              return $error;
          } elseif (!$this->elementExists($nameAfter)) {
              $error = self::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()", 'HTML_QuickForm_Error', true);
              return $error;
          }
          $elementName = $element->getName();
          $targetIdx   = $this->_elementIndex[$nameAfter];
          $duplicate   = false;
          // Like in addElement(), check that it's not an incompatible duplicate
          if (!empty($elementName) && isset($this->_elementIndex[$elementName])) {
              if ($this->_elements[$this->_elementIndex[$elementName]]->getType() != $element->getType()) {
                  $error = self::raiseError(null, QUICKFORM_INVALID_ELEMENT_NAME, null, E_USER_WARNING, "Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()", 'HTML_QuickForm_Error', true);
                  return $error;
              }
              $duplicate = true;
          }
          // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex
          $elKeys = array_keys($this->_elements);
          for ($i = end($elKeys); $i >= $targetIdx; $i--) {
              if (isset($this->_elements[$i])) {
                  $currentName = $this->_elements[$i]->getName();
                  $this->_elements[$i + 1] =& $this->_elements[$i];
                  if ($this->_elementIndex[$currentName] == $i) {
                      $this->_elementIndex[$currentName] = $i + 1;
                  } else {
                      if (!empty($currentName)) {
                          $dupIdx = array_search($i, $this->_duplicateIndex[$currentName]);
                          $this->_duplicateIndex[$currentName][$dupIdx] = $i + 1;
                      }
                  }
                  unset($this->_elements[$i]);
              }
          }
          // Put the element in place finally
          $this->_elements[$targetIdx] =& $element;
          if (!$duplicate) {
              $this->_elementIndex[$elementName] = $targetIdx;
          } else {
              $this->_duplicateIndex[$elementName][] = $targetIdx;
          }
          $element->onQuickFormEvent('updateValue', null, $this);
          if ($this->_freezeAll) {
              $element->freeze();
          }
          // If not done, the elements will appear in reverse order
          ksort($this->_elements);
          return $element;
      }
code_tokens: function & insertElementBefore ( & $ element , $ nameAfter ) { if ( ! empty ( $ this -> _duplicateIndex [ $ nameAfter ] ) ) { $ error = self :: raiseError ( null , QUICKFORM_INVALID_ELEMENT_NAME , null , E_USER_WARNING , 'Several elements named "' . $ nameAfter . '" exist in HTML_QuickForm::insertElementBefore().' , 'HTML_QuickForm_Error' , true ) ; return $ error ; } elseif ( ! $ this -> elementExists ( $ nameAfter ) ) { $ error = self :: raiseError ( null , QUICKFORM_NONEXIST_ELEMENT , null , E_USER_WARNING , "Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()" , 'HTML_QuickForm_Error' , true ) ; return $ error ; } $ elementName = $ element -> getName ( ) ; $ targetIdx = $ this -> _elementIndex [ $ nameAfter ] ; $ duplicate = false ; // Like in addElement(), check that it's not an incompatible duplicate if ( ! empty ( $ elementName ) && isset ( $ this -> _elementIndex [ $ elementName ] ) ) { if ( $ this -> _elements [ $ this -> _elementIndex [ $ elementName ] ] -> getType ( ) != $ element -> getType ( ) ) { $ error = self :: raiseError ( null , QUICKFORM_INVALID_ELEMENT_NAME , null , E_USER_WARNING , "Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()" , 'HTML_QuickForm_Error' , true ) ; return $ error ; } $ duplicate = true ; } // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex $ elKeys = array_keys ( $ this -> _elements ) ; for ( $ i = end ( $ elKeys ) ; $ i >= $ targetIdx ; $ i -- ) { if ( isset ( $ this -> _elements [ $ i ] ) ) { $ currentName = $ this -> _elements [ $ i ] -> getName ( ) ; $ this -> _elements [ $ i + 1 ] = & $ this -> _elements [ $ i ] ; if ( $ this -> _elementIndex [ $ currentName ] == $ i ) { $ this -> _elementIndex [ $ currentName ] = $ i + 1 ; } else { if ( ! empty ( $ currentName ) ) { $ dupIdx = array_search ( $ i , $ this -> _duplicateIndex [ $ currentName ] ) ; $ this -> _duplicateIndex [ $ currentName ] [ $ dupIdx ] = $ i + 1 ; } } unset ( $ this -> _elements [ $ i ] ) ; } } // Put the element in place finally $ this -> _elements [ $ targetIdx ] = & $ element ; if ( ! $ duplicate ) { $ this -> _elementIndex [ $ elementName ] = $ targetIdx ; } else { $ this -> _duplicateIndex [ $ elementName ] [ ] = $ targetIdx ; } $ element -> onQuickFormEvent ( 'updateValue' , null , $ this ) ; if ( $ this -> _freezeAll ) { $ element -> freeze ( ) ; } // If not done, the elements will appear in reverse order ksort ( $ this -> _elements ) ; return $ element ; }
docstring: Inserts a new element right before the other element
  
  Warning: it is not possible to check whether the $element is already
  added to the form, therefore if you want to move the existing form
  element to a new position, you'll have to use removeElement():
  $form->insertElementBefore($form->removeElement('foo', false), 'bar');
  
  @access   public
  @since    3.2.4
  @param    object  HTML_QuickForm_element  Element to insert
  @param    string  Name of the element before which the new one is inserted
  @return   object  HTML_QuickForm_element  reference to inserted element
  @throws   HTML_QuickForm_Error
docstring_tokens: Inserts a new element right before the other element