Exemple #1
0
 /**
  * load all templates from database
  * @param  boolean $reload reload even if already all loaded
  * @return void
  */
 public function loadAll($reload = false)
 {
     //skip loading if already loaded and reload not true
     if ($this->loadedAll && !$reload) {
         return;
     }
     $this->reset();
     /* collecting template_fields */
     $fields = array();
     $fieldsByIndex = array();
     $recs = DM\TemplatesStructure::getFields();
     foreach ($recs as $r) {
         $templateId = $r['template_id'];
         unset($r['template_id']);
         $fields[$templateId][$r['id']] =& $r;
         $fieldsByIndex[$templateId][] =& $r;
         unset($r);
     }
     /* loading templates */
     $recs = DM\Templates::readAllWithData();
     foreach ($recs as $r) {
         $r['fields'] = empty($fields[$r['id']]) ? array() : $fields[$r['id']];
         $r['fieldsByIndex'] = empty($fieldsByIndex[$r['id']]) ? array() : $fieldsByIndex[$r['id']];
         /* store template in collection */
         $this->templates[$r['id']] = new \CB\Objects\Template($r['id'], false);
         $this->templates[$r['id']]->setData($r);
     }
     $this->loadedAll = true;
 }
Exemple #2
0
 /**
  * load template custom data
  */
 protected function loadCustomData()
 {
     parent::loadCustomData();
     $r = DM\Templates::read($this->id);
     if (!empty($r)) {
         //dont override name from tree with name from templates table
         unset($r['name']);
         $this->data = array_merge($this->data, $r);
     } else {
         throw new \Exception("Template load error: no template found with id = " . $this->id);
     }
     /* loading template fields */
     $this->data['fields'] = array();
     $this->data['fieldsByIndex'] = array();
     $this->fieldsOrder = array();
     $recs = DM\TemplatesStructure::getFields($this->id);
     foreach ($recs as &$r) {
         $this->data['fields'][$r['id']] =& $r;
         $this->data['fieldsByIndex'][] =& $r;
         $this->fieldsOrder[$r['name']] = intval($r['order']);
         unset($r);
     }
 }
Exemple #3
0
 protected function moveCustomDataTo($targetId)
 {
     DM\TemplatesStructure::move($this->id, $targetId);
 }
Exemple #4
0
 /**
  * return records for an objects field based on its config
  * @param  array $p
  * @return json  repsponce
  */
 public function getObjectsForField($p)
 {
     // ,"scope": 'tree' //project, parent, self, $node_id
     // ,"field": <field_name> //for field type
     // ,"descendants": true
     // /* filter used for objects */
     // ,+"tags": []
     // ,+"types": []
     // ,+"templates": []
     // ,"templateGroups": []
     //,+query - user query
     //unset restricted query params from user input
     unset($p['fq']);
     $ids = array();
     $fieldConfig = array();
     // get field config from database
     if (!empty($p['fieldId']) && is_numeric($p['fieldId'])) {
         $r = DM\TemplatesStructure::read($p['fieldId']);
         if (!empty($r['cfg'])) {
             $fieldConfig = $r['cfg'];
             if (!empty($r['cfg']['fq'])) {
                 $p['fq'] = $r['cfg']['fq'];
             }
         }
     }
     if (!empty($p['source'])) {
         if (is_array($p['source'])) {
             // a custom source
             $rez = array();
             if (empty($p['fieldId'])) {
                 return $rez;
             }
             //get custom method from config
             if (empty($fieldConfig['source']['fn'])) {
                 return $rez;
             }
             $method = explode('.', $fieldConfig['source']['fn']);
             $class = new $method[0]();
             $rez = $class->{$method}[1]($p);
             if (!empty($rez)) {
                 return $rez;
             }
         }
         switch ($p['source']) {
             case 'field':
                 switch ($p['scope']) {
                     case 'project':
                         $ids = DM\Tree::getCaseId(Path::detectRealTargetId($p['path']));
                         break;
                     case 'parent':
                         $ids = Path::detectRealTargetId($p['path']);
                         break;
                     default:
                         if (empty($p['pidValue']) || empty($p['field'])) {
                             break 2;
                         }
                         $ids = $p['pidValue'];
                 }
                 $ids = Util\toNumericArray($ids);
                 if (empty($ids)) {
                     break;
                 }
                 /*get distinct target field values for selected objects in parent field */
                 $obj = new Objects\Object();
                 $values = array();
                 foreach ($ids as $id) {
                     $obj->load($id);
                     $fv = $obj->getFieldValue($p['field'], 0);
                     $fv = Util\toNumericArray(@$fv['value']);
                     $values = array_merge($values, $fv);
                 }
                 $values = array_unique($values);
                 if (empty($values)) {
                     return array('success' => true, 'data' => array());
                 }
                 $p['ids'] = $values;
                 break;
         }
     }
     $pids = false;
     if (!empty($fieldConfig['scope'])) {
         $scope = $fieldConfig['scope'];
         switch ($scope) {
             case 'project':
                 /* limiting pid to project. If not in a project then to parent directory */
                 if (!empty($p['objectId']) && is_numeric($p['objectId'])) {
                     $pids = DM\Tree::getCaseId($p['objectId']);
                 } elseif (!empty($p['path'])) {
                     $pids = DM\Tree::getCaseId(Path::detectRealTargetId($p['path']));
                 }
                 break;
             case 'parent':
                 if (!empty($p['objectId']) && is_numeric($p['objectId'])) {
                     $pids = Objects::getPids($p['objectId']);
                     if (!empty($pids)) {
                         $p['pids'] = array_pop($pids);
                     }
                 } elseif (!empty($p['path'])) {
                     $pids = Path::detectRealTargetId($p['path']);
                 }
                 break;
             case 'self':
                 if (!empty($p['objectId']) && is_numeric($p['objectId'])) {
                     $p['pids'] = $p['objectId'];
                 } elseif (!empty($p['path'])) {
                     $pids = Path::detectRealTargetId($p['path']);
                 }
                 break;
             case 'variable':
                 $pids = empty($p['pidValue']) ? Path::detectRealTargetId($p['path']) : Util\toNumericArray($p['pidValue']);
                 break;
             default:
                 $pids = Util\toNumericArray($scope);
                 break;
         }
     }
     if (!empty($pids)) {
         if (empty($p['descendants'])) {
             $p['pid'] = $pids;
         } elseif (@$p['source'] !== 'field') {
             $p['pids'] = $pids;
         }
     }
     $p['fl'] = 'id,name,type,template_id,status';
     if (!empty($p['fields'])) {
         if (!is_array($p['fields'])) {
             $p['fields'] = explode(',', $p['fields']);
         }
         for ($i = 0; $i < sizeof($p['fields']); $i++) {
             $fieldName = trim($p['fields'][$i]);
             if ($fieldName == 'project') {
                 $fieldName = 'case';
             }
             if (in_array($fieldName, array('date', 'path', 'case', 'size', 'cid', 'oid', 'cdate', 'udate'))) {
                 $p['fl'] .= ',' . $fieldName;
             }
         }
     }
     //increase number of returned items
     if (empty($p['rows'])) {
         if (empty($p['limit'])) {
             if (empty($p['pageSize'])) {
                 $p['rows'] = 50;
             } else {
                 $p['rows'] = $p['pageSize'];
             }
         } else {
             $p['rows'] = $p['limit'];
         }
     }
     if (!is_numeric($p['rows'])) {
         $p['rows'] = 50;
     }
     $search = new Search();
     // temporary: Don't use permissions for Objects fields
     // it can be later reinforced per field in config
     $p['skipSecurity'] = true;
     $rez = $search->query($p);
     foreach ($rez['data'] as &$doc) {
         $ids[] = $doc['id'];
     }
     $recs = DM\Tree::readByIds($ids, true);
     foreach ($rez['data'] as &$doc) {
         if (!empty($recs[$doc['id']]['cfg']['iconCls'])) {
             $doc['iconCls'] = $recs[$doc['id']]['cfg']['iconCls'];
         }
     }
     if (empty($rez['DC'])) {
         $rez['DC'] = array('name' => array('solr_column_name' => "name", 'idx' => 0));
     }
     return $rez;
 }