Example #1
0
 /**
  * return templates list
  * @param  array $p
  * @return json  response
  */
 public function readAll($p)
 {
     $rez = DM\Templates::readAllWithData();
     foreach ($rez as &$r) {
         $r['title'] = Util\detectTitle($r['data']);
         unset($r['data']);
         unset($r['cfg']['source']['fn']);
     }
     return $rez;
 }
Example #2
0
 /**
  * load template custom data
  */
 protected function loadCustomData()
 {
     parent::loadCustomData();
     $r = DM\Objects::read($this->id);
     if (!empty($r)) {
         $r = $r['data'];
     } else {
         //read from templates_structure if object not present in tree
         // for backward compatibility
         $r = DM\TemplatesStructure::read($this->id);
     }
     if (!empty($r)) {
         if (isset($r['cfg'])) {
             $r['cfg'] = Util\toJSONArray($r['cfg']);
         }
         $r['title'] = Util\detectTitle($r);
         $this->data = array_merge($this->data, $r);
     } else {
         \CB\debug("Template field load error: no field found with id = " . $this->id);
         // throw new \Exception("Template field load error: no field found with id = ".$this->id);
     }
 }
Example #3
0
    /**
     * get only active (not deleted fields) for given template
     * @param  int   $templateId optional, filter by a template
     * @param  bool  $onlyActive to return only active (nit deleted fields)
     * @return array
     */
    public static function getFields($templateId = false, $onlyActive = true)
    {
        $rez = array();
        $sql = 'SELECT
                ts.id
                ,ts.pid
                ,ts.template_id
                ,ts.name
                ,ts.`level`
                ,ts.`type`
                ,ts.cfg
                ,ts.order
                ,ts.solr_column_name
                ,o.data

            FROM templates_structure ts
            LEFT JOIN objects o ON ts.id = o.id ';
        if ($onlyActive) {
            $sql .= 'JOIN tree t on ts.id = t.id AND t.dstatus = 0 ';
        }
        if (is_numeric($templateId)) {
            $sql .= 'WHERE ts.template_id = $1 ';
        }
        $sql .= 'ORDER BY ts.template_id, ts.`order` ';
        $res = DB\dbQuery($sql, $templateId);
        while ($r = $res->fetch_assoc()) {
            $data = Util\toJSONArray($r['data']);
            unset($r['data']);
            //overwrite fields from templates table with values from objects.data
            $r = array_merge($r, $data);
            $r['cfg'] = Util\toJSONArray($r['cfg']);
            $r['title'] = Util\detectTitle($r);
            $rez[] = $r;
        }
        $res->close();
        return $rez;
    }
Example #4
0
 /**
  * method used to implement custom logic on solr query
  * @param  array $p search params
  * @return void
  */
 public function onSolrQuery(&$p)
 {
     $this->params =& $p;
     $ip =& $p['inputParams'];
     if (@$ip['view']['type'] !== $this->fromParam) {
         return;
     }
     // $sp = &$p['params'];
     $result =& $p['result'];
     $view =& $result['view'];
     $data =& $result['data'];
     $rez = array();
     $displayColumns = $this->getDC();
     //This if remains as backward compatible, but will be removed in future commits
     if (!empty($displayColumns['sort'])) {
         $view['sort'] = $displayColumns['sort'];
     }
     //get state
     $stateFrom = empty($displayColumns['from']) ? 'default' : $displayColumns['from'];
     $state = $this->getState($stateFrom);
     $customColumns = $this->prepareColumnsConfig($displayColumns);
     //set custom display columns data
     if (!empty($displayColumns['data'])) {
         // fill custom columns data
         foreach ($data as &$doc) {
             if (!is_numeric($doc['id'])) {
                 continue;
             }
             $obj = \CB\Objects::getCachedObject($doc['id']);
             if (!is_object($obj)) {
                 \CB\debug('DisplayColumns object not found: ' . $doc['id']);
                 continue;
             }
             $template = $obj->getTemplate();
             foreach ($customColumns as $fieldName => &$col) {
                 $templateField = $template->getField($col['fieldName']);
                 $values = array();
                 if (!empty($col['solr_column_name'])) {
                     if (isset($doc[$col['solr_column_name']]) && $col['solr_column_name'] !== $col['fieldName']) {
                         $v = $doc[$col['solr_column_name']];
                         $doc[$col['fieldName']] = $v;
                         unset($doc[$col['solr_column_name']]);
                         $values = array($v);
                     }
                     if (empty($templateField)) {
                         $templateField = array('type' => empty($col['fieldType']) ? 'varchar' : $col['fieldType'], 'name' => $col['solr_column_name'], 'title' => Util\detectTitle($col));
                     }
                 } elseif (!empty($col['lookup'])) {
                     //lookup field
                     $values = $obj->getLookupValues($col['lookup'], $templateField);
                 } else {
                     //default
                     $values = isset($doc[$col['fieldName']]) ? array($doc[$col['fieldName']]) : $obj->getFieldValue($col['fieldName']);
                 }
                 //populate column properties if empty
                 if (empty($col['title'])) {
                     $col['title'] = $templateField['title'];
                 }
                 if (empty($col['sortType']) && empty($col['solr_column_name'])) {
                     switch ($templateField['type']) {
                         case 'date':
                         case 'datetime':
                             $col['sortType'] = 'asDate';
                             break;
                         case 'float':
                             $col['sortType'] = 'asFloat';
                             break;
                         case 'checkbox':
                         case 'int':
                             $col['sortType'] = 'asInt';
                             break;
                         case 'html':
                         case 'memo':
                         case 'text':
                             $col['sortType'] = 'asUCText';
                             break;
                         case '_objects':
                         default:
                             $col['sortType'] = 'asUCString';
                             break;
                     }
                 }
                 //update value from document if empty from solr query
                 if (empty($doc[$fieldName]) || in_array($templateField['type'], array('_objects', 'time'))) {
                     $dv = array();
                     foreach ($values as $value) {
                         $value = is_array($value) ? @$value['value'] : $value;
                         $dv[] = $template->formatValueForDisplay($templateField, $value, false);
                     }
                     $doc[$fieldName] = implode(', ', $dv);
                 }
             }
         }
         /* remove columns without title */
         foreach ($customColumns as $fieldName => &$col) {
             if (empty($col['title'])) {
                 unset($customColumns[$fieldName]);
             }
         }
         $rez = $customColumns;
     }
     /* merge the state with display columns */
     $defaultColumns = array_keys(Config::getDefaultGridViewColumns());
     if (!empty($state['columns'])) {
         $rez = array();
         foreach ($state['columns'] as $k => $c) {
             if (!empty($customColumns[$k])) {
                 unset($customColumns[$k]['hidden']);
                 $c = array_merge($customColumns[$k], $c);
                 unset($customColumns[$k]);
                 $rez[$k] = $c;
             } elseif (in_array($k, $defaultColumns)) {
                 $rez[$k] = $c;
             }
         }
         if (!empty($customColumns)) {
             $rez = array_merge($rez, $customColumns);
         }
     }
     /* user clicked a column to sort by */
     if (!empty($ip['userSort'])) {
         $view['sort'] = array('property' => $ip['sort'][0]['property'], 'direction' => $ip['sort'][0]['direction']);
     } elseif (!empty($state['sort'])) {
         $view['sort'] = $state['sort'];
     }
     /* end of get user state and merge the state with display columns */
     //check grouping params
     if (!empty($ip['userGroup']) && !empty($ip['group'])) {
         $view['group'] = array('property' => $ip['sourceGroupField'], 'direction' => $ip['group']['direction']);
     } elseif (isset($state['group'])) {
         $view['group'] = $state['group'];
     } elseif (isset($displayColumns['group'])) {
         $view['group'] = $displayColumns['group'];
     }
     //analize grouping
     $this->analizeGrouping($p);
     if (!empty($rez)) {
         $result['DC'] = $rez;
     }
     /* check if we need to sort records using php (in case sort field is not from solr)*/
     if (!empty($view['sort']) && !empty($rez[$view['sort']['property']]['localSort']) && !in_array($view['sort']['property'], $defaultColumns)) {
         $s =& $view['sort'];
         Util\sortRecordsArray($data, $s['property'], $s['direction'], empty($rez[$s['property']]['sortType']) ? 'asString' : $rez[$s['property']]['sortType']);
     }
 }
Example #5
0
 /**
  * get view config for given view or default view if set in config
  * @param  array &$pathArray
  * @param  array &$rp        requestParams
  * @return array
  */
 public function getViewConfig(&$pathArray, &$rp)
 {
     $rez = array();
     if (!$this->acceptedPath($pathArray, $requestParams)) {
         return $rez;
     }
     $cfg =& $this->config;
     if (!empty($cfg['view'])) {
         $rez = is_scalar($cfg['view']) ? array('type' => $cfg['view']) : $cfg['view'];
     }
     if (empty($rez['type'])) {
         $rez['type'] = 'grid';
     } elseif ($rez['type'] == 'stream') {
         $rez['type'] = 'activityStream';
     }
     if (!empty($rp['userViewChange'])) {
         $rez['type'] = empty($rp['view']) ? $rp['from'] : $rp['view'];
     }
     if (!empty($rez)) {
         if (!empty($cfg['views'][$rez['type']])) {
             $rez = array_merge($rez, $cfg['views'][$rez['type']]);
         } elseif ($rez['type'] == 'activityStream' && !empty($cfg['views']['stream'])) {
             $rez = array_merge($rez, $cfg['views']['stream']);
         }
         //backward compatibility check
         switch ($rez['type']) {
             case 'pivot':
             case 'charts':
                 if (!empty($cfg['stats'])) {
                     $stats = array();
                     foreach ($cfg['stats'] as $item) {
                         $stats[] = array('title' => Util\detectTitle($item), 'field' => $item['field']);
                     }
                     $rez['stats'] = $stats;
                 }
                 $rez['sort'] = null;
                 // $rez['rows'] = 0;
                 break;
             default:
                 // grid
                 // if (!empty($cfg['view']['group'])) {
                 //     $rez['group'] = $cfg['view']['group'];
                 // }
         }
     }
     return $rez;
 }
Example #6
0
 /**
  * get view config for given view or default view if set in config
  * @param  array &$pathArray
  * @param  array &$rp        requestParams
  * @return array
  */
 public function getViewConfig(&$pathArray, &$rp)
 {
     $rez = array();
     if (!$this->acceptedPath($pathArray, $requestParams)) {
         return $rez;
     }
     $cfg =& $this->config;
     $view = array();
     if (!empty($cfg['view'])) {
         $view = is_scalar($cfg['view']) ? array('type' => $cfg['view']) : $cfg['view'];
     }
     if (empty($view['type'])) {
         $view['type'] = 'grid';
     }
     if (!empty($rp['userViewChange'])) {
         $view['type'] = empty($rp['view']) ? $rp['from'] : $rp['view'];
     }
     if (!empty($view)) {
         $rez['view'] = $view;
         switch ($view['type']) {
             case 'pivot':
             case 'charts':
                 if (!empty($cfg['stats'])) {
                     $stats = array();
                     foreach ($cfg['stats'] as $item) {
                         $stats[] = array('title' => Util\detectTitle($item), 'field' => $item['field']);
                     }
                     $rez['stats'] = $stats;
                 }
                 $rez['sort'] = null;
                 $rez['rows'] = 0;
                 break;
             default:
                 // grid
                 // if (!empty($cfg['view']['group'])) {
                 //     $rez['group'] = $cfg['view']['group'];
                 // }
         }
     }
     return $rez;
 }
Example #7
0
 /**
  * analize and adjust view config if needed
  * @param  array $viewConfig
  * @param  array $rp
  * @return array
  */
 public function adjustViewConfig($viewConfig, &$rp)
 {
     if (empty($viewConfig)) {
         return array();
     }
     $rez = $viewConfig;
     $cfg =& $this->config;
     if (!empty($cfg['views'][$rez['type']])) {
         $rez = array_merge($rez, $cfg['views'][$rez['type']]);
     } elseif ($rez['type'] == 'activityStream' && !empty($cfg['views']['stream'])) {
         $rez = array_merge($rez, $cfg['views']['stream']);
     }
     //dashboards extention check
     if (!empty($rez['extends'])) {
         $rez = Config::extend('dashboards', $rez);
     }
     switch ($rez['type']) {
         //backward compatibility check
         case 'pivot':
         case 'charts':
             if (!empty($cfg['stats'])) {
                 $stats = array();
                 foreach ($cfg['stats'] as $item) {
                     $stats[] = array('title' => Util\detectTitle($item), 'field' => $item['field']);
                 }
                 $rez['stats'] = $stats;
             }
             $rez['sort'] = null;
             //check renamed options
             if (isset($rez['chart_type']) && empty($rez['chartType'])) {
                 $rez['chartType'] = $rez['chart_type'];
                 unset($rez['chart_type']);
             }
             if (isset($rez['pivot_type']) && empty($rez['pivotType'])) {
                 $rez['pivotType'] = $rez['pivot_type'];
                 unset($rez['pivot_type']);
             }
             break;
         case 'dashboard':
             //analize dashboard items and merge referenced config if any
             if (empty($rez['items'])) {
                 $rez['items'] = array();
             }
             $this->subClasses = array();
             $copyConfigProperties = ['title', 'cellCls', 'rowspan', 'colspan', 'width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight'];
             foreach ($rez['items'] as $k => $v) {
                 if (!empty($v['extends'])) {
                     $rez['items'][$k] = Config::extend('treeNodes', $v);
                 }
                 $clsArr = Path::getNodeClasses($rez['items']);
                 if (!empty($clsArr)) {
                     $class = current($clsArr);
                     if (isset($v['pid'])) {
                         $class->id = $v['pid'];
                     }
                     $this->subClasses[$k] = $class;
                     $path = array($class);
                     $customRp = $rp;
                     unset($customRp['userViewChange']);
                     $vc = $class->getViewConfig($path, $customRp);
                     $rez['items'][$k] = array_merge($vc, array_intersect_key($v, array_flip($copyConfigProperties)));
                 }
             }
             break;
         default:
             // grid
             // if (!empty($cfg['view']['group'])) {
             //     $rez['group'] = $cfg['view']['group'];
             // }
     }
     return $rez;
 }