فهرست منبع

updated filters in SystemObjects

Piotr Labudda 9 سال پیش
والد
کامیت
a042a0b0ae
1فایلهای تغییر یافته به همراه47 افزوده شده و 5 حذف شده
  1. 47 5
      SE/se-lib/Schema/SystemObjectStorageAcl.php

+ 47 - 5
SE/se-lib/Schema/SystemObjectStorageAcl.php

@@ -216,6 +216,7 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
 
   public function _parseWhere($params = []) {
     $sqlWhere = [];
+    DBG::log($params, 'array', "SystemObject::_parseWhere");
     if (!empty($params['#refFrom'])) {
       // '#refFrom' => [
       //   'namespace' => 'default_objects/SystemSource',
@@ -225,7 +226,32 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
       if (empty($params['#refFrom']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey");
 
       if ('default_objects/SystemSource' != $params['#refFrom']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['#refFrom']['namespace']}'");
-      $sqlWhere[] = "idDatabase = " . DB::getPDO()->quote($params['#refFrom']['primaryKey'], PDO::PARAM_INT);
+      $sqlWhere[] = "t.idDatabase = " . DB::getPDO()->quote($params['#refFrom']['primaryKey'], PDO::PARAM_INT);
+    }
+    {
+      $filterParams = [];
+      $xsdFields = $this->getXsdTypes();
+      foreach ($params as $k => $v) {
+        if ('f_' != substr($k, 0, 2)) continue;
+        $fieldName = substr($k, 2);
+        if (!array_key_exists($fieldName, $xsdFields)) {
+          // TODO: check query by xpath or use different param prefix
+          throw new Exception("Field '{$fieldName}' not found in '{$this->_namespace}'");
+        }
+        $filterParams[$fieldName] = $v;
+      }
+    }
+    if (!empty($filterParams)) {
+      DBG::log($filterParams, 'array', "SystemObject::_parseWhere TODO \$filterParams");
+      foreach ($filterParams as $fieldName => $value) {
+        if (is_array($value)) {
+          DBG::log($value, 'array', "TODO SystemObject::_parseWhere array value for \$filterParams[{$fieldName}]");
+        } else if (is_scalar($value)) {
+          $sqlWhere[] = "t.{$fieldName} like " . DB::getPDO()->quote("%{$value}%", PDO::PARAM_STR);
+        } else {
+          DBG::log($value, 'array', "BUG SystemObject::_parseWhere unknown type for \$filterParams[{$fieldName}]");
+        }
+      }
     }
     return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : '';
   }
@@ -241,17 +267,33 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
 
   public function getItems($params = []) {
     $sqlWhere = $this->_parseWhere($params);
+
+    $currSortCol = V::get('order_by', 'idZasob', $params);
+    $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
+    // TODO: validate $currSortCol is in field list
+    // TODO: validate $currSortFlip ('asc' or 'desc')
+    $xsdFields = $this->getXsdTypes();
+    if (!array_key_exists($currSortCol, $xsdFields)) throw new Exception("Field '{$currSortCol}' not found in '{$this->_namespace}'");
+    if (!in_array($currSortFlip, ['asc', 'desc'])) throw new Exception("Sort dir not allowed");
+    $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
+
+    $limit = V::get('limit', 0, $params, 'int');
+    $limit = ($limit < 0) ? 0 : $limit;
+    $offset = V::get('limitstart', 0, $params, 'int');
+    $offset = ($offset < 0) ? 0 : $offset;
+    $sqlLimit = ($limit > 0)
+      ? "limit {$limit} offset {$offset}"
+      : '';
+
     return DB::getPDO()->fetchAll("
       select t.*
       from `{$this->_rootTableName}` t
       {$sqlWhere}
+      {$sqlOrderBy}
+      {$sqlLimit}
     ");
 
     $items = $this->_getAllItems();
-    $currSortCol = V::get('order_by', 'ID', $params);
-    $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
-    // TODO: validate $currSortCol is in field list
-    // TODO: validate $currSortFlip ('asc' or 'desc')
 
     $aliasMap = array();
     foreach ($this->_simpleSchema['root'] as $key => $field) {