Insert or update profile_manageurs when the user validates.
authorThomas Minvielle <thomas.minvielle@polytechnique.org>
Sun, 18 May 2014 07:52:14 +0000 (09:52 +0200)
committerThomas Minvielle <thomas.minvielle@polytechnique.org>
Sun, 18 May 2014 07:52:14 +0000 (09:52 +0200)
modules/profile/manageurs.inc.php
templates/profile/manageurs.tpl

index 5faaa64..428e8f2 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-class ProfileSettingManageurs implements ProfileSetting
+class ProfileSettingManageursYear implements ProfileSetting
 {
     public function value(ProfilePage $page, $field, $value, &$success)
     {
         $success = true;
+        if (is_null($value)) {
+            $value = XDB::fetchOneCell("SELECT m.entry_year
+                                          FROM profile_manageurs AS m
+                                         WHERE pid = {?}",
+                                        $page->pid());
+        } else {
+            if (($value < 1921) || ($value > (date('Y') + 4))) {
+                    Platal::page()->trigWarning('L\'année de début d\'activité professionnelle est mal ou non renseignée, elle doit être du type : 2004.');
+                    $success = (is_null($value) || ($value == '')); // Even if it triggers a warning, the null value is valid.
+                    $value = null;
+            }
+        }
+        
+        return $value;
+    }
+
+    public function save(ProfilePage $page, $field, $value)
+    {
+        XDB::execute("INSERT INTO profile_manageurs (pid, title, entry_year, project, anonymity, visibility, email, communication, push, network)
+                           VALUES ({?},{?},{?},{?},{?},{?},{?},{?},{?},{?})
+          ON DUPLICATE KEY UPDATE entry_year = VALUES(entry_year)",
+                     $page->pid(), '', $value, '', 0, 'blocked', '', '', 'never', 0);
+    }
+
+    public function getText($value) {
+    }
+}
+
+// Handles title, project and email fields
+class ProfileSettingManageursText implements ProfileSetting
+{
+    private $dbfield;
+
+    public function __construct($dbfield)
+    {
+        $this->dbfield = $dbfield;
+    }
+
+    public function value(ProfilePage $page, $field, $value, &$success)
+    {
+        if (is_null($value)) {
+            $value = XDB::fetchOneCell("SELECT m." . $this->dbfield .
+                                        " FROM profile_manageurs AS m
+                                         WHERE m.pid = {?}",
+                                       $page->pid());
+        }
+        else if ($this->dbfield == 'email') {
+            $success = (($value == '') || isvalid_email($value));
+            if (!isvalid_email($value)) {
+                Platal::page()->trigWarning('Le champ email n\'est pas correctement renseigné.');
+            }
+        } else {
+            $success = true;
+        }
+        return $value;
+    }
+
+    public function save(ProfilePage $page, $field, $value)
+    {
+        $default = array('title' => '', 'project' => '', 'email' => '');
+        $default[$this->dbfield] = $value;
+        XDB::execute("INSERT INTO profile_manageurs (pid, title, entry_year, project, anonymity, visibility, email, communication, push, network)
+                           VALUES ({?},{?},{?},{?},{?},{?},{?},{?},{?},{?})
+          ON DUPLICATE KEY UPDATE " . $this-> dbfield . " = VALUES(" . $this->dbfield .")",
+                     $page->pid(), $default['title'], null, $default['project'], 0, 'blocked', $default['email'], '', 'never', 0);
+    }
+
+    public function getText($value) {
+        return $value;
+    }
+}
+
+class ProfileSettingManageursVisibility implements ProfileSetting
+{
+    public function value(ProfilePage $page, $field, $value, &$success)
+    {
+        if (is_null($value)) {
+            $value = XDB::fetchOneCell("SELECT m.visibility
+                                          FROM profile_manageurs AS m
+                                         WHERE m.pid = {?}",
+                                       $page->pid());
+            if (is_null($value)) {
+                $value = 'blocked';
+            }
+        }
+        $success = in_array($value, array('visible', 'visible_exceptions', 'blocked'));
         return $value;
     }
 
     public function save(ProfilePage $page, $field, $value)
     {
+        XDB::execute("INSERT INTO profile_manageurs (pid, title, entry_year, project, anonymity, visibility, email, communication, push, network)
+                           VALUES ({?},{?},{?},{?},{?},{?},{?},{?},{?},{?})
+          ON DUPLICATE KEY UPDATE visibility = VALUES(visibility)",
+                     $page->pid(), '', null, '', 0, $value, '', '', 'never', 0);
     }
 
     public function getText($value) {
+        return $value;
     }
 }
 
@@ -39,40 +130,25 @@ class ProfileSettingManageursPush implements ProfileSetting
 {
     public function value(ProfilePage $page, $field, $value, &$success)
     {
-        $success = true;
         if (is_null($value)) {
             $value = XDB::fetchOneCell("SELECT m.push
                                           FROM profile_manageurs AS m
                                          WHERE m.pid = {?}",
                                        $page->pid());
-        }
-        else {
-            switch ($value) {
-            case 'unique':
-                break;
-            case 'weekly':
-                break;
-            case 'never':
-                break;
-            case 0:
-                $value = 'unique';
-                break;
-            case 1:
-                $value = 'weekly';
-                break;
-            default:
+            if (is_null($value)) {
                 $value = 'never';
             }
         }
+        $success = in_array($value, array('unique', 'weekly', 'never'));
         return $value;
     }
 
     public function save(ProfilePage $page, $field, $value)
     {
-        XDB::execute("UPDATE profile_manageurs AS m
-                         SET m.push = {?}
-                       WHERE m.pid = {?}",
-                     $value, $page->pid());
+        XDB::execute("INSERT INTO profile_manageurs (pid, title, entry_year, project, anonymity, visibility, email, communication, push, network)
+                           VALUES ({?},{?},{?},{?},{?},{?},{?},{?},{?},{?})
+          ON DUPLICATE KEY UPDATE push = VALUES(push)",
+                     $page->pid(), '', null, '', 0, 'blocked', '', '', $value, 0);
     }
 
     public function getText($value) {
@@ -91,15 +167,14 @@ class ProfileSettingManageursCommunication implements ProfileSetting
 
     public function value(ProfilePage $page, $field, $value, &$success)
     {
-        $success = true;
         if (is_null($value)) {
             $value = XDB::fetchOneCell("SELECT FIND_IN_SET({?}, m.communication)
                                           FROM profile_manageurs AS m
                                          WHERE m.pid = {?}",
                                        $this->communication_field, $page->pid());
         }
-
         $value = (bool) $value;
+        $success = true;
         return $value;
     }
 
@@ -125,10 +200,10 @@ class ProfileSettingManageursCommunication implements ProfileSetting
 
       if ($modified) {
           $res = implode(',', $res);
-          XDB::execute("UPDATE profile_manageurs AS m
-                           SET m.communication = {?}
-                         WHERE m.pid = {?}",
-                       $res, $page->pid());
+          XDB::execute("INSERT INTO profile_manageurs (pid, title, entry_year, project, anonymity, visibility, email, communication, push, network)
+                             VALUES ({?},{?},{?},{?},{?},{?},{?},{?},{?},{?})
+            ON DUPLICATE KEY UPDATE communication = VALUES(communication)",
+                        $page->pid(), '', null, '', 0, 'blocked', '', $res, 'never', 0);
       }
     }
 
@@ -142,6 +217,7 @@ class ProfileSettingManageursCommunication implements ProfileSetting
     }
 }
 
+// Handles anonymity and network fields
 class ProfileSettingManageursBool implements ProfileSetting
 {
     private $dbfield;
@@ -152,7 +228,6 @@ class ProfileSettingManageursBool implements ProfileSetting
     }
     public function value(ProfilePage $page, $field, $value, &$success)
     {
-        $success = true;
         if (is_null($value)) {
             $value = XDB::fetchOneCell("SELECT m." . $this->dbfield .
                                         " FROM profile_manageurs AS m
@@ -162,23 +237,18 @@ class ProfileSettingManageursBool implements ProfileSetting
         }
 
         $value = (bool) $value;
+        $success = true;
         return $value;
     }
 
     public function save(ProfilePage $page, $field, $value)
     {
-        if ($value) {
-          XDB::execute("UPDATE profile_manageurs AS m
-                           SET m." . $this->dbfield . " = 1
-                         WHERE m.pid = {?}",
-                       $page->pid());
-        }
-        else {
-          XDB::execute("UPDATE profile_manageurs AS m
-                           SET m." . $this->dbfield . " = 0
-                         WHERE m.pid = {?}",
-                       $page->pid());
-        }
+        $default = array('anonymity' => 0, 'network' => 0);
+        $default[$this->dbfield] = (($value) ? 1 : 0);
+        XDB::execute("INSERT INTO profile_manageurs (pid, title, entry_year, project, anonymity, visibility, email, communication, push, network)
+                           VALUES ({?},{?},{?},{?},{?},{?},{?},{?},{?},{?})
+          ON DUPLICATE KEY UPDATE " . $this->dbfield . " = VALUES(" . $this->dbfield . ")",
+                     $page->pid(), '', null, '', $default['anonymity'], 'blocked', '', '', 'never', $default['network']);
     }
 
     public function getText($value) {
@@ -198,11 +268,11 @@ class ProfilePageManageurs extends ProfilePage
     public function __construct(PlWizard $wiz)
     {
         parent::__construct($wiz);
-        $this->settings['manageurs_title'] = null;
-        $this->settings['manageurs_entry_year'] = null;
-        $this->settings['manageurs_project'] = null;
-        $this->settings['manageurs_visibility'] = null;
-        $this->settings['manageurs_email'] = null;
+        $this->settings['manageurs_title'] = new ProfileSettingManageursText('title');
+        $this->settings['manageurs_entry_year'] = new ProfileSettingManageursYear();
+        $this->settings['manageurs_project'] = new ProfileSettingManageursText('project');
+        $this->settings['manageurs_visibility'] = new ProfileSettingManageursVisibility();
+        $this->settings['manageurs_email'] = new ProfileSettingManageursText('email');
         $this->settings['manageurs_push'] = new ProfileSettingManageursPush();
         $this->settings['manageurs_anonymity'] = new ProfileSettingManageursBool('anonymity');
         $this->settings['manageurs_novelty'] = new ProfileSettingManageursCommunication('novelty');
@@ -213,50 +283,10 @@ class ProfilePageManageurs extends ProfilePage
 
     protected function _fetchData()
     {
-        $res = XDB::query("SELECT  title AS manageurs_title, entry_year AS manageurs_entry_year, project AS manageurs_project,
-                                   visibility AS manageurs_visibility, email AS manageurs_email
-                             FROM  profile_manageurs
-                            WHERE  pid = {?}",
-                          $this->pid());
-        $this->values = $res->fetchOneAssoc();
     }
 
     protected function _saveData()
     {
-        if ($this->changed['manageurs_title']) {
-          XDB::execute('UPDATE profile_manageurs
-                           SET title = {?}
-                         WHERE pid = {?}',
-                        $this->values['manageurs_title'], $this->pid());
-        }
-
-        if ($this->changed['manageurs_entry_year']) {
-          XDB::execute('UPDATE profile_manageurs
-                           SET entry_year = {?}
-                         WHERE pid = {?}',
-                        $this->values['manageurs_entry_year'], $this->pid());
-        }
-
-        if ($this->changed['manageurs_project']) {
-          XDB::execute('UPDATE profile_manageurs
-                           SET project = {?}
-                         WHERE pid = {?}',
-                        $this->values['manageurs_project'], $this->pid());
-        }
-
-        if ($this->changed['manageurs_visibility']) {
-          XDB::execute('UPDATE profile_manageurs
-                           SET visibility = {?}
-                         WHERE pid = {?}',
-                        $this->values['manageurs_visibility'], $this->pid());
-        }
-
-        if ($this->changed['manageurs_email']) {
-          XDB::execute('UPDATE profile_manageurs
-                           SET email = {?}
-                         WHERE pid = {?}',
-                        $this->values['manageurs_email'], $this->pid());
-        }
     }
 
     public function _prepare(PlPage $page, $id)
index 090fed9..5018ef9 100644 (file)
@@ -37,7 +37,7 @@
       <span class="titre">Intitulé du profil&nbsp;:</span>
     </td>
     <td>
-      <input type="text" name="manageurs_title" value="{$manageurs_title}" size="49" />
+      <input type="text" name="manageurs_title" value="{$manageurs_title}" size="49" maxlength="250" />
     </td>
   </tr>
   <tr>
     <td>
       <div>
         <label>
-          <input type="radio" name="manageurs_visibility" value="1"
-            {if $manageurs_visibility="visible"}checked="checked"{/if} />
+          <input type="radio" name="manageurs_visibility" value="visible"
+            {if $manageurs_visibility == "visible"}checked="checked"{/if} />
           Toutes les entreprises peuvent voir mon profil et me contacter.
         </label>
         <br />
         <!-- TODO: implement a list of firms that are blacklisted
         <label>
-          <input type="radio" name="manageurs_visibility" value="1"
-            {if $manageurs_visibility="visible_exceptions"}checked="checked"{/if} />
+          <input type="radio" name="manageurs_visibility" value="visible_exceptions"
+            {if $manageurs_visibility == "visible_exceptions"}checked="checked"{/if} />
           Les entreprises peuvent voir mon profil à l'exception de :
         </label>
         <br />
         -->
         <label>
-          <input type="radio" name="manageurs_visibility" value="0"
-            {if $manageurs_visibility="blocked"}checked="checked"{/if} />
+          <input type="radio" name="manageurs_visibility" value="blocked"
+            {if $manageurs_visibility == "blocked"}checked="checked"{/if} />
           Les entreprises ne peuvent pas voir mon profil.
         </label>
       </div>
       <small>Les offres d'entreprises, les messages de diplômés et la communication Manageurs seront envoyés à cette adresse.</small>
     </td>
     <td>
-      <input type="text" name="manageurs_email" value="{$manageurs_email}" size="40" />
+      <input type="text" name="manageurs_email" value="{$manageurs_email}" size="40" maxlength="250" />
     </td>
   </tr>
   <tr>
     <td>
       <div>
         <label>
-          <input type="radio" name="manageurs_push" value="0"
-            {if $manageurs_push="unique"}checked="checked"{/if} />
+          <input type="radio" name="manageurs_push" value="unique"
+            {if $manageurs_push == "unique"}checked="checked"{/if} />
           recevoir un email par offre.
         </label>
         <br />
         <label>
-          <input type="radio" name="manageurs_push" value="1"
-            {if $manageurs_push="weekly"}checked="checked"{/if} />
+          <input type="radio" name="manageurs_push" value="weekly"
+            {if $manageurs_push == "weekly"}checked="checked"{/if} />
           recevoir une compilation des offres 1 fois par semaine.
         </label>
         <br />
         <label>
-          <input type="radio" name="manageurs_push" value="2"
-            {if $manageurs_push="never"}checked="checked"{/if} />
+          <input type="radio" name="manageurs_push" value="never"
+            {if $manageurs_push == "never"}checked="checked"{/if} />
           ne jamais recevoir d'email.
         </label>
       </div>