Administrations pages for country and language edition (Closes #1320).
authorStéphane Jacob <sj@m4x.org>
Fri, 7 Jan 2011 06:36:18 +0000 (07:36 +0100)
committerStéphane Jacob <sj@m4x.org>
Fri, 7 Jan 2011 10:23:01 +0000 (11:23 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
modules/admin.php
templates/admin/geocoding.tpl
templates/admin/geocoding_edit.tpl [new file with mode: 0644]

index f007313..ef780f4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -1211,6 +1211,14 @@ class AdminModule extends PLModule
         return $warning;
     }
 
+    private static function updateCountry(array $item)
+    {
+        XDB::execute('UPDATE  geoloc_countries
+                         SET  countryPlain = {?}
+                       WHERE  iso_3166_1_a2 = {?}',
+                     mb_strtoupper(replace_accent($item['country'])), $item['iso_3166_1_a2']);
+    }
+
     private static function isLanguageIncomplete(array &$item)
     {
         if ($item['language'] == '') {
@@ -1220,7 +1228,9 @@ class AdminModule extends PLModule
         return false;
     }
 
-    function handler_geocoding(&$page, $action = null)
+    private static function updateLanguage(array $item) {}
+
+    function handler_geocoding(&$page, $category = null, $action = null, $id = null)
     {
         // Warning, this handler requires the following packages:
         //  * pkg-isocodes
@@ -1234,7 +1244,7 @@ class AdminModule extends PLModule
                 'id'           => 'iso_3166_1_a2',
                 'main_fields'  => array('iso_3166_1_a3', 'iso_3166_1_num', 'countryEn'),
                 'other_fields' => array('worldRegion', 'country', 'capital', 'nationality', 'nationalityEn',
-                                        'phonePrefix', 'phoneFormat', 'licensePlate', 'belongsTo', 'countryPlain')
+                                        'phonePrefix', 'phoneFormat', 'licensePlate', 'belongsTo')
             ),
             'language' => array(
                 'name'         => 'langages',
@@ -1246,16 +1256,94 @@ class AdminModule extends PLModule
 
             )
         );
-        //For  ISO  639,  the  first  three  columns are the ISO 639 2B code, the ISO 639 2T code and the ISO 639-1 code.  The third column may be
 
-        if (is_null($action) || !array_key_exists($action, $properties)) {
+        if (is_null($category) || !array_key_exists($category, $properties)) {
             pl_redirect('admin');
         }
 
-        $data = $properties[$action];
+        $data = $properties[$category];
+
+        if ($action == 'edit' || $action == 'add') {
+            $main_fields = array_merge(array($data['id']), $data['main_fields']);
+            $all_fields = array_merge($main_fields, $data['other_fields']);
+
+            if (is_null($id)) {
+                if (Post::has('new_id')) {
+                    $id = Post::v('new_id');
+                } else {
+                    pl_redirect('admin/geocoding/' . $category);
+                }
+            }
+
+            $list = array();
+            exec('isoquery --iso=' . $data['isocode'] . ' ' . $id, $list);
+            if (count($list) == 1) {
+                $array = explode("\t", $list[0]);
+                foreach ($main_fields as $i => $field) {
+                    $iso[$field] = $array[$i];
+                }
+            } else {
+                $iso = array();
+            }
+
+            if ($action == 'add') {
+                if (Post::has('new_id')) {
+                    S::assert_xsrf_token();
+                }
+
+                if (count($iso)) {
+                    $item = $iso;
+                } else {
+                    $item = array($data['id'] => $id);
+                }
+                XDB::execute('INSERT INTO  ' . $data['table'] . '(' . implode(', ', array_keys($item)) . ')
+                                   VALUES  ' . XDB::formatArray($item));
+                $page->trigSuccess($id . ' a bien été ajouté à la base.');
+            } elseif ($action == 'edit') {
+                if (Post::has('edit')) {
+                    S::assert_xsrf_token();
+
+                    $item = array();
+                    $set  = array();
+                    foreach ($all_fields as $field) {
+                        $item[$field] = Post::t($field);
+                        $set[] = $field . XDB::format(' = {?}', ($item[$field] ? $item[$field] : null));
+                    }
+                    XDB::execute('UPDATE  ' . $data['table'] . '
+                                     SET  ' . implode(', ', $set) . '
+                                   WHERE  ' . $data['id'] . ' = {?}',
+                                 $id);
+                    call_user_func_array(array('self', 'update' . ucfirst($category)), array($item));
+                    $page->trigSuccess($id . ' a bien été mis à jour.');
+                } elseif (Post::has('del')) {
+                    S::assert_xsrf_token();
+
+                    XDB::execute('DELETE FROM  ' . $data['table'] . '
+                                        WHERE  ' . $data['id'] . ' = {?}',
+                                 $id);
+                    $page->trigSuccessRedirect($id . ' a bien été supprimé.', 'admin/geocoding/' . $category);
+                } else {
+                    $item = XDB::fetchOneAssoc('SELECT  *
+                                                  FROM  ' . $data['table'] . '
+                                                 WHERE  ' . $data['id'] . ' = {?}',
+                                               $id);
+                }
+            }
+
+            $page->changeTpl('admin/geocoding_edit.tpl');
+            $page->setTitle('Administration - ' . ucfirst($data['name']));
+            $page->assign('category', $category);
+            $page->assign('name', $data['name']);
+            $page->assign('all_fields', $all_fields);
+            $page->assign('id', $id);
+            $page->assign('iso', $iso);
+            $page->assign('item', $item);
+            return;
+        }
 
         $page->changeTpl('admin/geocoding.tpl');
         $page->setTitle('Administration - ' . ucfirst($data['name']));
+        $page->assign('category', $category);
         $page->assign('name', $data['name']);
         $page->assign('id', $data['id']);
         $page->assign('main_fields', $data['main_fields']);
@@ -1308,7 +1396,7 @@ class AdminModule extends PLModule
                 }
                 if ($error == true) {
                     $erroneous[$id] = $item;
-                } elseif (call_user_func_array(array('self', 'is' . ucfirst($action) . 'Incomplete'), array(&$item))) {
+                } elseif (call_user_func_array(array('self', 'is' . ucfirst($category) . 'Incomplete'), array(&$item))) {
                     $incomplete[$id] = $item;
                 } else {
                     $remaining[$id] = $item;
index f3a5564..05c1db7 100644 (file)
 {foreach from=$lists item=list key=list_description}
 {if $list_description eq "manquant"}
 {assign var="fields" value=$main_fields}
+{assign var="action" value="add"}
 {else}
 {assign var="fields" value=$all_fields}
+{assign var="action" value="edit"}
 {/if}
 
 <h2>{$list|@count} {$name} {$list_description}{if $list|@count > 1}s{/if}.</h2>
@@ -37,6 +39,7 @@
     {foreach from=$fields item=field}
     <th>{$field}</th>
     {/foreach}
+    <th>{$action}</th>
   </tr>
 {foreach from=$list item=item key=key}
   <tr>
@@ -46,6 +49,7 @@
     {assign var="warning" value=$field|cat:'_warning'}
     <td{if t($item.$error)} class="error"{elseif t($item.$warning)} class="warning"{/if}>{$item.$field}</td>
     {/foreach}
+    <td><a href="admin/geocoding/{$category}/{$action}/{$key}">{icon name="page_edit"}</a></td>
   </tr>
 {/foreach}
   <tr>
     {foreach from=$fields item=field}
     <th>{$field}</th>
     {/foreach}
+    <th>{$action}</th>
   </tr>
 </table>
 {/if}
 {/foreach}
 
+<form method="post" action="admin/geocoding/{$category}/add">
+  {xsrf_token_field}
+  <p>
+    Ajouter un champ (n'indiquer que l'indentifiant principal)&nbsp;:
+    <input type="text" name="new_id" size="3" maxlength="3" />
+    <input type="submit" value="Ajouter" />
+  </p>
+</form>
+
 {* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
diff --git a/templates/admin/geocoding_edit.tpl b/templates/admin/geocoding_edit.tpl
new file mode 100644 (file)
index 0000000..0e49a8b
--- /dev/null
@@ -0,0 +1,55 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2010 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<h1><a href="admin/geocoding/{$category}">{$name|ucfirst}&nbsp;: édition</a></h1>
+
+<form method="post" action="admin/geocoding/{$category}/edit/{$id}">
+  {xsrf_token_field}
+
+  {if $iso|@count eq 0}
+  <p class="error">Cet élément ne figure pas dans la liste ISO.</p>
+  {/if}
+  <table class="tinybicol">
+    <tr>
+      <th>Champs</th>
+      <th>Élément</th>
+      <th>Données ISO</th>
+    </tr>
+    {foreach from=$all_fields item=field}
+    <tr>
+      <th>{$field}</th>
+      <td>
+        <input type="text" name="{$field}" value="{$item.$field}"
+               {if t($iso.$field)}{if !$item.$field} class="warning"{elseif $iso.$field neq $item.$field} class="error"{/if}{/if} />
+      </td>
+      <td>{if t($iso.$field)}{$iso.$field}{/if}</td>
+    </tr>
+    {/foreach}
+  </table>
+
+  <p class="center">
+    <input type="submit" name="edit" value="Éditer" />&nbsp;&nbsp;&nbsp;
+    <input type="submit" name="del" value="Supprimer" onclick="return confirm('Es-tu sûr de vouloir supprimer cet élément&nbsp;?')" />
+  </p>
+</form>
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}