/**
  * Ensures that a row exists in the given tablet
  * @param I2CE_Form $form
  * @param string $col
  * @param mixed $parent_col.  If a string it is the parent col to save the parent id in
  */
 protected function ensureFormId($form, $col, $parent_col)
 {
     $table = $this->getTable($form->getName());
     if (!$table) {
         I2CE::raiseError("No table specified for {$form}");
         return '0';
     }
     $id = $form->getId();
     if ($id == '0') {
         $new_id = $this->db->getBeforeID($table, $col, true, true);
         if (I2CE::pearError($new_id, "Cannot get next id")) {
             return '0';
         }
         $stmt = "INSERT INTO  SET `{$col}` = '" . $form->getName() . "|{$id}'";
         if (is_string($parent_col)) {
             $stmt .= ", `{$parent_col}` = '" . $form->getParent() . "'";
         }
         if (I2CE::pearError($this->db->exec($stmt), "Error inserting form " . $form->getName() . ": ")) {
             return '0';
         }
         $new_id = $this->db->getAfterID($new_id, $table, $col);
         $form->setId($new_id);
         $form->setChangeType(I2CE_FormStorage_Mechanism::CHANGE_INITIAL);
         return $new_id;
     } else {
         $stmt = "INSERT IGNORE INTO {$table} SET `{$col}` = '{$id}'";
         if (is_string($parent_col)) {
             $stmt .= ", `{$parent_col}` = '" . $form->getParent() . "'";
         }
         if (I2CE::pearError($this->db->exec($stmt), "Error inserting form " . $form->getName() . ": ")) {
             return '0';
         }
         return $id;
     }
 }
 /**
  * Save a form object into magicdata
  * @param I2CE_Form $form
  * @param I2CE_User $user
  * @param boolean $transact
  */
 public function save($form, $user, $transact)
 {
     $form_id = $form->getId();
     if (!$form_id) {
         $form_id = $this->getNextAvailableId($form->getName());
     }
     if (!$form_id) {
         return false;
     }
     $form->setId($form_id);
     $form_config = $this->getFormConfig($form, true);
     if (!$form_config instanceof I2CE_MagicDataNode) {
         return false;
     }
     $form_config->last_modified = I2CE_Date::now(I2CE_Date::DATE_TIME)->dbFormat();
     $form_config->who = $user->getId();
     $parent = $form->getParent();
     if ($parent != "") {
         /*  Does this need to be here?  the parent node may be new and doesn't exist...
             if ($form_config->is_parent('parent')) {
                 return false;
             }
             */
         $form_config->parent = $parent;
     }
     return parent::save($form, $user, $transact);
 }
 /**
  * Save a form object into entry tables.
  * If this functio is over-written, it should include the fuzzy method call
  * foreach ($form as $field) {
  *      $field->save(true/false, $user)
  * }
  * @param I2CE_Form $form
  * @param I2CE_User $user
  * @param boolean $transact
  */
 public function save($form, $user, $transact)
 {
     if (!$this->isWritable()) {
         return true;
     }
     if (!$form instanceof I2CE_Form || !($form_name = $form->getName()) || !$this->init_data($form_name)) {
         return false;
     }
     $form_xml = $form->getXMLRepresentation()->ownerDocument;
     $form_id = $form->getID();
     if ($form_id != '0') {
         $endpoint = 'update';
     } else {
         $endpoint = 'create';
     }
     I2CE::raiseError("Saving ({$endpoint}) on " . $form_xml->saveXML());
     if (!array_key_exists($endpoint, $this->services[$form_name]) || !is_array($this->services[$form_name][$endpoint])) {
         I2CE::raiseError("No service set specified for {$endpoint} of {$form_name}");
         return false;
     }
     if ($form_id == '0' && (!array_key_exists('results', $this->services[$form_name]['create']) || !is_string($results = $this->services[$form_name]['create']['results']) || !$results)) {
         I2CE::raiseError("No result set specified for create {$form_name}");
         return false;
     }
     if (($out_message = $this->process_transform($form_name, $endpoint, 'out', $form_xml, true)) === false) {
         I2CE::raiseError("Could not transform out going message");
         return false;
     }
     $in_message = $this->call_service($form_name, $endpoint, $out_message);
     if (($trans_message = $this->process_transform($form_name, $endpoint, 'in', $in_message, false)) === false) {
         I2CE::raiseError("Could not transform incoming message");
         return false;
     }
     if ($form_id == '0') {
         $xpath = new DOMXpath($trans_message);
         foreach ($this->namespaces[$form_name] as $ns => $uri) {
             $xpath->registerNamespace($ns, $uri);
         }
         $xpath = new DOMXpath($trans_message);
         if (!($val_nodes = $xpath->query($results)) instanceof DOMNodeList || !($val_nodes->length == 1)) {
             I2CE::raiseError("Invalid results query");
             return false;
         }
         $val_node = $val_nodes->item(0);
         if ($val_node instanceof DOMAttr) {
             $form_id = $val_node->value;
         } else {
             if ($val_node instanceof DOMNode) {
                 $form_id = $val_node->textContent;
             }
         }
         $form->setId($form_id);
     }
     $this->clear_service_cache($form_name);
     return true;
 }