getName(); } public function getSourceName() { return 'default_objects'; } public function init($force = false) {} public function isInitialized() { return true; } public function getName() { return 'AccessGroup'; } public function getRootTableName() { return 'CRM_LISTA_ZASOBOW'; } public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } public function getVirtualFieldListByIdZasob() { return array(); } public function getRealFieldListByIdZasob($force = false) { $cols[100000] = 'id';// CRM_LISTA_ZASOBOW.ID $cols[100001] = 'name';// CRM_LISTA_ZASOBOW.DESC $cols[100002] = 'uid';// Ldap.uid -> value stored in fields: A_ADM_COMPANY, A_CLASSIFIED return $cols; } public function getFields() { $fields = array(); $fields[100000] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>100]; $fields[100001] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>101]; $fields[100002] = ['name'=>'uid', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>102]; return $fields; } public function getFieldType($fieldName) { return null; } // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName public function canCreateField($fieldName) { return false; } public function canReadField($fieldName) { return true; } public function canReadObjectField($fieldName, $record) {return true; } public function canWriteField($fieldName) { return false; } public function canWriteObjectField($fieldName, $record) { return false; } public function getTotal($params = array()) { return count($this->getItems($params)); } public function getItem($primaryKey, $params = []) { $items = $this->getItems(['primaryKey'=>$primaryKey]); return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null; } public function getItems($params = array()) { $DBG = V::get('DBG_DS', 0, $_GET, 'int'); if($DBG>2){echo'
';}
    if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
    $items = array();
    // TODO: fetch groups connectes with current user
  {
    $userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin());
    if($DBG>4){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$userLdapGroups:";print_r($userLdapGroups);echo "\n";}
    if (empty($userLdapGroups)) throw new Exception("User groups not found", 404);

    foreach ($userLdapGroups as $vLdapGroup) {
      $allowGroup = false;
      if ('workgroup' == $vLdapGroup->cn) {
        $items[0] = ['id'=>'0', 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
      } else {
        $cnTest = str_replace('-', '_', $vLdapGroup->cn);
        $cnTest = explode('_', $cnTest);
        $idZasob = $cnTest[0];
        if (!is_numeric($idZasob)) {
          if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems - skip cn - missing id zasob \$vLdapGroup->cn:";print_r($vLdapGroup->cn);echo "\n";}
          continue;
        }
        $items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn];
      }
    }
  }
    if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948
      if (!array_key_exists($pk, $items)) return array();
      $items = array($pk => $items[$pk]);
    }
    if (!empty($params['ogc:Filter'])) {
      $parser = new ParseOgcFilter();
      $parser->loadOgcFilter($params['ogc:Filter']);
      $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
      DBG::_('DBG_DS', '>2', "ogc:Filter \$queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
      if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
      $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
    }
    $filterId = trim(V::get('f_id', '', $params));
    if (strlen($filterId)) {// allow '0'
      $queryWhereBuilder = new SqlQueryWhereBuilder();
      if (is_numeric($filterId)) {
        $queryWhereBuilder->addComparisonFieldToValue('id', '=', $filterId);
      } else if (false !== strpos($filterId, '%') && is_numeric(trim($filterId, '%'))) {
        $queryWhereBuilder->addComparisonFieldToValue('id', 'like', $filterId);
      } else if ('>=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
        $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThenOrEqualTo', substr($filterId, 2));
      } else if ('<=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) {
        $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThenOrEqualTo', substr($filterId, 2));
      } else if ('>' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
        $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThen', substr($filterId, 1));
      } else if ('<' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
        $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThen', substr($filterId, 1));
      } else if ('=' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) {
        $queryWhereBuilder->addComparisonFieldToValue('id', '=', substr($filterId, 1));
      } else {
        $filterId = null;// TODO: BUG uniimplemented comparison sign
      }
      if ($filterId) $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
    }

    foreach (['name', 'uid'] as $fieldName) {
      $filterValue = trim(V::get("f_{$fieldName}", '', $params));
      if (strlen($filterValue)) {// allow '0'
        $queryWhereBuilder = new SqlQueryWhereBuilder();
        if (!is_scalar($filterValue)) {
        } else if ('=' == substr($filterValue, 0, 1)) {
          $queryWhereBuilder->addComparisonFieldToValue($fieldName, '=', substr($filterValue, 1));
        } else {
          if ('%' != substr($filterValue, 0, 1)) $filterValue = "%{$filterValue}";
          if ('%' != substr($filterValue, -1)) $filterValue = "{$filterValue}%";
          $queryWhereBuilder->addComparisonFieldToValue($fieldName, 'like', $filterValue);
        }
        $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray'));
      }
    }

    $orderBy = strtolower(V::get('order_by', 'id', $params));
    $orderDir = strtolower(V::get('order_dir', 'desc', $params));
    if (!in_array($orderBy, ['id', 'name', 'uid'])) throw new HttpException("Bad Request - wrong or missing order by", 400);
    if (!in_array($orderDir, ['desc', 'asc'])) throw new HttpException("Bad Request - wrong or missing order dir", 400);
    uasort($items, function ($a, $b) use ($orderBy, $orderDir) {
      if ('desc' == $orderDir) {
        return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? -1 : 1;
      } else if ('asc' == $orderDir) {
        return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? 1 : -1;
      }
      return 0;
    });

    if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
    return $items;
  }
  public function addItem($itemTodo) { throw new Exception("Insert not allowed"); }
  public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
  public function getGeomFieldType($fieldName) { return null; }
  public function getPrimaryKeyField() { return 'id'; }
  public function getAttributesFromZasoby() { return array(); }
  public function isEnumerationField($fieldName) { return false; }
  public function getEnumerations($fieldName) { return null; }
  public function getXsdFieldType($fieldName) {
    if ('id' == $fieldName) return 'xsd:string';
    if ('name' == $fieldName) return 'xsd:string';
    if ('uid' == $fieldName) return 'xsd:string';
  }
  public function isGeomField($fldName) { return false; }

}