/**
  * @param array $field_def
  * @param string $joinTableAlias
  * @param bool $withIdName
  * @return array|bool
  */
 public function getRelateJoin($field_def, $joinTableAlias, $withIdName = true)
 {
     if (empty($field_def['type']) || $field_def['type'] != 'relate') {
         return false;
     }
     $rel_mod = BeanFactory::getBean($field_def['module']);
     if (!$rel_mod) {
         return false;
     }
     $rel_table = $rel_mod->table_name;
     $name_field = '';
     if (isset($rel_mod->field_defs['name'])) {
         $name_field_def = $rel_mod->field_defs['name'];
         if (isset($name_field_def['db_concat_fields'])) {
             $name_field = $this->db->concat($joinTableAlias, $name_field_def['db_concat_fields']);
         } elseif (!empty($rel_mod->field_defs['name']['source']) && $rel_mod->field_defs['name']['source'] == 'non-db' && !empty($field_def['rname'])) {
             $name_field = "{$joinTableAlias}." . $field_def['rname'];
         } else {
             $name_field = "{$joinTableAlias}.name";
         }
     }
     $tableName = isset($field_def['custom_module']) ? "{$this->bean->table_name}_cstm" : $this->bean->table_name;
     $relID = $field_def['id_name'];
     $ret_array['rel_table'] = $rel_table;
     $ret_array['name_field'] = $name_field;
     $ret_array['select'] = ($withIdName ? ", {$tableName}.{$relID}" : '') . ", {$name_field} {$field_def['name']} ";
     $ret_array['from'] = " LEFT JOIN {$rel_table} {$joinTableAlias} ON {$tableName}.{$relID} = {$joinTableAlias}.id" . " AND {$joinTableAlias}.deleted=0 ";
     return $ret_array;
 }
Exemple #2
0
 /**
  * Composes SELECT statement for fetching data of a relate field
  *
  * @param array $field_def       Relate field definition
  * @param string $joinTableAlias Alias for joined table
  *
  * @return array
  */
 public function getRelateFieldQuery($field_def, $joinTableAlias, $selectedFields = array())
 {
     global $locale;
     $name = $field_def['name'];
     $rname = isset($field_def['rname']) ? $field_def['rname'] : 'name';
     $joinCustomTableAlias = $joinTableAlias . '_cstm';
     $fields = $sort_fields = array();
     $has_custom_fields = false;
     if (isset($this->field_defs[$rname])) {
         $rname_field_def = $this->field_defs[$rname];
         if (isset($rname_field_def['type']) && $rname_field_def['type'] == 'fullname') {
             $format_fields = $locale->getNameFormatFields($this);
             foreach ($format_fields as $format_field) {
                 $is_custom = $this->is_custom_field($format_field);
                 if ($is_custom) {
                     $joinAlias = $joinCustomTableAlias;
                     $has_custom_fields = true;
                 } else {
                     $joinAlias = $joinTableAlias;
                 }
                 $alias = $this->getRelateAlias($name, $format_field);
                 $fields[$alias] = $joinAlias . '.' . $format_field;
             }
             if (!empty($rname_field_def['sort_on'])) {
                 if ($joinTableAlias) {
                     $fields[$name] = $joinTableAlias . '.' . $rname_field_def['sort_on'];
                 } else {
                     $fields[$name] = $rname_field_def['sort_on'];
                 }
             }
         } elseif (isset($rname_field_def['db_concat_fields'])) {
             $fields[$name] = $this->db->concat($joinTableAlias, $rname_field_def['db_concat_fields']);
         } else {
             $fields[$name] = $rname;
             if ($joinTableAlias) {
                 $fields[$name] = $joinTableAlias . '.' . $fields[$name];
             }
         }
         $sort_fields = $this->getRelateSortColumns($rname_field_def, $joinTableAlias, $joinCustomTableAlias, $has_custom_fields);
     }
     $parts = array();
     foreach ($fields as $alias => $field) {
         if (!in_array($alias, $selectedFields)) {
             $parts[] = $field . ' ' . $alias;
         }
     }
     $select = implode(', ', $parts);
     if ($has_custom_fields) {
         $join = ' LEFT JOIN ' . $this->get_custom_table_name() . ' ' . $joinCustomTableAlias . ' ON ' . $joinCustomTableAlias . '.id_c = ' . $joinTableAlias . '.id';
     } else {
         $join = '';
     }
     return array('select' => $select, 'join' => $join, 'fields' => $fields, 'sort_fields' => $sort_fields);
 }
Exemple #3
0
 function getRelatedFields($module, $id, $fields, $return_array = false)
 {
     if (empty($GLOBALS['beanList'][$module])) {
         return '';
     }
     $object = BeanFactory::getObjectName($module);
     VardefManager::loadVardef($module, $object);
     if (empty($GLOBALS['dictionary'][$object]['table'])) {
         return '';
     }
     $table = $GLOBALS['dictionary'][$object]['table'];
     $query = 'SELECT id';
     foreach ($fields as $field => $alias) {
         if (!empty($GLOBALS['dictionary'][$object]['fields'][$field]['db_concat_fields'])) {
             $query .= ' ,' . $this->db->concat($table, $GLOBALS['dictionary'][$object]['fields'][$field]['db_concat_fields']) . ' as ' . $alias;
         } else {
             if (!empty($GLOBALS['dictionary'][$object]['fields'][$field]) && (empty($GLOBALS['dictionary'][$object]['fields'][$field]['source']) || $GLOBALS['dictionary'][$object]['fields'][$field]['source'] != "non-db")) {
                 $query .= ' ,' . $table . '.' . $field . ' as ' . $alias;
             }
         }
         if (!$return_array) {
             $this->{$alias} = '';
         }
     }
     if ($query == 'SELECT id' || empty($id)) {
         return '';
     }
     if (isset($GLOBALS['dictionary'][$object]['fields']['assigned_user_id'])) {
         $query .= " , " . $table . ".assigned_user_id owner";
     } else {
         if (isset($GLOBALS['dictionary'][$object]['fields']['created_by'])) {
             $query .= " , " . $table . ".created_by owner";
         }
     }
     $query .= ' FROM ' . $table . ' WHERE deleted=0 AND id=';
     $result = $GLOBALS['db']->query($query . "'{$id}'");
     $row = $GLOBALS['db']->fetchByAssoc($result);
     if ($return_array) {
         return $row;
     }
     $owner = empty($row['owner']) ? '' : $row['owner'];
     foreach ($fields as $alias) {
         $this->{$alias} = !empty($row[$alias]) ? $row[$alias] : '';
         $alias = $alias . '_owner';
         $this->{$alias} = $owner;
         $a_mod = $alias . '_mod';
         $this->{$a_mod} = $module;
     }
 }