Browse code

Add alias to assets

Benjamin Roth authored on01/10/2024 14:01:30
Showing3 changed files
... ...
@@ -99,7 +99,7 @@ $GLOBALS['TL_DCA']['tl_vr_real_estate_assets'] = array
99 99
     'palettes'    => array
100 100
     (
101 101
         '__selector__' => array('availability','managedPropertyId'),
102
-        'default'      => '{title_legend},title,assetNo,floor,managedPropertyId;{availability_legend},availability;{misc_legend},description;{basic_data_legend},livingSpace,numberOfRooms,rent,coldRent,advanceOperatingCosts,constructionYear,deposit,entitlementCertificate;{features_legend},features,parking,heatingType,energyPassType,energyConsumption;{images_legend},gallerySRC,floorPlansSRC;{publish_legend},published,start,stop',
102
+        'default'      => '{title_legend},title,alias,assetNo,floor,managedPropertyId;{availability_legend},availability;{misc_legend},description;{basic_data_legend},livingSpace,numberOfRooms,rent,coldRent,advanceOperatingCosts,constructionYear,deposit,entitlementCertificate;{features_legend},features,parking,heatingType,energyPassType,energyConsumption;{images_legend},gallerySRC,floorPlansSRC;{publish_legend},published,start,stop',
103 103
     ),
104 104
 
105 105
     // Subpalettes
... ...
@@ -135,6 +135,13 @@ $GLOBALS['TL_DCA']['tl_vr_real_estate_assets'] = array
135 135
             'eval'      => array('mandatory' => true, 'maxlength' => 255, 'tl_class' => 'w50'),
136 136
             'sql'       => "varchar(255) NOT NULL default ''"
137 137
         ),
138
+        'alias' => array
139
+        (
140
+            'search'                  => true,
141
+            'inputType'               => 'text',
142
+            'eval'                    => array('rgxp'=>'alias', 'doNotCopy'=>true, 'unique'=>true, 'maxlength'=>255, 'tl_class'=>'w50'),
143
+            'sql'                     => "varchar(255) BINARY NOT NULL default ''"
144
+        ),
138 145
         'postal'                => array
139 146
         (
140 147
             'exclude'   => true,
... ...
@@ -95,7 +95,7 @@ class RealEstateAssetsListController extends RealEstateAssetsModuleController
95 95
 
96 96
         foreach ($assets as $asset)
97 97
         {
98
-            $strUrl = $jumpTo !== null ? $urlGenerator->generate($jumpTo, ['parameters'=>'/items/'.$asset->id]) : null;
98
+            $strUrl = $jumpTo !== null ? $urlGenerator->generate($jumpTo, ['parameters'=>$asset->alias]) : null;
99 99
             $figureBuilder->setLinkHref($strUrl);
100 100
             $arrItem = array_merge($asset->row(), [
101 101
                 'teaserFigure' => $this->getImageFigures($asset->gallerySRC, $figureBuilder, $asset->orderSRC, 1),
... ...
@@ -11,9 +11,13 @@
11 11
 namespace vonRotenberg\RealEstateListingBundle\EventListener;
12 12
 
13 13
 use Contao\CoreBundle\ServiceAnnotation\Callback;
14
+use Contao\Database;
14 15
 use Contao\DataContainer;
16
+use Contao\System;
15 17
 use Doctrine\DBAL\Connection;
18
+use Exception;
16 19
 use vonRotenberg\RealEstateListingBundle\Model\ManagedPropertyModel;
20
+use vonRotenberg\RealEstateListingBundle\Model\RealEstateCategoriesModel;
17 21
 
18 22
 class RealEstateAssetsContainerListener
19 23
 {
... ...
@@ -52,4 +56,34 @@ class RealEstateAssetsContainerListener
52 56
 
53 57
         $this->db->update('tl_vr_real_estate_assets', ['geox' => $Property->geox, 'geoy' => $Property->geoy],['id' => $dc->id]);
54 58
     }
59
+
60
+    /**
61
+     * @Callback(table="tl_vr_real_estate_assets", target="fields.alias.save")
62
+     */
63
+    public function generateAlias($varValue, DataContainer $dc)
64
+    {
65
+        $aliasExists = static function (string $alias) use ($dc): bool {
66
+            $result = Database::getInstance()
67
+                ->prepare("SELECT id FROM tl_vr_real_estate_assets WHERE alias=? AND id!=?")
68
+                ->execute($alias, $dc->id);
69
+
70
+            return $result->numRows > 0;
71
+        };
72
+
73
+        // Generate alias if there is none
74
+        if (!$varValue)
75
+        {
76
+            $varValue = System::getContainer()->get('contao.slug')->generate($dc->activeRecord->title, [], $aliasExists);
77
+        }
78
+        elseif (preg_match('/^[1-9]\d*$/', $varValue))
79
+        {
80
+            throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $varValue));
81
+        }
82
+        elseif ($aliasExists($varValue))
83
+        {
84
+            throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
85
+        }
86
+
87
+        return $varValue;
88
+    }
55 89
 }