diff --git a/controllers/contacts.php b/controllers/contacts.php index c0d246e..48d95e9 100755 --- a/controllers/contacts.php +++ b/controllers/contacts.php @@ -182,13 +182,42 @@ foreach ($_POST['contacts'] as $id => $contact) { - if (!$number = internalTools::parsePhone($contact['phone'])) + if (!isset($contact['name']) || !isset($contact['phone']) || (RASPISMS_SETTINGS_EXTENDED_CONTACTS_INFOS && !isset($contact['civility']))) { - $errors[] = $contact['id']; + $errors[] = $id; continue; } - $db->updateTableWhere('contacts', ['name' => $contact['name'], 'number' => $number], ['id' => $id]); + $nom = $contact['name']; + $phone = $contact['phone']; + // on enregistre les infos si elles ont été saisies par l'utilisateur + $civility = isset($contact['civility']) ? $contact['civility'] : null; + $prenom = isset($contact['first_name']) ? $contact['first_name'] : null; + $birthday = isset($contact['birthday']) ? $contact['birthday'] : null; + $loveSituation = isset($contact['love_situation']) ? $contact['love_situation'] : null; + $nomComplet = $prenom ? $prenom.' '.$nom : $nom; + + if (!$number = internalTools::parsePhone($contact['phone'])) + { + $errors[] = $id; + continue; + } + + $db->updateTableWhere('contacts', ['name' => $nomComplet, 'number' => $number], ['id' => $id]); + + if (!isset($contact['contacts_infos_id'])) { + if (!$db->insertIntoTable('contacts_infos', ['id_contact' => $id, 'civility' => $civility, 'first_name' => $prenom, 'last_name' => $nom, 'birthday' => $birthday, 'love_situation' => $loveSituation])) + { + $errors[] = $id; + continue; + } + } + + if (!$db->updateTableWhere('contacts_infos', ['id_contact' => $id, 'civility' => $civility, 'first_name' => $prenom, 'last_name' => $nom, 'birthday' => $birthday, 'love_situation' => $loveSituation], ['id' => $contact['contacts_infos_id']])) + { + $errors[] = $id; + continue; + } } //Si on a eu des erreurs diff --git a/model/DataBase.php b/model/DataBase.php index 9a35e03..9511bbf 100755 --- a/model/DataBase.php +++ b/model/DataBase.php @@ -249,18 +249,27 @@ { if (RASPISMS_SETTINGS_EXTENDED_CONTACTS_INFOS) { $extended_contact_join = ' - LEFT JOIN contacts_infos as inf - ON (inf.id_contact = contacts.id) + LEFT JOIN contacts_infos + ON (contacts_infos.id_contact = contacts.id) '; + $tableToDescribe = "contacts,contacts_infos"; } else { $extended_contact_join = ''; + $tableToDescribe = "contacts"; } - $query = " - SELECT * + $fields = $this->describeTable($tableToDescribe); + + // liste les champs disponibles pour ajouter des alias et éviter des problèmes en cas de colonnes avec le même nom + $fieldNames = array_keys($fields); + foreach ($fieldNames as $key => $fieldName) { + $fieldNames[$key] = $fieldName . " AS '" . $fieldName . "'"; + } + + $query = "SELECT " . implode(', ', $fieldNames) . " FROM contacts ".$extended_contact_join." - WHERE id "; + WHERE contacts.id "; //On génère la clause IN et les paramètres adaptés depuis le tableau des id $generted_in = $this->generateInFromArray($contacts_ids); diff --git a/templates/contacts/edit.php b/templates/contacts/edit.php index 33ea099..e099348 100755 --- a/templates/contacts/edit.php +++ b/templates/contacts/edit.php @@ -41,27 +41,65 @@ <?php foreach ($contacts as $contact) { - ?> - <div class="form-group"> - <label>Nom contact</label> - <div class="form-group input-group"> - <span class="input-group-addon"><span class="fa fa-user"></span></span> - <input name="contacts[<?php secho($contact['id']); ?>][name]" class="form-control" type="text" placeholder="Nom contact" autofocus required value="<?php secho($contact['name']); ?>"> - </div> - </div> - <div class="form-group"> + if (RASPISMS_SETTINGS_EXTENDED_CONTACTS_INFOS) { ?> + <div class="form-group"> + <label>Civilité du contact</label> + <div class="form-group"> + <input name="contacts[<?php secho($contact['contacts.id']); ?>][civility]" type="radio" value="1" required <?php echo ($contact['contacts_infos.civility']==='1' ? 'checked' : ''); ?>/> Monsieur + <input name="contacts[<?php secho($contact['contacts.id']); ?>][civility]" type="radio" value="0" required <?php echo ($contact['contacts_infos.civility']==='0' ? 'checked' : ''); ?>/> Madame + </div> + </div> + <div class="form-group"> + <label>Prénom du contact (facultatif)</label> + <div class="form-group input-group"> + <span class="input-group-addon"><span class="fa fa-user"></span></span> + <input name="contacts[<?php secho($contact['contacts.id']); ?>][first_name]" class="form-control" type="text" placeholder="Prénom du contact (facultatif)" value="<?php secho($contact['contacts_infos.first_name']); ?>"> + </div> + </div> + <?php } ?> + <?php + if (RASPISMS_SETTINGS_EXTENDED_CONTACTS_INFOS) { + $name = ($contact['contacts_infos.last_name'] != '') ? $contact['contacts_infos.last_name'] : $contact['contacts.name']; + $tableAlias = "contacts."; + } else { + $name = $contact['name']; + $tableAlias = ''; + } + ?> + <div class="form-group"> + <label>Nom du contact</label> + <div class="form-group input-group"> + <span class="input-group-addon"><span class="fa fa-user"></span></span> + <input name="contacts[<?php secho($contact[$tableAlias.'id']); ?>][name]" class="form-control" type="text" placeholder="Nom du contact" required value="<?php secho($name); ?>"> + </div> + </div> + <div class="form-group"> <label>Numéro de téléphone du contact</label> <div class="form-group"> - <input name="" class="form-control phone-international-input" type="tel" contact-id="<?php secho($contact['id']); ?>" value="<?php secho($contact['number']); ?>"> - <input name="contacts[<?php secho($contact['id']); ?>][phone]" type="hidden" id="phone-hidden-input-<?php secho($contact['id']); ?>" required> + <input name="" class="form-control phone-international-input" type="tel" contact-id="<?php secho($contact[$tableAlias.'id']); ?>" value="<?php secho($contact[$tableAlias.'number']); ?>"> + <input name="contacts[<?php secho($contact[$tableAlias.'id']); ?>][phone]" type="hidden" id="phone-hidden-input-<?php secho($contact[$tableAlias.'id']); ?>" required> </div> </div> + <?php if (RASPISMS_SETTINGS_EXTENDED_CONTACTS_INFOS) { ?> + <input name="contacts[<?php secho($contact['contacts.id']); ?>][contacts_infos_id]" type="hidden" value="<?php secho($contact['contacts_infos.id']); ?>"> + <div class="form-group"> + <label>Date de naissance du contact (facultatif)</label> + <input name="contacts[<?php secho($contact['contacts.id']); ?>][birthday]" class="form-control form-date" type="text" readonly value="<?php secho($contact['contacts_infos.birthday']); ?>"> + </div> + <div class="form-group"> + <label>Situation amoureuse du contact (facultatif)</label> + <div class="form-group"> + <input name="contacts[<?php secho($contact['contacts.id']); ?>][love_situation]" type="radio" value="0" <?php echo ($contact['contacts_infos.love_situation']==='0' ? 'checked' : ''); ?>/> Célibataire + <input name="contacts[<?php secho($contact['contacts.id']); ?>][love_situation]" type="radio" value="1" <?php echo ($contact['contacts_infos.love_situation']==='1' ? 'checked' : ''); ?>/> En couple + </div> + </div> + <?php } ?> <hr/> <?php } ?> <a class="btn btn-danger" href="<?php echo $this->generateUrl('contacts'); ?>">Annuler</a> - <input type="submit" class="btn btn-success" value="Enregistrer le contact" /> + <input type="submit" class="btn btn-success" value="Enregistrer le contact" /> </form> </div> </div> @@ -80,6 +118,15 @@ utilsScript: '<?php echo HTTP_PWD; ?>/js/intlTelInput/lib/libphonenumber/utils.js' }); + jQuery('.form-date').datepicker( + { + format: 'yyyy-mm-dd', + autoclose: true, + minuteStep: 1, + startView: 3, + language: 'fr' + }); + jQuery('form').on('submit', function(e) { e.preventDefault(); @@ -87,7 +134,7 @@ { jQuery('#phone-hidden-input-' + jQuery(this).attr('contact-id')).val(jQuery(this).intlTelInput("getNumber")); }); - + this.submit(); }); });