public function sendForm($formData, $listOfUpdateIDs = array(), $smartAssistant = false) { if ($tokenError = $this->verifyFormToken()) { return $tokenError; } $actions = array(); $processActions = function ($type, $action, $response) { return array($type => $response->result ?: $response->errors, "{$type}{$action}" => $response->errors ? false : true); }; $formData = $this->processFields($formData, $presentFields); if (!$formData || !array_intersect(array_keys($presentFields), array_keys(Connect::getSupportedObjects(), 'read,write'))) { Api::phpoutlog("The form did not contain any fields with names of the Connect objects for which we have read and write support. Fields present: " . var_export($presentFields, true)); return $this->getResponseObject(null, null, Config::getMessage(ERROR_REQUEST_ACTION_COMPLETED_MSG)); } if ($presentFields['Contact']) { //Contact update if (Framework::isLoggedIn()) { $result = $this->CI->model('Contact')->update($this->CI->session->getProfileData('contactID'), $formData); $action = 'Updated'; } else { if ($formData['Contact.Emails.PRIMARY.Address'] && $formData['Contact.Emails.PRIMARY.Address']->value && $presentFields['Incident']) { $existingContact = $this->CI->model('Contact')->lookupContactByEmail($formData['Contact.Emails.PRIMARY.Address']->value, $formData['Contact.Name.First'] ? $formData['Contact.Name.First']->value : null, $formData['Contact.Name.Last'] ? $formData['Contact.Name.Last']->value : null)->result; if ($existingContact) { $formData['Incident.PrimaryContact'] = $existingContact; } else { $result = $this->CI->model('Contact')->create($formData, true); if ($result->result && $result->result->ID) { $formData['Incident.PrimaryContact'] = $result->result; } $action = 'Created'; } } else { $result = $this->CI->model('Contact')->create($formData, true); $action = 'Created'; } } if ($action) { $actions += $processActions('contact', $action, $result); } } if ($presentFields['Incident']) { if ($incidentIDToUpdate = $listOfUpdateIDs['i_id']) { $result = $this->CI->model('Incident')->update($incidentIDToUpdate, $formData); $action = 'Updated'; } else { $result = $this->CI->model('Incident')->create($formData, $smartAssistant); $action = 'Created'; } $actions += $processActions('incident', $action, $result); } if ($presentFields['Asset']) { $productID = $listOfUpdateIDs['product_id']; $serialNumber = $listOfUpdateIDs['serial_no']; if ($assetIDToUpdate = $listOfUpdateIDs['asset_id']) { if ($serialNumber !== null) { $serialNumber = urldecode($serialNumber); } $result = $this->CI->model('custom/AssetExtn')->create($productID, $formData, $serialNumber); $action = 'Updated'; } else { $result = $this->CI->model('custom/AssetExtn')->create($productID, $formData, $serialNumber); $action = 'Created'; } $actions += $processActions('asset', $action, $result); } return $this->getStatus($actions); }
public function create($productID, array $formData, $serialNumber) { if (!Framework::isValidID($productID)) { return $this->getResponseObject(null, null, Config::getMessage(INVALID_ID_SALES_PRODUCT_COLON_LBL)); } //$resultSet = Connect\ROQL::queryObject(sprintf("SELECT SalesProduct FROM SalesProduct WHERE ID = %d And Disabled != 1 And Attributes.IsServiceProduct = 1 And Attributes.HasSerialNumber != 1 And AdminVisibleInterfaces.ID = curInterface()", $productID))->next(); $resultSet = Connect\ROQL::queryObject(sprintf("SELECT SalesProduct FROM SalesProduct WHERE ID = %d And Disabled != 1 And Attributes.IsServiceProduct = 1 And AdminVisibleInterfaces.ID = curInterface()", $productID))->next(); if (!($salesProduct = $resultSet->next())) { return $this->getResponseObject(null, null, Config::getMessage(INVALID_ID_SALES_PRODUCT_COLON_LBL)); } $asset = $this->getBlank()->result; if ($contact = $this->getContact()) { $asset->Contact = $contact->ID; } else { return $this->getResponseObject(null, null, Config::getMessage(CONTACT_IS_NOT_LOGGED_IN_MSG)); } if ($asset->Contact->Organization) { $asset->Organization = $asset->Contact->Organization->ID; } $errors = $warnings = array(); foreach ($formData as $name => $field) { if (!\RightNow\Utils\Text::beginsWith($name, 'Asset')) { continue; } $fieldName = explode('.', $name); try { //Get the metadata about the field we're trying to set. In order to do that we have to //populate some of the sub-objects on the record. We don't want to touch the existing //record at all, so instead we'll just pass in a dummy instance. list(, $fieldMetaData) = ConnectUtil::getObjectField($fieldName, $this->getBlank()->result); } catch (\Exception $e) { $warnings[] = $e->getMessage(); continue; } if (\RightNow\Utils\Validation::validate($field, $name, $fieldMetaData, $errors)) { $field->value = ConnectUtil::castValue($field->value, $fieldMetaData); if ($setFieldError = $this->setFieldValue($asset, $name, $field->value)) { $errors[] = $setFieldError; } } } if ($productID !== null && ($setFieldError = $this->setFieldValue($asset, "Asset.Product", $productID))) { $errors[] = $setFieldError; } if ($serialNumber !== null && ($setFieldError = $this->setFieldValue($asset, "Asset.SerialNumber", $serialNumber))) { $errors[] = $setFieldError; } if ($errors) { return $this->getResponseObject(null, null, $errors); } try { $asset = parent::createObject($asset, SRC2_EU_ASSET); } catch (\Exception $e) { $asset = $e->getMessage(); } if (!is_object($asset)) { return $this->getResponseObject(null, null, $asset); } return $this->getResponseObject($asset, 'is_object', null, $warnings); }