diff --git a/api/src/Model/Services/UserData.php b/api/src/Model/Services/UserData.php index bb762b322..2a01cdbb5 100644 --- a/api/src/Model/Services/UserData.php +++ b/api/src/Model/Services/UserData.php @@ -192,7 +192,7 @@ function getUsers($getCount, $isStaffMember, $stringMatch, $page, $sortBy = null } } - $rows = $this->db->paginate("SELECT $extc p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, '' as postcode, l.country + $rows = $this->db->paginate("SELECT $extc p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, l.postcode, l.country FROM person p LEFT OUTER JOIN proposalhasperson prhp ON prhp.personid = p.personid LEFT OUTER JOIN labcontact lc ON lc.personid = p.personid @@ -253,7 +253,7 @@ function getUsersForProposal($where, $getCount, $page, $sortBy, $proposalid, $cu } } - $extc = "p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, '' as postcode, l.country"; + $extc = "p.personid, p.givenname, p.familyname, CONCAT(p.givenname, ' ', p.familyname) as fullname, p.login, p.emailaddress, p.phonenumber, l.name as labname, l.address, l.city, l.postcode, l.country"; $rows = $this->db->paginate("(SELECT $extc FROM person p LEFT OUTER JOIN ProposalHasPerson prhp ON prhp.personid = p.personid diff --git a/api/src/Page/Contact.php b/api/src/Page/Contact.php index 762656e0a..10807e4c8 100644 --- a/api/src/Page/Contact.php +++ b/api/src/Page/Contact.php @@ -149,19 +149,62 @@ function _add_contact() { WHERE c.cardname=:1 and c.proposalid=:2", array($this->arg('CARDNAME'), $this->proposalid)); if (sizeof($cont)) $this->_error('The specified card name already exists'); - $this->db->pq("INSERT INTO laboratory (laboratoryid,name,address,city,postcode,country) - VALUES (s_laboratory.nextval, :1, :2, :3, :4, :5) RETURNING laboratoryid INTO :id", - array($this->arg('LABNAME'), $this->arg('ADDRESS'), $this->arg('CITY'), $this->arg('POSTCODE'), $this->arg('COUNTRY'))); - $lid = $this->db->id(); - - $email = $this->has_arg('EMAILADDRESS') ? $this->arg('EMAILADDRESS') : ''; - $phone = $this->has_arg('PHONENUMBER') ? $this->arg('PHONENUMBER') : ''; - - $this->db->pq("INSERT INTO person (personid, givenname, familyname, emailaddress, phonenumber, laboratoryid) - VALUES (s_person.nextval, :1, :2, :3, :4, :5) RETURNING personid INTO :id", - array($this->arg('GIVENNAME'), $this->arg('FAMILYNAME'), $email, $phone, $lid)); + if ($this->has_arg('PERSONID')) { + $pid = $this->arg('PERSONID'); + + $check = $this->db->pq("SELECT c.labcontactid + FROM labcontact c + WHERE c.personid=:1 and c.proposalid=:2", array($pid, $this->proposalid)); + if (sizeof($check)) $this->_error('The specified login already has a lab contact for this proposal'); + + $lab = $this->db->pq("SELECT l.laboratoryid FROM laboratory l + INNER JOIN person p ON l.laboratoryid = p.laboratoryid + WHERE p.personid=:1", array($pid)); + + if (sizeof($lab)) { + # Update laboratory + $lfields = array('LABNAME', 'ADDRESS', 'CITY', 'COUNTRY', 'POSTCODE'); + foreach ($lfields as $i => $f) { + if ($this->has_arg($f)) { + $c = $f == 'LABNAME' ? 'NAME' : $f; + $this->db->pq('UPDATE laboratory SET '.$c.'=:1 WHERE laboratoryid=:2', array($this->arg($f), $lab['LABORATORYID'])); + } + } + } + else { + # Create laboratory + $this->db->pq("INSERT INTO laboratory (laboratoryid,name,address,city,postcode,country) + VALUES (s_laboratory.nextval, :1, :2, :3, :4, :5) RETURNING laboratoryid INTO :id", + array($this->arg('LABNAME'), $this->arg('ADDRESS'), $this->arg('CITY'), $this->arg('POSTCODE'), $this->arg('COUNTRY'))); + $lid = $this->db->id(); + $this->db->pq('UPDATE person SET laboratoryid=:1 WHERE personid=:2', array($lid, $pid)); + } + + # Update person + $pfields = array('FAMILYNAME', 'GIVENNAME', 'PHONENUMBER', 'EMAILADDRESS'); + foreach ($pfields as $i => $f) { + if ($this->has_arg($f)) { + $this->db->pq('UPDATE person SET '.$f.'=:1 WHERE personid=:2', array($this->arg($f), $pid)); + } + } + + } + else { - $pid = $this->db->id(); + $this->db->pq("INSERT INTO laboratory (laboratoryid,name,address,city,postcode,country) + VALUES (s_laboratory.nextval, :1, :2, :3, :4, :5) RETURNING laboratoryid INTO :id", + array($this->arg('LABNAME'), $this->arg('ADDRESS'), $this->arg('CITY'), $this->arg('POSTCODE'), $this->arg('COUNTRY'))); + $lid = $this->db->id(); + + $email = $this->has_arg('EMAILADDRESS') ? $this->arg('EMAILADDRESS') : ''; + $phone = $this->has_arg('PHONENUMBER') ? $this->arg('PHONENUMBER') : ''; + + $this->db->pq("INSERT INTO person (personid, givenname, familyname, emailaddress, phonenumber, laboratoryid) + VALUES (s_person.nextval, :1, :2, :3, :4, :5) RETURNING personid INTO :id", + array($this->arg('GIVENNAME'), $this->arg('FAMILYNAME'), $email, $phone, $lid)); + + $pid = $this->db->id(); + } $c = $this->def_arg('DEFAULTCOURRIERCOMPANY', ''); $ca = $this->has_arg('COURIERACCOUNT') ? $this->arg('COURIERACCOUNT') : ''; diff --git a/client/src/js/models/labcontact.js b/client/src/js/models/labcontact.js index 1b732ca40..b2eff310d 100644 --- a/client/src/js/models/labcontact.js +++ b/client/src/js/models/labcontact.js @@ -9,6 +9,9 @@ define(['backbone'], function(Backbone) { required: true, pattern: 'wwsdash', }, + PERSONID: { + required: false, + }, FAMILYNAME: { required: true, pattern: 'wwdash', diff --git a/client/src/js/modules/contact/views/addcontact.js b/client/src/js/modules/contact/views/addcontact.js index 29d7c9f34..45f61414a 100644 --- a/client/src/js/modules/contact/views/addcontact.js +++ b/client/src/js/modules/contact/views/addcontact.js @@ -1,6 +1,7 @@ define(['views/form', 'models/labcontact', 'collections/countries', + 'collections/users', 'templates/contact/contactadd.html', 'jquery', 'backbone', @@ -8,7 +9,7 @@ define(['views/form', 'backbone-validation', ], function(FormView, - Contact, Countries, + Contact, Countries, Users, template, $_, Backbone) { @@ -17,26 +18,68 @@ define(['views/form', ui: { country: 'select[name=COUNTRY]', + pid: 'select[name=PERSONID]', + cn: 'input[name=CARDNAME]', + fn: 'input[name=FAMILYNAME]', + gn: 'input[name=GIVENNAME]', + pn: 'input[name=PHONENUMBER]', + ea: 'input[name=EMAILADDRESS]', + ln: 'input[name=LABNAME]', + ad: 'textarea[name=ADDRESS]', + ci: 'input[name=CITY]', + pc: 'input[name=POSTCODE]', + }, + + events: { + 'change @ui.pid': 'fillUserInfo', + }, + + fillUserInfo: function() { + var u = this.users.findWhere({ PERSONID: this.ui.pid.val() }) + if (u){ + this.ui.cn.val(u.get('FULLNAME')) + this.ui.fn.val(u.get('FAMILYNAME')) + this.ui.gn.val(u.get('GIVENNAME')) + this.ui.pn.val(u.get('PHONENUMBER')) + this.ui.ea.val(u.get('EMAILADDRESS')) + this.ui.ln.val(u.get('LABNAME')) + this.ui.ad.val(u.get('ADDRESS')) + this.ui.ci.val(u.get('CITY')) + this.ui.pc.val(u.get('POSTCODE')) + this.ui.country.val(u.get('COUNTRY')) + } }, initialize: function() { this.countries = new Countries() this.countries.state.pageSize = 9999 - this.ready = this.countries.fetch() + this.users = new Users() + this.users.queryParams.login = 1 }, createModel: function() { this.model = new Contact() }, - + onRender: function() { - $.when(this.ready).done(this.populateCountries.bind(this)) + Promise.all([this.countries.fetch(), this.users.fetch()]).then(() => { + this.populateCountries(); + this.populateUsers(); + }) }, populateCountries: function() { this.ui.country.html(this.countries.opts()) }, + populateUsers: function() { + // only want to display the current user + var u = this.users.findWhere({ PERSONID: app.personid.toString() }) + var opts = '' + opts += '' + this.ui.pid.html(opts) + }, + success: function(model, response, options) { console.log('success from contact add') if (this.getOption('dialog')) { diff --git a/client/src/js/templates/contact/contactadd.html b/client/src/js/templates/contact/contactadd.html index ac10a6303..6aee6dc98 100644 --- a/client/src/js/templates/contact/contactadd.html +++ b/client/src/js/templates/contact/contactadd.html @@ -18,6 +18,13 @@

Add New Home Lab Contact

  • Contact Details
  • +
  • + + +
  • +