/**
  * Formats the bean so it is ready to be handed back to the API's client. Certain fields will get extra processing
  * to make them easier to work with from the client end.
  *
  * @param $bean SugarBean|ForecastManagerWorksheet The bean you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     $sq = new SugarQuery();
     $sq->select('date_modified');
     $sq->from($bean)->where()->equals('assigned_user_id', $bean->assigned_user_id)->equals('user_id', $bean->user_id)->equals('draft', 0)->equals('timeperiod_id', $bean->timeperiod_id);
     $beans = $sq->execute();
     $data['show_history_log'] = 0;
     if (empty($beans) && !empty($bean->fetched_row['date_modified'])) {
         /* @var $tp TimePeriod */
         $tp = BeanFactory::getBean('TimePeriods', $bean->timeperiod_id);
         // When reportee has committed but manager has not
         // make sure that the reportee actually has a commit for the timeperiod,
         // this is to handle the case where the manager saves draft before the reportee can commit
         $sq = new SugarQuery();
         $sq->select('id');
         $sq->from(BeanFactory::getBean('ForecastWorksheets'))->where()->equals('assigned_user_id', $bean->user_id)->equals('draft', 0)->queryAnd()->gte('date_closed_timestamp', $tp->start_date_timestamp)->lte('date_closed_timestamp', $tp->end_date_timestamp);
         $worksheets = $sq->execute();
         if (!empty($worksheets)) {
             $data['show_history_log'] = 1;
         }
     } else {
         if (!empty($beans)) {
             $fBean = $beans[0];
             $committed_date = $bean->db->fromConvert($fBean["date_modified"], "datetime");
             if (strtotime($committed_date) < strtotime($bean->fetched_row['date_modified'])) {
                 $db = DBManagerFactory::getInstance();
                 // find the differences via the audit table
                 // we use a direct query since SugarQuery can't do the audit tables...
                 $sql = sprintf("SELECT field_name, before_value_string, after_value_string FROM %s\n                        WHERE parent_id = %s AND date_created >= " . $db->convert('%s', 'datetime'), $bean->get_audit_table_name(), $db->quoted($bean->id), $db->quoted($committed_date));
                 $results = $db->query($sql);
                 // get the setting for which fields to compare on
                 /* @var $admin Administration */
                 $admin = BeanFactory::getBean('Administration');
                 $settings = $admin->getConfigForModule('Forecasts', 'base');
                 while ($row = $db->fetchByAssoc($results)) {
                     $field = substr($row['field_name'], 0, strpos($row['field_name'], '_'));
                     if ($settings['show_worksheet_' . $field] == "1") {
                         // calculate the difference to make sure it actually changed at 2 digits vs changed at 6
                         $diff = SugarMath::init($row['after_value_string'], 6)->sub($row['before_value_string'])->result();
                         // due to decimal rounding on the front end, we only want to know about differences greater
                         // of two decimal places.
                         // todo-sfa: This hardcoded 0.01 value needs to be changed to a value determined by userprefs
                         if (abs($diff) >= 0.01) {
                             $data['show_history_log'] = 1;
                             break;
                         }
                     }
                 }
             }
         }
     }
     if (!empty($bean->user_id)) {
         $data['is_manager'] = User::isManager($bean->user_id);
     }
     return $data;
 }
 /**
  * Formats the bean so it is ready to be handed back to the API's client. Certian fields will get extra processing
  * to make them easier to work with from the client end.
  *
  * @param $bean SugarBean The bean you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     if (isset($bean->fetched_row) && !empty($bean->fetched_row['report_type']) && $bean->report_type == 'summary' && $bean->fetched_row['report_type'] == 'Matrix') {
         $bean->report_type = $bean->fetched_row['report_type'];
     }
     return parent::formatForApi($bean, $fieldList, $options);
 }
 /**
  * 'view' is deprecated because it's reserved db word.
  * Some old API (before 7.2.0) can use 'view'.
  * Because of that API will return 'view' with the same value as 'view_name'.
  *
  * @param SugarBean $bean
  * @param array     $fieldList
  * @param array     $options
  *
  * @return array
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     if (isset($data['view_name'])) {
         $data['view'] = $data['view_name'];
     }
     return $data;
 }
Example #4
0
 /**
  * Formats the bean so it is ready to be handed back to the API's client. Certian fields will get extra processing
  * to make them easier to work with from the client end.
  *
  * @param $bean SugarBean The bean you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     if (in_array("name", $fieldList) && !empty($bean->name_2)) {
         $data['name'] = trim($bean->name . ' ' . $bean->name_2);
         if (!empty($data['name_2'])) {
             $data['name_2'] = '';
         }
     }
     return $data;
 }
Example #5
0
 public function populateFromApi(SugarBean $bean, array $submittedData, array $options = array())
 {
     parent::populateFromApi($bean, $submittedData, $options);
     if (!$bean->new_with_id && !empty($bean->id)) {
         return true;
     }
     if (empty($submittedData) || empty($submittedData['user_name'])) {
         throw new SugarApiExceptionMissingParameter("Missing username");
     }
     return true;
 }
 /**
  * This function checks the sync_contact var and does the appropriate actions
  * @param SugarBean $bean
  * @param array $submittedData
  * @param array $options
  * @return array
  */
 public function populateFromApi(SugarBean $bean, array $submittedData, array $options = array())
 {
     global $current_user;
     $data = parent::populateFromApi($bean, $submittedData, $options);
     if ($data) {
         if (!empty($bean->emailAddress) && $bean->emailAddress->addresses != $bean->emailAddress->fetchedAddresses) {
             $bean->emailAddress->populateLegacyFields($bean);
         }
         if (isset($submittedData['sync_contact'])) {
             $bean->sync_contact = $submittedData['sync_contact'];
         }
     }
     return $data;
 }
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $db = DBManagerFactory::getInstance();
     $query = "SELECT\n            jt0.id assigned_user_id,\n            jt0.user_name assigned_user_name,\n            jt0.first_name assgn_fn,\n            jt0.last_name assgn_ln,\n            jt1.id kbdoc_approver_id,\n            jt1.user_name kbdoc_approver_name,\n            jt1.first_name appr_fn,\n            jt1.last_name appr_ln,\n            kvr.views_number views_number\n        FROM\n            kbdocuments LEFT JOIN kbdocuments_views_ratings kvr ON kbdocuments.id = kvr.kbdocument_id\n            LEFT JOIN  users jt0 ON jt0.id = kbdocuments.assigned_user_id AND jt0.deleted = 0\n            LEFT JOIN  users jt1 ON jt1.id = kbdocuments.kbdoc_approver_id AND jt1.deleted = 0\n        WHERE\n            kbdocuments.id = {$db->quoted($bean->id)}";
     $res = $db->query($query);
     $addon = $db->fetchRow($res);
     if (in_array('views_number', $fieldList) && !empty($addon['views_number'])) {
         $bean->views_number = $addon['views_number'];
     }
     // bug 56834 - the api doesn't return kbdoc_approver_name
     $isKbApprover = in_array('kbdoc_approver_name', $fieldList);
     if ($isKbApprover && !empty($addon['kbdoc_approver_id'])) {
         $bean->kbdoc_approver_id = $addon['kbdoc_approver_id'];
     }
     //add kbdoc_approver_id if not in fieldList
     if ($isKbApprover && !in_array('kbdoc_approver_id', $fieldList)) {
         $fieldList[] = 'kbdoc_approver_id';
     }
     $data = parent::formatForApi($bean, $fieldList, $options);
     // bug 56834 - manually fill kbdoc_approver_name if in fieldList
     if ((empty($fieldList) || $isKbApprover) && isset($data['kbdoc_approver_id'])) {
         $user = BeanFactory::getBean('Users');
         $user->populateFromRow(array('id' => $addon['kbdoc_approver_id'], 'first_name' => $addon['appr_fn'], 'last_name' => $addon['appr_ln']));
         $data['kbdoc_approver_name'] = Localization::getObject()->formatName($user);
     }
     if (in_array('assigned_user_name', $fieldList) && !empty($addon['assigned_user_id'])) {
         $user = BeanFactory::getBean('Users');
         $user->populateFromRow(array('id' => $addon['assigned_user_id'], 'first_name' => $addon['assgn_fn'], 'last_name' => $addon['assgn_ln']));
         $data['assigned_user_name'] = Localization::getObject()->formatName($user);
     }
     if (empty($fieldList) || in_array('attachment_list', $fieldList)) {
         $query = "SELECT rev.id rev_id, rev.filename filename, kbrev.id docrev_id FROM kbdocument_revisions kbrev LEFT JOIN document_revisions rev ON (kbrev.document_revision_id = rev.id) WHERE kbrev.kbdocument_id = '" . $bean->id . "' AND kbrev.deleted = 0 AND rev.deleted = 0 AND kbrev.kbcontent_id is NULL";
         $ret = $db->query($query, true);
         $files = array();
         while ($row = $db->fetchByAssoc($ret)) {
             $thisFile = array();
             $thisFile['document_revision_id'] = $row['rev_id'];
             // add some extra meta so we can build the urls on the client
             $thisFile['id'] = $row['rev_id'];
             $thisFile['module'] = 'DocumentRevisions';
             $thisFile['field_name'] = 'filename';
             $thisFile['name'] = $row['filename'];
             $thisFile['kbdocument_revision_id'] = $row['docrev_id'];
             $thisFile['uri'] = $this->api->getResourceURI(array('DocumentRevisions', $row['rev_id'], 'file', 'filename'));
             $files[] = $thisFile;
         }
         $data['attachment_list'] = $files;
     }
     return $data;
 }
Example #8
0
 /**
  * Formats the bean so it is ready to be handed back to the API's client.
  *
  * @param $bean SugarBean The Task you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     if (isset($bean->contact_id)) {
         $contact = BeanFactory::getBean('Contacts', $bean->contact_id);
         if (!empty($contact) && $contact->id != "") {
             if (isset($data['contact_name'])) {
                 $data['contact_name'] = empty($contact->full_name) ? '' : $contact->full_name;
             }
             if (isset($data['contact_phone'])) {
                 $data['contact_phone'] = empty($contact->phone_work) ? '' : $contact->phone_work;
             }
         }
     }
     return $data;
 }
 /**
  * Formats the bean so it is ready to be handed back to the API's client. Certian fields will get extra processing
  * to make them easier to work with from the client end.
  *
  * @param $bean SugarBean|ForecastWorksheet The bean you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     $data['parent_deleted'] = 0;
     if ($bean->draft == 0) {
         $sq = new SugarQuery();
         $sq->select('id');
         $sq->from(BeanFactory::getBean($bean->parent_type))->where()->equals('id', $bean->parent_id);
         $beans = $sq->execute();
         if (empty($beans)) {
             $data['parent_name'] = $data['name'];
             $data['parent_deleted'] = 1;
         }
     }
     return $data;
 }
Example #10
0
 public function populateFromApi(SugarBean $bean, array $submittedData, array $options = array())
 {
     foreach ($submittedData as $fieldName => $data) {
         if (isset($bean->field_defs[$fieldName])) {
             $properties = $bean->field_defs[$fieldName];
             $type = !empty($properties['custom_type']) ? $properties['custom_type'] : $properties['type'];
             /* Field with name=email is the only field of type=email supported at this time */
             if ($type === 'email') {
                 if ($fieldName !== 'email') {
                     unset($submittedData[$fieldName]);
                 }
             }
         }
     }
     parent::populateFromApi($bean, $submittedData, $options);
     return true;
 }
 /**
  * This function adds support for the related_name field (of type 'function' which is no longer supported)
  * @param $bean SugarBean The bean you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     if (in_array('related_name', $fieldList) && !empty($bean->related_id) && !empty($bean->related_type)) {
         $relatedBean = BeanFactory::getBean($bean->related_type, $bean->related_id);
         if (!empty($relatedBean)) {
             if ($bean->related_type == 'CampaignTrackers') {
                 $relatedNameField = 'tracker_url';
             } elseif ($bean->related_type == 'Contacts' || $bean->related_type == 'Leads' || $bean->related_type == 'Prospects') {
                 $relatedNameField = 'full_name';
             } else {
                 $relatedNameField = 'name';
             }
             $data['parent_id'] = $bean->related_id;
             $data['parent_type'] = $bean->related_type;
             $data['related_name'] = $relatedBean->{$relatedNameField};
         }
     }
     return $data;
 }
 /**
  * Formats the bean so it is ready to be handed back to the API's client. Certian fields will get extra processing
  * to make them easier to work with from the client end.
  *
  * @param $bean SugarBean The bean you want formatted
  * @param $fieldList array Which fields do you want formatted and returned (leave blank for all fields)
  * @param $options array Currently no options are supported
  * @return array The bean in array format, ready for passing out the API to clients.
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     // If there was a field list requested and document_revision_id was not
     // a requested field we will have problems. Set the revision id so that
     // additional fields like filename get picked up.
     if (!empty($fieldList) && !in_array('document_revision_id', $fieldList)) {
         $db = DBManagerFactory::getInstance();
         // Get the revision ID so that it can be set into the bean
         $sql = "SELECT document_revision_id \n                    FROM {$bean->table_name} \n                    WHERE id = '{$bean->id}'";
         $rs = $db->query($sql);
         $row = $db->fetchByAssoc($rs);
         if (isset($row['document_revision_id'])) {
             // Set the revision and setup everything else for a document that
             // depends on a revision id, like filename, revision, etc
             $bean->document_revision_id = $row['document_revision_id'];
             $bean->fill_in_additional_detail_fields();
         }
     }
     return parent::formatForApi($bean, $fieldList, $options);
 }
Example #13
0
 /**
  * This function sets the team & assigned user and sets up the contact & account relationship
  * for new Notes submitted via portal users.
  *
  * @param SugarBean $bean
  * @param array $submittedData
  * @param array $options
  * @return array
  */
 public function populateFromApi(SugarBean $bean, array $submittedData, array $options = array())
 {
     //TODO: need a more generic way to deal with file types
     if (isset($submittedData['file_mime_type'])) {
         unset($submittedData['file_mime_type']);
     }
     $data = parent::populateFromApi($bean, $submittedData, $options);
     //Only needed for Portal sessions
     if (isset($_SESSION['type']) && $_SESSION['type'] == 'support_portal') {
         if (empty($bean->id)) {
             $bean->id = create_guid();
             $bean->new_with_id = true;
         }
         $contact = BeanFactory::getBean('Contacts', $_SESSION['contact_id']);
         $account = $contact->account_id;
         $bean->assigned_user_id = $contact->assigned_user_id;
         $bean->team_id = $contact->fetched_row['team_id'];
         $bean->team_set_id = $contact->fetched_row['team_set_id'];
         $bean->account_id = $account;
         $bean->contact_id = $contact->id;
     }
     return $data;
 }
Example #14
0
 /**
  * This function sets up shipping and billing address for new Quote.
  *
  * @param SugarBean|Quote $quote The current SugarBean that is being worked with
  * @param array $submittedData The data from the request
  * @param array $options Any Options that may have been passed in.
  * @return array|boolean An array of validation errors if any occurred, otherwise `true`.
  */
 public function populateFromApi(SugarBean $quote, array $submittedData, array $options = array())
 {
     parent::populateFromApi($quote, $submittedData, $options);
     // valid relate modules
     $valid_relate_modules = array('Contacts', 'Accounts');
     // Bug #57888 : REST API: Create related quote must populate billing/shipping contact and account
     if (isset($submittedData['relate_module'], $submittedData['relate_record']) && in_array($submittedData['relate_module'], $valid_relate_modules)) {
         $this->setAddressFromBean($submittedData['relate_module'], $submittedData['relate_record'], $quote);
     } else {
         // we are not on a related record, so lets check the field and fill in the data correctly
         $hasBillingAccountId = isset($quote->billing_account_id) && !empty($quote->billing_account_id);
         $hasShippingAccountId = isset($quote->shipping_account_id) && !empty($quote->shipping_account_id);
         $hasBillingContactId = isset($quote->billing_contact_id) && !empty($quote->billing_contact_id);
         $hasShippingContactId = isset($quote->shipping_contact_id) && !empty($quote->shipping_contact_id);
         if ($hasBillingAccountId) {
             $account = BeanFactory::getBean('Accounts', $quote->billing_account_id);
             $this->processBeanAddressFields($account, $quote, 'billing', 'billing', 'shipping');
         } elseif (!$hasBillingAccountId && $hasBillingContactId) {
             $contact = BeanFactory::getBean('Contacts', $quote->billing_contact_id);
             $this->processBeanAddressFields($contact, $quote, 'shipping', 'primary', 'alt');
         }
         if (!$hasShippingAccountId && !$hasShippingContactId && $hasBillingAccountId) {
             // we don't have a id set for the shipping account or contact, pull the account from the billing
             $quote->shipping_account_id = $quote->billing_account_id;
             $hasShippingAccountId = true;
         }
         if ($hasShippingAccountId && !$hasShippingContactId) {
             $account = BeanFactory::getBean('Accounts', $quote->shipping_account_id);
             $this->processBeanAddressFields($account, $quote, 'shipping', 'shipping', 'billing');
         } elseif ($hasShippingContactId) {
             $contact = BeanFactory::getBean('Contacts', $quote->shipping_contact_id);
             $this->processBeanAddressFields($contact, $quote, 'shipping', 'primary', 'alt');
         }
     }
     return true;
 }
 /**
  * {@inheritdoc}
  *
  * Adds the contact's name if one is related.
  *
  * `send_invites` and `auto_invite_parent` are internal processing flags and should never be
  * returned as fields.
  *
  * @param SugarBean $bean
  * @param array $fieldList
  * @param array $options
  * @return array
  */
 public function formatForApi(SugarBean $bean, array $fieldList = array(), array $options = array())
 {
     $data = parent::formatForApi($bean, $fieldList, $options);
     if (!empty($bean->contact_id)) {
         $contact = BeanFactory::getBean('Contacts', $bean->contact_id);
         if ($contact instanceof Contact) {
             $data['contact_name'] = $contact->full_name;
         }
     }
     unset($data['send_invites']);
     unset($data['auto_invite_parent']);
     // Handle enforcement of numerics, an issue introduced in MACAROON-684.
     foreach ($this->numericFields as $field) {
         if (array_key_exists($field, $data)) {
             $data[$field] = $this->getNumericValue($data[$field]);
         }
     }
     return $data;
 }