public function initialize(AgaviContext $context, array $parameters = array())
 {
     parent::initialize($context, $parameters);
     $this->configAll = AgaviConfig::get('modules.web.simpledataprovider');
     $this->apiSearch = $this->getContext()->getModel('Icinga.ApiContainer', 'Web')->createSearch();
     /**
      * @todo Use new Implementation (Doctrine Query)
      */
     IcingaPrincipalTargetTool::applyApiSecurityPrincipals($this->apiSearch);
     $this->tm = $this->getContext()->getTranslationManager();
 }
 public function executeRead(AgaviRequestDataHolder $rd)
 {
     if (!$this->context->getUser()->isAuthenticated() || $this->getContainer()->getAttribute('success', 'org.icinga.api.auth', true) == false) {
         return array('Api', 'GenericError');
     }
     $context = $this->getContext();
     $API = $context->getModel("Icinga.ApiContainer", "Web");
     $target = $rd->getParameter("target");
     $connection = $rd->getParameter("connection", "icinga");
     $this->context->getModel("DBALMetaManager", "Api")->switchIcingaDatabase($connection);
     $search = @$API->createSearch($connection)->setSearchTarget($target);
     $this->addFilters($search, $rd);
     $this->setColumns($search, $rd);
     $this->setGrouping($search, $rd);
     $this->setOrder($search, $rd);
     $this->setLimit($search, $rd);
     $search->setResultType(IcingaApiConstants::RESULT_ARRAY);
     // Adding security principal targets to the query
     IcingaPrincipalTargetTool::applyApiSecurityPrincipals($search);
     $res = $search->fetch()->getAll();
     // if we had blob columns in selection we want to join them
     if (count($this->selected_blob_columns) > 0) {
         // get ID list
         $columns_default = self::$defaultColumns[$rd->getParameter("target")];
         $idfield = $columns_default[0];
         $idlist = array();
         // iterate thru result and collect ids
         for ($i = 0; $i < count($res); $i++) {
             if (isset($res[$i][$idfield])) {
                 $idlist[] = $res[$i][$idfield];
             }
         }
         // build search
         if (!empty($idlist)) {
             $columns = $this->selected_blob_columns;
             $columns[] = $idfield;
             $search = @$API->createSearch($connection)->setSearchTarget($target);
             $search->setDistinct(false);
             // force no distinct here - cause we are selecting BLOBs
             $search->setResultColumns($columns);
             $search->setSearchFilter($idfield, $idlist);
             $subres = $search->fetch()->getAll();
             // foreach result from the blobfield search
             foreach ($subres as &$subrow) {
                 $id = $subrow[$idfield];
                 // check against each original resul
                 for ($i = 0; $i < count($res); $i++) {
                     $resRow = $res[$i];
                     // merge the data when matching
                     if ($resRow[$idfield] == $id) {
                         $resRow = array_merge($subrow, $resRow);
                         $res[$i] = $resRow;
                     }
                 }
             }
         }
     }
     //Setup count
     if ($rd->getParameter("countColumn")) {
         $search = @$API->createSearch()->setSearchTarget($target);
         $this->addFilters($search, $rd);
         $this->setColumns($search, $rd);
         $this->setGrouping($search, $rd);
         $this->setOrder($search, $rd);
         $this->setLimit($search, $rd);
         $search->setSearchType(IcingaApiConstants::SEARCH_TYPE_COUNT);
         IcingaPrincipalTargetTool::applyApiSecurityPrincipals($search);
         $rd->setParameter("searchCount", $search->fetch()->getAll());
     }
     if ($rd->getParameter("withSLA") && ($target == "host" || $target == "service")) {
         $slaDefaults = AgaviConfig::get("modules.api.sla_settings");
         if (isset($slaDefaults["enabled"]) && $slaDefaults["enabled"]) {
             if (!isset($slaDefaults["default_timespan"])) {
                 $slaDefaults["default_timespan"] = "-1 Month";
             }
             $ts = $rd->getParameter("slaTimespan", $slaDefaults["default_timespan"]);
             $this->addSLAData($res, $ts);
         }
     }
     // Rewrite output, e.g. plugin_output
     // see #2598
     $rewrite = $this->getContext()->getModel('Result.OutputRewrite', 'Api', array('target' => $target, 'optional' => $rd->getParameter('enableRewrite', false)));
     $res = $rewrite->rewrite($res);
     $rd->setParameter("searchResult", $res);
     return $this->getDefaultViewName();
 }
 private function getDsArray($name, array $filters = array(), $index = false, $ignore_defined_filters = false)
 {
     if (array_key_exists($name, $this->ds)) {
         $dataSource = $this->ds[$name];
         if (!array_key_exists('target', $dataSource)) {
             throw new Cronks_System_StaticContentTemplateException('Datasource \'%s\' needs attribute target!', $name);
         } else {
             $apiSearch = $this->api->getConnection()->createSearch()->setResultType(IcingaApiConstants::RESULT_ARRAY)->setSearchTarget(constant($dataSource['target']));
             // set search type
             if (array_key_exists('search_type', $dataSource)) {
                 $apiSearch->setSearchType(constant($dataSource['search_type']));
             }
             if (array_key_exists('columns', $dataSource)) {
                 $columns = explode(',', $dataSource['columns']);
                 foreach ($columns as $currentColumn) {
                     $apiSearch->setResultColumns(trim($currentColumn));
                 }
             }
             if (array_key_exists('limit', $dataSource)) {
                 $apiSearch->setSearchLimit(0, (int) $dataSource['limit']);
             }
             /*
              * @todo Check if we can remove this
              */
             //              if (array_key_exists('filters', $dataSource)) {
             //
             //                  foreach ($dataSource['filters'] as $filter) {
             //                      $filter = $apiSearch->createFilter($filter['field'],
             //                          $filter['value'],
             //                          constant(isset($filter['match']) ? $filter['match'] : 'IcingaApiConstants::MATCH_LIKE')
             //                      );
             //
             //                        $apiSearch->setSearchFilter($filter);
             //                  }
             //              }
             //
             //              if (count($filters)) {
             //                  foreach ($filters as $f) {
             //                      if (!isset($f[2])) $f[2] = IcingaApiConstants::MATCH_EXACT;
             //                      $f = $this->processDsFiltermap($dataSource, $f);
             //                      $apiSearch->setSearchFilter($f[0], $f[1], $f[2]);
             //                  }
             //              }
             $add_filters = $this->getDsFilters($name, $filters, $ignore_defined_filters);
             foreach ($add_filters as $f) {
                 $apiSearch->setSearchFilter($f[0], $f[1], $f[2]);
             }
             /*
              * @todo Filters missing!
              */
             IcingaPrincipalTargetTool::applyApiSecurityPrincipals($apiSearch);
             $res = $apiSearch->fetch();
             $d = $res->getAll();
             if (is_array($d)) {
                 if ($res->getResultCount() > 0) {
                     if ($index !== false) {
                         if (isset($d[$index])) {
                             $d = $d[$index];
                         }
                     }
                     if (isset($dataSource['function'])) {
                         $this->dsRecursiveWalk($d, $dataSource['function']);
                     }
                 }
             }
             return $d;
         }
     }
 }
 private function setPrivileges(&$search)
 {
     IcingaPrincipalTargetTool::applyApiSecurityPrincipals($search);
 }
 /**
  * Executes query and return bulk struct
  * @return array
  */
 private function bulkQuery()
 {
     // We want only one specific type
     if ($this->type && array_key_exists($this->type, $this->mapping)) {
         $mappings = array($this->type);
     } else {
         $mappings = array_keys($this->mapping);
     }
     $data = array();
     $count = array();
     AppKitLogger::verbose("Performing bulk query (mappings = %s)", $mappings);
     foreach ($mappings as $mapping) {
         $md = $this->mapping[$mapping];
         $fields = $md['fields'];
         AppKitLogger::verbose("Performing bulk query (current mapping = %s)", $md);
         $search = $this->api->createSearch()->setSearchTarget($md['target'])->setResultColumns(array_values($md['fields']))->setResultType(IcingaApiConstants::RESULT_ARRAY)->setSearchLimit(0, AgaviConfig::get('modules.cronks.search.maximumResults', 200));
         $search_group = $search->createFilterGroup(IcingaApiConstants::SEARCH_OR);
         foreach ($md['search'] as $search_field) {
             $search_group->addFilter($search->createFilter($search_field, $this->query, IcingaApiConstants::MATCH_LIKE));
         }
         $search->setSearchFilter($search_group);
         // Limiting results for security
         IcingaPrincipalTargetTool::applyApiSecurityPrincipals($search);
         $result = $search->fetch();
         AppKitLogger::verbose("Query: %s ", $search->getSqlQuery());
         $count[$mapping] = $result->getResultCount();
         $data[$mapping] = $this->resultToArray($result, $fields, $mapping);
         AppKitLogger::verbose("Result: %s ", $data[$mapping]);
     }
     $new = $this->sortArray($data, $count);
     $sum = array_sum($count);
     AppKitLogger::verbose("Complete search result: %s ", $data);
     return array('resultCount' => array_sum($count), 'resultRows' => $new, 'resultSuccess' => $sum > 0 ? true : false);
 }
 public function executeRead(AgaviRequestDataHolder $rd)
 {
     if (!$this->checkAuth($rd)) {
         return "Error";
     }
     $context = $this->getContext();
     $API = $context->getModel("Icinga.ApiContainer", "Web");
     $target = $rd->getParameter("target");
     $search = @$API->createSearch()->setSearchTarget($target);
     $this->addFilters($search, $rd);
     $this->setColumns($search, $rd);
     $this->setGrouping($search, $rd);
     $this->setOrder($search, $rd);
     $this->setLimit($search, $rd);
     $search->setResultType(IcingaApiSearch::RESULT_ARRAY);
     // Adding security principal targets to the query
     /*
      * @todo Use new implementation (Doctrine Query)
      */
     IcingaPrincipalTargetTool::applyApiSecurityPrincipals($search);
     $res = $search->fetch()->getAll();
     //Setup count
     if ($rd->getParameter("countColumn")) {
         $search = @$API->createSearch()->setSearchTarget($target);
         $search->setSearchType(IcingaApiSearch::SEARCH_TYPE_COUNT);
         $this->addFilters($search, $rd);
         $this->setColumns($search, $rd);
         $search->setResultType(IcingaApiSearch::RESULT_ARRAY);
         /**
          * @todo Use new implementation (Doctrine Query)
          */
         IcingaPrincipalTargetTool::applyApiSecurityPrincipals($search);
         $rd->setParameter("searchCount", $search->fetch()->getAll());
     }
     $rd->setParameter("searchResult", $res);
     return $this->getDefaultViewName();
 }