public function testDeleteByQuery() { $this->solr->deleteByQuery('*:*'); $search = new Search(); $rez = $search->query(array('rows' => 0)); $this->assertTrue($rez['total'] == 0, 'Delete all by query didnt clear the solr instance.'); }
public function getChildren(&$pathArray, $requestParams) { $pid = null; /* should start with path check and see if child request is for a real db node*/ if (empty($pathArray)) { if (empty($requestParams['query'])) { return; } } else { $lastNode = @$pathArray[sizeof($pathArray) - 1]; if ($lastNode instanceof Dbnode || get_class($lastNode) == 'CB\\TreeNode\\Base') { $pid = $lastNode->id; } else { //we are under another node type $cfg = $lastNode->getConfig(); if (!empty($cfg['realNodeId']) && $lastNode instanceof RealSubnode) { $pid = $cfg['realNodeId']; } else { return array(); } } } if (empty($pid)) { return array(); } /* end of check */ $p =& $requestParams; $folderTemplates = \CB\Config::get('folder_templates'); $p['fl'] = 'id,pid,system,path,name,case_id,date,date_end,size,cid,' . 'oid,cdate,uid,udate,template_id,acl_count,cls,status,task_status,versions,' . 'comment_user_id,comment_date'; if (empty($p['showFoldersContent'])) { $p['templates'] = $folderTemplates; } if (empty($p['descendants'])) { $p['pid'] = $pid; } else { $p['pids'] = $pid; } $s = new \CB\Search(); $rez = $s->query($p); if (!empty($rez['data'])) { for ($i = 0; $i < sizeof($rez['data']); $i++) { $d =& $rez['data'][$i]; $r = DM\Tree::read($d['id']); if (!empty($r['cfg']) && $p['from'] == 'tree') { if (isset($r['cfg']['loaded'])) { $d['loaded'] = $r['cfg']['loaded']; } if (isset($r['cfg']['expanded'])) { $d['expanded'] = $r['cfg']['expanded']; } if (isset($r['cfg']['leaf'])) { $d['leaf'] = $r['cfg']['leaf']; } } } \CB\Tasks::setTasksActionFlags($rez['data']); } return $rez; }
/** * getChildNodes description * @return json responce */ protected function getChildNodes() { $p = $this->requestParams; unset($p['facets']); $fq = empty($this->config['fq']) ? array() : $this->config['fq']; $this->replaceFilterVars($fq); $p['fq'] = $fq; $s = new \CB\Search(); $rez = $s->query($p); return $rez; }
public function getData($id = false) { $rez = array('success' => true); parent::getData($id); $preview = Objects::getPreview($this->id); $obj = Objects::getCachedObject($this->id); if (empty($obj)) { return $rez; } $data = $obj->getData(); if (!empty($preview)) { $rez['data'] = array('preview' => $preview); } if (!empty($data)) { if (!empty($data['pids'])) { $path = explode(',', $data['pids']); array_pop($path); $rez['data']['pids'] = $rez['data']['path'] = implode('/', $path); $arr = array(&$rez['data']); Search::setPaths($arr); } foreach ($data as $k => $v) { if (in_array($k, array('id', 'template_id', 'date_end', 'cid', 'uid', 'cdate', 'udate'))) { if (in_array($k, array('date', 'date_end', 'cdate', 'udate'))) { $v = Util\dateMysqlToISO($v); } $rez['data'][$k] = $v; //add ago udate text if (in_array($k, array('cdate', 'udate'))) { $rez['data'][$k . '_ago_text'] = Util\formatAgoTime($v); } } } $rez['data']['name'] = $obj->getName(); } $rez['data']['can'] = $obj->getActionFlags(); //set status info for tasks if not active if ($obj->getType() == 'task') { $d =& $rez['data']; $d['status'] = ''; switch ($obj->getStatus()) { case Objects\Task::$STATUS_ACTIVE: break; case Objects\Task::$STATUS_CLOSED: //just add title css class and continue with default $d['titleCls'] = 'task-completed'; // break; // break; default: $d['status'] = $obj->getStatusText(); $d['statusCls'] = $obj->getStatusCSSClass(); } } return $rez; }
/** * test search exception */ public function testSearchException() { $search = new \CB\Api\Search(); try { $result = $search->query(['strictSort' => 'erorrneous_data_to_receive_exception asc']); $this->assertTrue(false, 'No exception on wrong date'); } catch (\Exception $e) { $this->assertTrue(true); } try { $result = \CB\Search::getObjects(7); $this->assertTrue(true); $result = \CB\Search::getObjects(1, '"erorrneous field list\\/'); $this->assertTrue(false, 'No exception on getObjects'); } catch (\Exception $e) { $this->assertTrue(true); } }
public function getData($id = false) { $rez = array('success' => true, 'data' => array()); parent::getData($id); $obj = $this->getObjectClass(); if (!is_object($obj)) { return $rez; } $data = $obj->getData(); $rez['data'] = array_intersect_key($data, array('id' => 1, 'name' => 1, 'template_id' => 1, 'cid' => 1, 'cdate' => 1, 'uid' => 1, 'udate' => 1, 'dstatus' => 1, 'did' => 1, 'ddate' => 1, 'size' => 1)); $d =& $rez['data']; $pids = Util\toNumericArray($data['pids']); array_pop($pids); $d['pids'] = $d['path'] = implode('/', $pids); $arr = array(&$d); Search::setPaths($arr); $d['template_name'] = Objects::getName($d['template_id']); $sd = $obj->getSysData(); $userId = User::getId(); $d['subscription'] = 'ignore'; if (!empty($sd['fu']) && in_array($userId, $sd['fu'])) { $d['subscription'] = 'watch'; //follow } if (!empty($sd['wu']) && in_array($userId, $sd['wu'])) { $d['subscription'] = 'watch'; } $d['cid_text'] = User::getDisplayName($d['cid']); $d['cdate_ago_text'] = Util\formatAgoTime($d['cdate']); $d['cdate'] = Util\dateMysqlToISO($d['cdate']); $d['udate'] = Util\dateMysqlToISO($d['udate']); $d['uid_text'] = User::getDisplayName($d['uid']); $d['udate_ago_text'] = Util\formatAgoTime($d['udate']); if (!empty($d['dstatus'])) { $d['did_text'] = User::getDisplayName($d['did']); $d['ddate_text'] = Util\formatAgoTime($d['ddate']); } return $rez; }
protected static function getMenuRules() { $rez = Cache::get('CreateMenuRules', array()); if (!empty($rez)) { return $rez; } $s = new Search(); $ids = array(); $sr = $s->query(array('fl' => 'id', 'template_types' => 'menu', 'skipSecurity' => true)); foreach ($sr['data'] as $r) { $ids[] = $r['id']; } $arr = Objects::getCachedObjects($ids); foreach ($arr as $o) { $d = $o->getData()['data']; $rez[] = array('nids' => empty($d['node_ids']) ? array() : Util\toNumericArray($d['node_ids']), 'ntids' => empty($d['template_ids']) ? array() : Util\toNumericArray($d['template_ids']), 'ugids' => empty($d['user_group_ids']) ? array() : Util\toNumericArray($d['user_group_ids']), 'menu' => $d['menu']); } Cache::set('CreateMenuRules', $rez); return $rez; }
/** * get items * @return json responce */ protected function getItems() { $rez = array('data' => array()); $p = $this->requestParams; $fq = empty($this->config['fq']) ? array() : $this->config['fq']; $p['fq'] = array_merge($fq, $this->getParentNodeFilters()); $this->replaceFilterVars($p['fq']); $s = new \CB\Search(); $rez = $s->query($p); return $rez; }
/** * get the list of objects referenced inside another object * @param array | int $p params * @return json response */ public static function getAssociatedObjects($p) { $data = array(); if (is_numeric($p)) { $p = array('id' => $p); } if (empty($p['id']) && empty($p['template_id'])) { return array('success' => true, 'data' => $data, 's' => '1'); } $ids = array(); $template = null; if (!empty($p['id'])) { // SECURITY: check if current user has at least read access to this case if (!Security::canRead($p['id'])) { throw new \Exception(L\get('Access_denied')); } /* select distinct associated case ids from the case */ $obj = new Objects\Object($p['id']); $obj->load(); $template = $obj->getTemplate(); $linearData = $obj->getLinearData(); foreach ($linearData as $f) { $tf = $template->getField($f['name']); if ($tf['type'] == '_objects') { $a = Util\toIntArray(@$f['value']); $ids = array_merge($ids, $a); } } } else { $template = new Objects\Template($p['template_id']); $template->load(); } if (!empty($p['data']) && is_array($p['data'])) { foreach ($p['data'] as $key => $value) { $a = Util\toIntArray($value); $ids = array_merge($ids, $a); } } if ($template) { $templateData = $template->getData(); foreach ($templateData['fields'] as $field) { if (!empty($field['cfg']['value'])) { $a = Util\toIntArray($field['cfg']['value']); $ids = array_merge($ids, $a); } } } $ids = array_unique($ids); if (empty($ids)) { return array('success' => true, 'data' => array()); } /* end of select distinct case ids from the case */ $data = Search::getObjects($ids, 'id,template_id,name,date,status:task_status'); $data = array_values($data); return array('success' => true, 'data' => $data); }
public function getGroupItems() { $params = $this->requestParams; $params['fq'] = $this->fq; switch ($this->lastNode->id) { case 'commented': $params['fq'][] = 'comment_user_id: [* TO *]'; $params['strictSort'] = 'comment_date desc'; break; case 'modified': $params['fq'][] = 'uid: [* TO *]'; $params['strictSort'] = 'udate desc'; break; case 'added': $params['fq'][] = 'cid: [* TO *]'; $params['strictSort'] = 'cdate desc'; break; } $s = new \CB\Search(); $rez = $s->query($params); return $rez; }
/** * 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']); $fieldConfig = array(); // get field config from database if (!empty($p['fieldId']) && is_numeric($p['fieldId'])) { $res = DB\dbQuery('SELECT cfg FROM templates_structure WHERE id = $1', $p['fieldId']) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { $fieldConfig = Util\jsonDecode($r['cfg']); //set "fq" param from database (dont trust user imput) if (!empty($fieldConfig['fq'])) { $p['fq'] = $fieldConfig['fq']; } } $res->close(); } 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': $ids = array(); switch ($p['scope']) { case 'project': $ids = $this->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 = $this->getCaseId($p['objectId']); } elseif (!empty($p['path'])) { $pids = $this->getCaseId(Path::detectRealTargetId($p['path'])); } break; case 'parent': if (!empty($p['objectId']) && is_numeric($p['objectId'])) { $p['pids'] = $this->getPid($p['objectId']); } 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'])) { $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) { $res = DB\dbQuery('SELECT cfg FROM tree WHERE id = $1 AND cfg IS NOT NULL', $doc['id']) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { if (!empty($r['cfg'])) { $cfg = Util\toJSONArray($r['cfg']); if (!empty($cfg['iconCls'])) { $doc['iconCls'] = $cfg['iconCls']; } } } $res->close(); } if (empty($rez['DC'])) { $rez['DC'] = array('name' => array('solr_column_name' => "name", 'idx' => 0)); } return $rez; }
/** * method to get multiple object properties from solr * Multilanguage plugin works also * * @param array | string $ids * @param string $fieldList * @return array associative array of properties per id */ public static function getObjects($ids, $fieldList = 'id,name') { $rez = array(); $ids = Util\toNumericArray($ids); if (!empty($ids)) { $chunks = array_chunk($ids, 200); //execute search try { foreach ($chunks as $chunk) { $params = array('fl' => $fieldList, 'facet' => false, 'skipSecurity' => true, 'fq' => array('id:(' . implode(' OR ', $chunk) . ')')); $search = new Search(); $sr = $search->query($params); if (!empty($sr['data'])) { foreach ($sr['data'] as &$d) { $rez[$d['id']] = $d; } } } } catch (\Exception $e) { throw new \Exception("An error occured in getObjects: \n\n {$e->__toString()}"); } } return $rez; }
protected function getCases() { $fq = $this->fq; $parent = $this->lastNode->parent; switch ($parent->id) { case 4: //all my cases $fq[] = '(role_ids2:' . $this->user_id . ' OR role_ids3:' . $this->user_id . ')'; break; default: $fq[] = 'role_ids' . $parent->id . ':' . $this->user_id; } $fq[] = 'status:' . $this->lastNode->id; $s = new \CB\Search(); $rez = $s->query(array('fq' => $fq)); return $rez; }