Browse code

Refactor findPublishedBy asset model method to accept numeric ids and alias names

Benjamin Roth authored on28/02/2024 13:32:48
Showing2 changed files
... ...
@@ -54,7 +54,7 @@ class RealEstateAssetsReaderController extends RealEstateAssetsModuleController
54 54
             Input::setGet('items', Input::get('auto_item'));
55 55
         }
56 56
 
57
-        $this->asset = RealEstateAssetsModel::findPublishedById(Input::get('items'));
57
+        $this->asset = RealEstateAssetsModel::findPublishedByIdOrAlias(Input::get('items'));
58 58
 
59 59
         if ($this->asset === null)
60 60
         {
... ...
@@ -138,15 +138,15 @@ class RealEstateAssetsModel extends Model
138 138
   /**
139 139
    * Find a published assets from one or more categories
140 140
    *
141
-   * @param int     $intId      The id of
142
-   * @param array   $arrOptions An optional options array
141
+   * @param mixed $varId      The numeric id or alias name
142
+   * @param array $arrOptions An optional options array
143 143
    *
144 144
    * @return RealEstateAssetsModel|null A model or null if there is no asset
145 145
    */
146
-  public static function findPublishedById(int $intId, array $arrOptions=array())
146
+  public static function findPublishedByIdOrAlias($varId, array $arrOptions=array())
147 147
   {
148 148
     $t = static::$strTable;
149
-    $arrColumns = array("$t.id=?");
149
+    $arrColumns = !preg_match('/^[1-9]\d*$/', $varId) ? array("BINARY $t.alias=?") : array("$t.id=?");
150 150
 
151 151
     if (!static::isPreviewMode($arrOptions))
152 152
     {
... ...
@@ -154,7 +154,7 @@ class RealEstateAssetsModel extends Model
154 154
       $arrColumns[] = "$t.published='1' AND ($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'$time')";
155 155
     }
156 156
 
157
-    return static::findOneBy($arrColumns, $intId, $arrOptions);
157
+    return static::findOneBy($arrColumns, $varId, $arrOptions);
158 158
   }
159 159
 
160 160
 	/**