Displays and compares our country and language lists with iso lists.
authorStéphane Jacob <sj@m4x.org>
Wed, 5 Jan 2011 15:13:36 +0000 (16:13 +0100)
committerStéphane Jacob <sj@m4x.org>
Wed, 5 Jan 2011 17:55:38 +0000 (18:55 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
modules/admin.php
templates/admin/geocoding.tpl [new file with mode: 0644]
templates/admin/index.tpl

index 26c0475..f007313 100644 (file)
@@ -47,6 +47,7 @@ class AdminModule extends PLModule
             'admin/wiki'                   => $this->make_hook('wiki',                   AUTH_MDP, 'admin'),
             'admin/ipwatch'                => $this->make_hook('ipwatch',                AUTH_MDP, 'admin'),
             'admin/icons'                  => $this->make_hook('icons',                  AUTH_MDP, 'admin'),
+            'admin/geocoding'              => $this->make_hook('geocoding',              AUTH_MDP, 'admin'),
             'admin/accounts'               => $this->make_hook('accounts',               AUTH_MDP, 'admin'),
             'admin/account/watch'          => $this->make_hook('account_watch',          AUTH_MDP, 'admin'),
             'admin/account/types'          => $this->make_hook('account_types',          AUTH_MDP, 'admin'),
@@ -1190,6 +1191,140 @@ class AdminModule extends PLModule
         $table_editor->apply($page, $action, $id);
     }
 
+    private static function isCountryIncomplete(array &$item)
+    {
+        $warning = false;
+        foreach (array('worldRegion', 'country', 'capital', 'phonePrefix', 'licensePlate', 'countryPlain') as $field) {
+            if ($item[$field] == '') {
+                $item[$field . '_warning'] = true;
+                $warning = true;
+            }
+        }
+        if (is_null($item['belongsTo'])) {
+            foreach (array('nationality', 'nationalityEn') as $field) {
+                if ($item[$field] == '') {
+                    $item[$field . '_warning'] = true;
+                    $warning = true;
+                }
+            }
+        }
+        return $warning;
+    }
+
+    private static function isLanguageIncomplete(array &$item)
+    {
+        if ($item['language'] == '') {
+            $item['language_warning'] = true;
+            return true;
+        }
+        return false;
+    }
+
+    function handler_geocoding(&$page, $action = null)
+    {
+        // Warning, this handler requires the following packages:
+        //  * pkg-isocodes
+        //  * isoquery
+
+        static $properties = array(
+            'country'  => array(
+                'name'         => 'pays',
+                'isocode'      => '3166',
+                'table'        => 'geoloc_countries',
+                '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')
+            ),
+            'language' => array(
+                'name'         => 'langages',
+                'isocode'      => '639',
+                'table'        => 'profile_langskill_enum',
+                'id'           => 'iso_639_2b',
+                'main_fields'  => array('iso_639_2t', 'iso_639_1', 'language_en'),
+                'other_fields' => array('language')
+
+            )
+        );
+        //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)) {
+            pl_redirect('admin');
+        }
+
+        $data = $properties[$action];
+
+        $page->changeTpl('admin/geocoding.tpl');
+        $page->setTitle('Administration - ' . ucfirst($data['name']));
+        $page->assign('name', $data['name']);
+        $page->assign('id', $data['id']);
+        $page->assign('main_fields', $data['main_fields']);
+        $page->assign('all_fields', array_merge($data['main_fields'], $data['other_fields']));
+
+        // First build the list provided by the iso codes.
+        $list = array();
+        exec('isoquery --iso=' . $data['isocode'], $list);
+
+        foreach ($list as $key => $item) {
+            $array = explode("\t", $item);
+            unset($list[$key]);
+            $list[$array[0]] = array();
+            foreach ($data['main_fields'] as $i => $field) {
+                $list[$array[0]][$field] = $array[$i + 1];
+            }
+        }
+        ksort($list);
+
+        // Retrieve all data from the database.
+        $db_list = XDB::rawFetchAllAssoc('SELECT  *
+                                            FROM  ' . $data['table'] . '
+                                        ORDER BY  ' . $data['id'],
+                                         $data['id']);
+
+        // Sort both iso and database data into 5 categories:
+        //  $missing: data from the iso list not in the database,
+        //  $non_existing: data from the database not in the iso list,
+        //  $erroneous: data that differ on main fields,
+        //  $incomplete: data with empty fields in the data base,
+        //  $remaining: remaining correct and complete data from the database.
+
+        $missing = $non_existing = $erroneous = $incomplete = $remaining = array();
+        foreach (array_keys($list) as $id) {
+            if (!array_key_exists($id, $db_list)) {
+                $missing[$id] = $list[$id];
+            }
+        }
+
+        foreach ($db_list as $id => $item) {
+            if (!array_key_exists($id, $list)) {
+                $non_existing[$id] = $item;
+            } else {
+                $error = false;
+                foreach ($data['main_fields'] as $field) {
+                    if ($item[$field] != $list[$id][$field]) {
+                        $item[$field . '_error'] = true;
+                        $error = true;
+                    }
+                }
+                if ($error == true) {
+                    $erroneous[$id] = $item;
+                } elseif (call_user_func_array(array('self', 'is' . ucfirst($action) . 'Incomplete'), array(&$item))) {
+                    $incomplete[$id] = $item;
+                } else {
+                    $remaining[$id] = $item;
+                }
+            }
+        }
+
+        $page->assign('lists', array(
+                'manquant'  => $missing,
+                'disparu'   => $non_existing,
+                'erroné'    => $erroneous,
+                'incomplet' => $incomplete,
+                'restant'   => $remaining
+        ));
+    }
+
     function handler_accounts(PlPage $page)
     {
         $page->changeTpl('admin/accounts.tpl');
diff --git a/templates/admin/geocoding.tpl b/templates/admin/geocoding.tpl
new file mode 100644 (file)
index 0000000..f3a5564
--- /dev/null
@@ -0,0 +1,61 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  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>{$name|ucfirst}</h1>
+
+{foreach from=$lists item=list key=list_description}
+{if $list_description eq "manquant"}
+{assign var="fields" value=$main_fields}
+{else}
+{assign var="fields" value=$all_fields}
+{/if}
+
+<h2>{$list|@count} {$name} {$list_description}{if $list|@count > 1}s{/if}.</h2>
+{if $list|@count}
+<table class="bicol">
+  <tr>
+    <th>{$id}</th>
+    {foreach from=$fields item=field}
+    <th>{$field}</th>
+    {/foreach}
+  </tr>
+{foreach from=$list item=item key=key}
+  <tr>
+    <td>{$key}</td>
+    {foreach from=$fields item=field}
+    {assign var="error" value=$field|cat:'_error'}
+    {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}
+  </tr>
+{/foreach}
+  <tr>
+    <th>{$id}</th>
+    {foreach from=$fields item=field}
+    <th>{$field}</th>
+    {/foreach}
+  </tr>
+</table>
+{/if}
+{/foreach}
+
+{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
index af6d8b1..17cd7eb 100644 (file)
 
   <tr><th colspan="2">{icon name=user_gray} Champs</th></tr>
   <tr class="impair">
+    <td class="titre">Pays / Langues</td>
+    <td>
+      <a href="admin/geocoding/country">Pays</a>
+      &nbsp;&nbsp;|&nbsp;&nbsp;
+      <a href="admin/geocoding/language">Langues</a>
+    </td>
+  </tr>
+  <tr class="impair">
     <td class="titre">Formation</td>
     <td>
       <a href="admin/education">Formations</a>