From f407f9cb28825f64ca513c072f813150cf68d936 Mon Sep 17 00:00:00 2001
From: Laurent Giovannoni <laurent.giovannoni@maarch.org>
Date: Tue, 14 Mar 2017 14:28:22 +0100
Subject: [PATCH] FEAT new method to insert res and resExt

---
 .../Models/ContactsModelAbstract.php          |  2 +-
 core/Controllers/ResController.php            | 52 +++++--------------
 core/Models/ResModelAbstract.php              | 32 ++++++++++++
 3 files changed, 46 insertions(+), 40 deletions(-)

diff --git a/apps/maarch_entreprise/Models/ContactsModelAbstract.php b/apps/maarch_entreprise/Models/ContactsModelAbstract.php
index 336e0c19be8..10f887bdd29 100644
--- a/apps/maarch_entreprise/Models/ContactsModelAbstract.php
+++ b/apps/maarch_entreprise/Models/ContactsModelAbstract.php
@@ -76,7 +76,7 @@ class ContactsModelAbstract extends Apps_Table_Service
             'table'     => ['view_contacts'],
             'where'     => ['email = ? and enabled = ?'],
             'data'      => [$aArgs['email'], 'Y'],
-            'order'     => ['creation_date'],
+            'order_by'     => ['creation_date'],
         ]);
 
         return $aReturn;
diff --git a/core/Controllers/ResController.php b/core/Controllers/ResController.php
index 3705692c8b5..f807dd3b8f9 100644
--- a/core/Controllers/ResController.php
+++ b/core/Controllers/ResController.php
@@ -154,22 +154,18 @@ class ResController
                 'data'          => $data,
             ];
 
-            $resId = $this->loadIntoDb($aArgs);
-            var_dump($resId);
-            exit;
+            $this->loadIntoDb($aArgs);
+
+            $resDetails = ResModel::getByPath([
+                'docserverId' => $storeResult['docserver_id'],
+                'path'        => $storeResult['destination_dir'],
+                'filename'    => $storeResult['file_destination_name'],
+                'table'       => $aArgs['table'],
+                'select'      => ['res_id']
+            ]);
+            
+            $resId = $resDetails[0]['res_id'];
             
-            require_once 'core/class/class_resource.php';
-            $resource = new \resource();
-            $resId = $resource->load_into_db(
-                $table,
-                $storeResult['destination_dir'],
-                $storeResult['file_destination_name'],
-                $storeResult['path_template'],
-                $storeResult['docserver_id'],
-                $data,
-                $_SESSION['config']['databasetype'],
-                true
-            );
 
             if (!is_numeric($resId)) {
                 return ['errors' => 'Pb with SQL insertion : ' .$resId];
@@ -193,7 +189,7 @@ class ResController
     * @param  $filename string Resource file name
     * @param  $docserverPath  string Docserver path
     * @param  $docserverId  string Docserver identifier
-    * @param  $data  array Data array
+    * @param  boolean
     */
     public function loadIntoDb($aArgs)
     {
@@ -302,35 +298,13 @@ class ResController
                 //VALUE
                 $prepareData[$data[$i]['column']] = $data[$i]['value'];
             }
-            var_dump($prepareData);
 
             $resInsert = ResModel::create([
                 'table' => 'res_letterbox',
                 'data'  => $prepareData
             ]);
-            var_dump($resInsert);
-
-            exit;
 
-            if (!$this->insert($table, $data, $_SESSION['config']['databasetype'])) {
-                if (!$calledByWs) {
-                    $this->error = _INDEXING_INSERT_ERROR."<br/>".$this->show();
-                }
-                return false;
-            } else {
-                $db2 = new Database();
-                $stmt = $db2->query(
-                    "select res_id from " . $table
-                        . " where docserver_id = ? and path = ? and filename= ?  order by res_id desc ",
-                    array(
-                        $docserverId,
-                        $path,
-                        $filename
-                    )
-                );
-                $res = $stmt->fetchObject();
-                return $res->res_id;
-            }
+            return true;
         }
     }
 
diff --git a/core/Models/ResModelAbstract.php b/core/Models/ResModelAbstract.php
index 27c910f2103..ad58c36da30 100644
--- a/core/Models/ResModelAbstract.php
+++ b/core/Models/ResModelAbstract.php
@@ -47,6 +47,38 @@ class ResModelAbstract extends \Apps_Table_Service
         return $aReturn;
     }
 
+    /**
+     * Retrieve info of resId by path
+     * @param  $docserverId string
+     * @param  $path string
+     * @param  $filename string
+     * @param  $table string
+     * @param  $select string
+     * @return array $res
+     */
+    public static function getByPath(array $aArgs = [])
+    {
+        static::checkRequired($aArgs, ['docserverId']);
+        static::checkRequired($aArgs, ['path']);
+        static::checkRequired($aArgs, ['filename']);
+
+        if (!empty($aArgs['table'])) {
+            $table = $aArgs['table'];
+        } else {
+            $table = 'res_letterbox';
+        }
+
+        $aReturn = static::select([
+            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'     => [$table],
+            'where'     => ['docserver_id = ? and path = ? and filename = ?'],
+            'data'      => [$aArgs['docserverId'], $aArgs['path'], $aArgs['filename']],
+            'order_by'  => ['res_id desc'],
+        ]);
+
+        return $aReturn;
+    }
+
     /**
      * Retrieve process_limit_date for resource in extension table if mlb
      * @param  $resId integer
-- 
GitLab