Ejemplo n.º 1
0
 /**
  * Returns an array of indices for the current module
  *
  * @return array
  */
 private function _getIndexVardefs()
 {
     $indexes = $this->_focus->getIndices();
     //grab any custom indexes if they exist
     if ($this->_focus->hasCustomFields()) {
         $custmIndexes = $this->_focus->db->helper->get_indices($this->_focus->table_name . '_cstm');
         $indexes = array_merge($custmIndexes, $indexes);
     }
     // remove any that are datetime or time field as we can't dupe check them correctly since we don't export
     // seconds
     $fields = $this->_focus->getFieldDefinitions();
     foreach ($indexes as $key => $index) {
         foreach ($index['fields'] as $field) {
             if (isset($fields[$field]) && ($fields[$field]['type'] == 'datetime' || $fields[$field]['type'] == 'datetimecombo' || $fields[$field]['type'] == 'time')) {
                 unset($indexes[$key]);
                 break 1;
             }
         }
     }
     if ($this->_focus->getFieldDefinition('email1')) {
         $indexes[] = array('name' => 'special_idx_email1', 'type' => 'index', 'fields' => array('email1'));
     }
     if ($this->_focus->getFieldDefinition('email2')) {
         $indexes[] = array('name' => 'special_idx_email2', 'type' => 'index', 'fields' => array('email2'));
     }
     return $indexes;
 }
Ejemplo n.º 2
0
 /**
  * @param XTemplate $xtpl
  * @param string $view
  */
 public function populateXTPL($xtpl, $view)
 {
     if ($this->bean->hasCustomFields()) {
         $results = $this->getAllFieldsView($view, 'xtpl');
         foreach ($results as $name => $value) {
             if (is_array($value['xtpl'])) {
                 foreach ($value['xtpl'] as $xName => $xValue) {
                     $xtpl->assign(strtoupper($xName), $xValue);
                 }
             } else {
                 $xtpl->assign(strtoupper($name), $value['xtpl']);
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Generate a set of Insert statements based on the bean given
  *
  * @deprecated
  *
  * @param  SugarBean $bean         the bean from which table we will generate insert stmts
  * @param  string $select_query the query which will give us the set of objects we want to place into our insert statement
  * @param  int    $start        the first row to query
  * @param  int    $count        the number of rows to query
  * @param  string $table        the table to query from
  * @param bool $is_related_query
  * @return string SQL insert statement
  */
 public function generateInsertSQL(SugarBean $bean, $select_query, $start, $count = -1, $table, $is_related_query = false)
 {
     $this->log->info('call to DBManager::generateInsertSQL() is deprecated');
     global $sugar_config;
     $rows_found = 0;
     $count_query = $bean->create_list_count_query($select_query);
     if (!empty($count_query)) {
         // We have a count query.  Run it and get the results.
         $result = $this->query($count_query, true, "Error running count query for {$this->object_name} List: ");
         $assoc = $this->fetchByAssoc($result);
         if (!empty($assoc['c'])) {
             $rows_found = $assoc['c'];
         }
     }
     if ($count == -1) {
         $count = $sugar_config['list_max_entries_per_page'];
     }
     $next_offset = $start + $count;
     $result = $this->limitQuery($select_query, $start, $count);
     // get basic insert
     $sql = "INSERT INTO " . $table;
     $custom_sql = "INSERT INTO " . $table . "_cstm";
     // get field definitions
     $fields = $bean->getFieldDefinitions();
     $custom_fields = array();
     if ($bean->hasCustomFields()) {
         foreach ($fields as $fieldDef) {
             if ($fieldDef['source'] == 'custom_fields') {
                 $custom_fields[$fieldDef['name']] = $fieldDef['name'];
             }
         }
         if (!empty($custom_fields)) {
             $custom_fields['id_c'] = 'id_c';
             $id_field = array('name' => 'id_c', 'custom_type' => 'id');
             $fields[] = $id_field;
         }
     }
     // get column names and values
     $row_array = array();
     $columns = array();
     $cstm_row_array = array();
     $cstm_columns = array();
     $built_columns = false;
     while (($row = $this->fetchByAssoc($result)) != null) {
         $values = array();
         $cstm_values = array();
         if (!$is_related_query) {
             foreach ($fields as $fieldDef) {
                 if (isset($fieldDef['source']) && $fieldDef['source'] != 'db' && $fieldDef['source'] != 'custom_fields') {
                     continue;
                 }
                 $val = $row[$fieldDef['name']];
                 //handle auto increment values here only need to do this on insert not create
                 if ($fieldDef['name'] == 'deleted') {
                     $values['deleted'] = $val;
                     if (!$built_columns) {
                         $columns[] = 'deleted';
                     }
                 } else {
                     $type = $fieldDef['type'];
                     if (!empty($fieldDef['custom_type'])) {
                         $type = $fieldDef['custom_type'];
                     }
                     // need to do some thing about types of values
                     if ($this->dbType == 'mysql' && $val == '' && ($type == 'datetime' || $type == 'date' || $type == 'int' || $type == 'currency' || $type == 'decimal')) {
                         if (!empty($custom_fields[$fieldDef['name']])) {
                             $cstm_values[$fieldDef['name']] = 'null';
                         } else {
                             $values[$fieldDef['name']] = 'null';
                         }
                     } else {
                         if (isset($type) && $type == 'int') {
                             if (!empty($custom_fields[$fieldDef['name']])) {
                                 $cstm_values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val));
                             } else {
                                 $values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val));
                             }
                         } else {
                             if (!empty($custom_fields[$fieldDef['name']])) {
                                 $cstm_values[$fieldDef['name']] = "'" . $GLOBALS['db']->quote(from_html($val)) . "'";
                             } else {
                                 $values[$fieldDef['name']] = "'" . $GLOBALS['db']->quote(from_html($val)) . "'";
                             }
                         }
                     }
                     if (!$built_columns) {
                         if (!empty($custom_fields[$fieldDef['name']])) {
                             $cstm_columns[] = $fieldDef['name'];
                         } else {
                             $columns[] = $fieldDef['name'];
                         }
                     }
                 }
             }
         } else {
             foreach ($row as $key => $val) {
                 if ($key != 'orc_row') {
                     $values[$key] = "'{$val}'";
                     if (!$built_columns) {
                         $columns[] = $key;
                     }
                 }
             }
         }
         $built_columns = true;
         if (!empty($values)) {
             $row_array[] = $values;
         }
         if (!empty($cstm_values) && !empty($cstm_values['id_c']) && strlen($cstm_values['id_c']) > 7) {
             $cstm_row_array[] = $cstm_values;
         }
     }
     //if (sizeof ($values) == 0) return ""; // no columns set
     // get the entire sql
     $sql .= "(" . implode(",", $columns) . ") ";
     $sql .= "VALUES";
     for ($i = 0; $i < count($row_array); $i++) {
         $sql .= " (" . implode(",", $row_array[$i]) . ")";
         if ($i < count($row_array) - 1) {
             $sql .= ", ";
         }
     }
     //custom
     // get the entire sql
     $custom_sql .= "(" . implode(",", $cstm_columns) . ") ";
     $custom_sql .= "VALUES";
     for ($i = 0; $i < count($cstm_row_array); $i++) {
         $custom_sql .= " (" . implode(",", $cstm_row_array[$i]) . ")";
         if ($i < count($cstm_row_array) - 1) {
             $custom_sql .= ", ";
         }
     }
     return array('data' => $sql, 'cstm_sql' => $custom_sql, 'total_count' => $rows_found, 'next_offset' => $next_offset);
 }
Ejemplo n.º 4
0
 /**
  * Joins the custom table to the current query (if possible)
  * @param SugarBean $bean
  * @param string $alias
  */
 public function joinCustomTable($bean, $alias = "")
 {
     if ($bean->hasCustomFields() && !$this->customJoined) {
         $table = $bean->getTableName();
         $table_cstm = $bean->get_custom_table_name();
         if (!empty($table_cstm)) {
             // TODO: CLEAN THIS UP
             if (!empty($alias)) {
                 $sql = "LEFT JOIN {$table_cstm} {$alias}_c ON {$alias}_c.id_c = {$alias}.id";
             } else {
                 $sql = "LEFT JOIN {$table_cstm} ON {$table_cstm}.id_c = {$table}.id";
             }
             // can do a join here because we haven't got to the joins yet in the compile sequence.
             $this->joinRaw($sql);
         }
     }
 }