email address develop
This commit is contained in:
@@ -177,229 +177,10 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable
|
||||
return $result;
|
||||
}
|
||||
|
||||
// TODO move this logic somewhere out
|
||||
protected function handleEmailAddressSave(Entity $entity)
|
||||
{
|
||||
if ($entity->hasRelation('emailAddresses') && $entity->hasField('emailAddress')) {
|
||||
$email = trim($entity->get('emailAddress'));
|
||||
$emailAddressData = null;
|
||||
|
||||
if ($entity->has('emailAddressData')) {
|
||||
$emailAddressData = $entity->get('emailAddressData');
|
||||
}
|
||||
|
||||
$pdo = $this->getPDO();
|
||||
|
||||
$emailAddressRepository = $this->getEntityManager()->getRepository('EmailAddress');
|
||||
|
||||
if ($emailAddressData !== null && is_array($emailAddressData)) {
|
||||
$previousEmailAddressData = array();
|
||||
if (!$entity->isNew()) {
|
||||
$previousEmailAddressData = $emailAddressRepository->getEmailAddressData($entity);
|
||||
}
|
||||
|
||||
$hash = array();
|
||||
foreach ($emailAddressData as $row) {
|
||||
$key = $row->emailAddress;
|
||||
if (!empty($key)) {
|
||||
$hash[$key] = array(
|
||||
'primary' => $row->primary ? true : false,
|
||||
'optOut' => $row->optOut ? true : false,
|
||||
'invalid' => $row->invalid ? true : false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$hashPrev = array();
|
||||
foreach ($previousEmailAddressData as $row) {
|
||||
$key = $row->emailAddress;
|
||||
if (!empty($key)) {
|
||||
$hashPrev[$key] = array(
|
||||
'primary' => $row->primary ? true : false,
|
||||
'optOut' => $row->optOut ? true : false,
|
||||
'invalid' => $row->invalid ? true : false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$primary = false;
|
||||
$toCreate = array();
|
||||
$toUpdate = array();
|
||||
$toRemove = array();
|
||||
|
||||
|
||||
foreach ($hash as $key => $data) {
|
||||
$new = true;
|
||||
$changed = false;
|
||||
|
||||
if ($hash[$key]['primary']) {
|
||||
$primary = $key;
|
||||
}
|
||||
|
||||
if (array_key_exists($key, $hashPrev)) {
|
||||
$new = false;
|
||||
$changed = $hash[$key]['optOut'] != $hashPrev[$key]['optOut'] || $hash[$key]['invalid'] != $hashPrev[$key]['invalid'];
|
||||
if ($hash[$key]['primary']) {
|
||||
if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) {
|
||||
$primary = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($new) {
|
||||
$toCreate[] = $key;
|
||||
}
|
||||
if ($changed) {
|
||||
$toUpdate[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($hashPrev as $key => $data) {
|
||||
if (!array_key_exists($key, $hash)) {
|
||||
$toRemove[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*echo $primary;
|
||||
print_r($toCreate);
|
||||
print_r($toUpdate);
|
||||
print_r($toRemove);
|
||||
|
||||
die;*/
|
||||
|
||||
foreach ($toRemove as $address) {
|
||||
$emailAddress = $emailAddressRepository->getByAddress($address);
|
||||
if ($emailAddress) {
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `deleted` = 1, `primary` = 0
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($this->entityName)." AND
|
||||
email_address_id = ".$pdo->quote($emailAddress->id)."
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($toUpdate as $address) {
|
||||
$emailAddress = $emailAddressRepository->getByAddress($address);
|
||||
if ($emailAddress) {
|
||||
$emailAddress->set(array(
|
||||
'optOut' => $hash[$address]['optOut'],
|
||||
'invalid' => $hash[$address]['invalid'],
|
||||
));
|
||||
$emailAddressRepository->save($emailAddress);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($toCreate as $address) {
|
||||
$emailAddress = $emailAddressRepository->getByAddress($address);
|
||||
if (!$emailAddress) {
|
||||
$emailAddress = $emailAddressRepository->get();
|
||||
|
||||
$emailAddress->set(array(
|
||||
'name' => $address,
|
||||
'optOut' => $hash[$address]['optOut'],
|
||||
'invalid' => $hash[$address]['invalid'],
|
||||
));
|
||||
$emailAddressRepository->save($emailAddress);
|
||||
} else {
|
||||
if ($emailAddress->get('optOut') != $hash[$address]['optOut'] || $emailAddress->get('invalid') != $hash[$address]['invalid']) {
|
||||
$emailAddress->set(array(
|
||||
'optOut' => $hash[$address]['optOut'],
|
||||
'invalid' => $hash[$address]['invalid'],
|
||||
));
|
||||
$emailAddressRepository->save($emailAddress);
|
||||
}
|
||||
}
|
||||
|
||||
$query = "
|
||||
INSERT entity_email_address
|
||||
(entity_id, entity_type, email_address_id, `primary`)
|
||||
VALUES
|
||||
(
|
||||
".$pdo->quote($entity->id).",
|
||||
".$pdo->quote($this->entityName).",
|
||||
".$pdo->quote($emailAddress->id).",
|
||||
".$pdo->quote($address === $primary)."
|
||||
)
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
|
||||
if ($primary) {
|
||||
$emailAddress = $emailAddressRepository->getByAddress($primary);
|
||||
if ($emailAddress) {
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `primary` = 0
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($this->entityName)." AND
|
||||
`primary` = 1 AND
|
||||
deleted = 0
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `primary` = 1
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($this->entityName)." AND
|
||||
email_address_id = ".$pdo->quote($emailAddress->id)." AND
|
||||
deleted = 0
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
if (!empty($email)) {
|
||||
if ($email != $entity->getFetched('emailAddress')) {
|
||||
|
||||
$emailAddressNew = $emailAddressRepository->where(array('lower' => strtolower($email)))->findOne();
|
||||
$isNewEmailAddress = false;
|
||||
if (!$emailAddressNew) {
|
||||
$emailAddressNew = $emailAddressRepository->get();
|
||||
$emailAddressNew->set('name', $email);
|
||||
$emailAddressRepository->save($emailAddressNew);
|
||||
$isNewEmailAddress = true;
|
||||
}
|
||||
|
||||
$emailOld = $entity->getFetched('emailAddress');
|
||||
if (!empty($emailOld)) {
|
||||
$emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne();
|
||||
$this->unrelate($entity, 'emailAddresses', $emailAddressOld);
|
||||
}
|
||||
$this->relate($entity, 'emailAddresses', $emailAddressNew);
|
||||
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `primary` = 1
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($this->entityName)." AND
|
||||
email_address_id = ".$pdo->quote($emailAddressNew->id)."
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
} else {
|
||||
$emailOld = $entity->getFetched('emailAddress');
|
||||
if (!empty($emailOld)) {
|
||||
$emailAddressOld = $emailAddressRepository->where(array('lower' => strtolower($emailOld)))->findOne();
|
||||
$this->unrelate($entity, 'emailAddresses', $emailAddressOld);
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
if ($entity->hasRelation('emailAddresses') && $entity->hasField('emailAddress')) {
|
||||
$emailAddressRepository = $this->getEntityManager()->getRepository('EmailAddress')->storeEntityEmailAddress($entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,5 +94,219 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB
|
||||
{
|
||||
return $this->where(array('lower' => strtolower($address)))->findOne();
|
||||
}
|
||||
|
||||
public function storeEntityEmailAddress(Entity $entity)
|
||||
{
|
||||
$email = trim($entity->get('emailAddress'));
|
||||
$emailAddressData = null;
|
||||
|
||||
if ($entity->has('emailAddressData')) {
|
||||
$emailAddressData = $entity->get('emailAddressData');
|
||||
}
|
||||
|
||||
$pdo = $this->getEntityManager()->getPDO();
|
||||
|
||||
if ($emailAddressData !== null && is_array($emailAddressData)) {
|
||||
$previousEmailAddressData = array();
|
||||
if (!$entity->isNew()) {
|
||||
$previousEmailAddressData = $this->getEmailAddressData($entity);
|
||||
}
|
||||
|
||||
$hash = array();
|
||||
foreach ($emailAddressData as $row) {
|
||||
$key = $row->emailAddress;
|
||||
if (!empty($key)) {
|
||||
$hash[$key] = array(
|
||||
'primary' => $row->primary ? true : false,
|
||||
'optOut' => $row->optOut ? true : false,
|
||||
'invalid' => $row->invalid ? true : false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$hashPrev = array();
|
||||
foreach ($previousEmailAddressData as $row) {
|
||||
$key = $row->emailAddress;
|
||||
if (!empty($key)) {
|
||||
$hashPrev[$key] = array(
|
||||
'primary' => $row->primary ? true : false,
|
||||
'optOut' => $row->optOut ? true : false,
|
||||
'invalid' => $row->invalid ? true : false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$primary = false;
|
||||
$toCreate = array();
|
||||
$toUpdate = array();
|
||||
$toRemove = array();
|
||||
|
||||
|
||||
foreach ($hash as $key => $data) {
|
||||
$new = true;
|
||||
$changed = false;
|
||||
|
||||
if ($hash[$key]['primary']) {
|
||||
$primary = $key;
|
||||
}
|
||||
|
||||
if (array_key_exists($key, $hashPrev)) {
|
||||
$new = false;
|
||||
$changed = $hash[$key]['optOut'] != $hashPrev[$key]['optOut'] || $hash[$key]['invalid'] != $hashPrev[$key]['invalid'];
|
||||
if ($hash[$key]['primary']) {
|
||||
if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) {
|
||||
$primary = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($new) {
|
||||
$toCreate[] = $key;
|
||||
}
|
||||
if ($changed) {
|
||||
$toUpdate[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($hashPrev as $key => $data) {
|
||||
if (!array_key_exists($key, $hash)) {
|
||||
$toRemove[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($toRemove as $address) {
|
||||
$emailAddress = $this->getByAddress($address);
|
||||
if ($emailAddress) {
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `deleted` = 1, `primary` = 0
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($entity->getEntityName())." AND
|
||||
email_address_id = ".$pdo->quote($emailAddress->id)."
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($toUpdate as $address) {
|
||||
$emailAddress = $this->getByAddress($address);
|
||||
if ($emailAddress) {
|
||||
$emailAddress->set(array(
|
||||
'optOut' => $hash[$address]['optOut'],
|
||||
'invalid' => $hash[$address]['invalid'],
|
||||
));
|
||||
$this->save($emailAddress);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($toCreate as $address) {
|
||||
$emailAddress = $this->getByAddress($address);
|
||||
if (!$emailAddress) {
|
||||
$emailAddress = $this->get();
|
||||
|
||||
$emailAddress->set(array(
|
||||
'name' => $address,
|
||||
'optOut' => $hash[$address]['optOut'],
|
||||
'invalid' => $hash[$address]['invalid'],
|
||||
));
|
||||
$this->save($emailAddress);
|
||||
} else {
|
||||
if ($emailAddress->get('optOut') != $hash[$address]['optOut'] || $emailAddress->get('invalid') != $hash[$address]['invalid']) {
|
||||
$emailAddress->set(array(
|
||||
'optOut' => $hash[$address]['optOut'],
|
||||
'invalid' => $hash[$address]['invalid'],
|
||||
));
|
||||
$this->save($emailAddress);
|
||||
}
|
||||
}
|
||||
|
||||
$query = "
|
||||
INSERT entity_email_address
|
||||
(entity_id, entity_type, email_address_id, `primary`)
|
||||
VALUES
|
||||
(
|
||||
".$pdo->quote($entity->id).",
|
||||
".$pdo->quote($entity->getEntityName()).",
|
||||
".$pdo->quote($emailAddress->id).",
|
||||
".$pdo->quote($address === $primary)."
|
||||
)
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
|
||||
if ($primary) {
|
||||
$emailAddress = $this->getByAddress($primary);
|
||||
if ($emailAddress) {
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `primary` = 0
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($entity->getEntityName())." AND
|
||||
`primary` = 1 AND
|
||||
deleted = 0
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `primary` = 1
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($entity->getEntityName())." AND
|
||||
email_address_id = ".$pdo->quote($emailAddress->id)." AND
|
||||
deleted = 0
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
$entityRepository = $this->getEntityManager()->getRepository($entity->getEntityName());
|
||||
if (!empty($email)) {
|
||||
if ($email != $entity->getFetched('emailAddress')) {
|
||||
|
||||
$emailAddressNew = $this->where(array('lower' => strtolower($email)))->findOne();
|
||||
$isNewEmailAddress = false;
|
||||
if (!$emailAddressNew) {
|
||||
$emailAddressNew = $this->get();
|
||||
$emailAddressNew->set('name', $email);
|
||||
$this->save($emailAddressNew);
|
||||
$isNewEmailAddress = true;
|
||||
}
|
||||
|
||||
$emailOld = $entity->getFetched('emailAddress');
|
||||
if (!empty($emailOld)) {
|
||||
$emailAddressOld = $this->getByAddress($emailOld);
|
||||
$entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld);
|
||||
}
|
||||
$entityRepository->relate($entity, 'emailAddresses', $emailAddressNew);
|
||||
|
||||
$query = "
|
||||
UPDATE entity_email_address
|
||||
SET `primary` = 1
|
||||
WHERE
|
||||
entity_id = ".$pdo->quote($entity->id)." AND
|
||||
entity_type = ".$pdo->quote($entity->getEntityName())." AND
|
||||
email_address_id = ".$pdo->quote($emailAddressNew->id)."
|
||||
";
|
||||
$sth = $pdo->prepare($query);
|
||||
$sth->execute();
|
||||
}
|
||||
} else {
|
||||
$emailOld = $entity->getFetched('emailAddress');
|
||||
if (!empty($emailOld)) {
|
||||
$emailAddressOld = $this->getByAddress($emailOld);
|
||||
$entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
{{#each emailAddressData}}
|
||||
<div>
|
||||
{{#unless invalid}}{{#unless optOut}}
|
||||
<a href="javascript:" data-email-address="{{emailAddress}}" data-action="mailTo">
|
||||
{{/unless}}{{/unless}}
|
||||
{{#if emailAddressData}}
|
||||
{{#each emailAddressData}}
|
||||
<div>
|
||||
{{#unless invalid}}{{#unless optOut}}
|
||||
<a href="javascript:" data-email-address="{{emailAddress}}" data-action="mailTo">
|
||||
{{/unless}}{{/unless}}
|
||||
|
||||
<span {{#if invalid}}style="text-decoration: line-through;"{{/if}}>
|
||||
{{emailAddress}}
|
||||
</span>
|
||||
<span {{#if invalid}}style="text-decoration: line-through;"{{/if}}>
|
||||
{{emailAddress}}
|
||||
</span>
|
||||
|
||||
{{#unless invalid}}{{#unless optOut}}
|
||||
</a>
|
||||
{{/unless}}{{/unless}}
|
||||
</div>
|
||||
{{/each}}
|
||||
{{#unless invalid}}{{#unless optOut}}
|
||||
</a>
|
||||
{{/unless}}{{/unless}}
|
||||
</div>
|
||||
{{/each}}
|
||||
{{else}}
|
||||
<a href="javascript:" data-email-address="{{value}}" data-action="mailTo">{{value}}</a>
|
||||
{{/if}}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
<div>
|
||||
{{#each emailAddressData}}
|
||||
<div class="input-group email-address-block">
|
||||
<div class="input-group email-address-block">
|
||||
<input type="email" class="form-control email-address" value="{{emailAddress}}" autocomplete="off">
|
||||
<span class="input-group-btn">
|
||||
<button class="btn btn-default email-property{{#if primary}} active{{/if}}" type="button" tabindex="-1" data-action="switchEmailProperty" data-property-type="primary" data-toggle="tooltip" data-placement="top" title="{{translate 'Primary' scope='EmailAddress'}}">
|
||||
@@ -12,9 +13,13 @@
|
||||
<button class="btn btn-default email-property{{#if invalid}} active{{/if}}" type="button" tabindex="-1" data-action="switchEmailProperty" data-property-type="invalid" data-toggle="tooltip" data-placement="top" title="{{translate 'Invalid' scope='EmailAddress'}}">
|
||||
<span class="glyphicon glyphicon-exclamation-sign{{#unless invalid}} text-muted{{/unless}}"></span>
|
||||
</button>
|
||||
<button class="btn btn-default" style="margin-left: 5px;" type="button" tabindex="-1" data-action="removeEmailAddress" data-property-type="invalid" data-toggle="tooltip" data-placement="top" title="{{translate 'Remove'}}">
|
||||
<span class="glyphicon glyphicon-remove"></span>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
{{/each}}
|
||||
</div>
|
||||
|
||||
<button class="btn btn-default" type="button" data-action="addEmailAddress"><span class="glyphicon glyphicon-plus"></span></button>
|
||||
|
||||
|
||||
@@ -67,15 +67,32 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
if (this.params.required || this.model.isRequired(this.name)) {
|
||||
if (this.model.get(this.name) === '') {
|
||||
var msg = this.translate('fieldIsRequired', 'messages').replace('{field}', this.translate(this.name, 'fields', this.model.name));
|
||||
this.showValidationMessage(msg, 'button[data-action="addEmailAddress"]');
|
||||
this.showValidationMessage(msg, 'div.email-address-block:nth-child(1) input');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
data: function () {
|
||||
var emailAddressData;
|
||||
if (this.mode == 'edit') {
|
||||
emailAddressData = Espo.Utils.clone(this.model.get(this.dataFieldName));
|
||||
|
||||
if (this.model.isNew()) {
|
||||
if (!emailAddressData || !emailAddressData.length) {
|
||||
emailAddressData = [{
|
||||
emailAddress: '',
|
||||
primary: true,
|
||||
optOut: false,
|
||||
invalid: false
|
||||
}];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
emailAddressData = this.model.get(this.dataFieldName) || false;
|
||||
}
|
||||
return _.extend({
|
||||
emailAddressData: this.model.get(this.name + 'Data') || [],
|
||||
emailAddressData: emailAddressData
|
||||
}, Dep.prototype.data.call(this));
|
||||
},
|
||||
|
||||
@@ -85,14 +102,17 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
},
|
||||
'click [data-action="switchEmailProperty"]': function (e) {
|
||||
var $target = $(e.currentTarget);
|
||||
var $block = $(e.currentTarget).closest('div.email-address-block');
|
||||
var property = $target.data('property-type');
|
||||
|
||||
|
||||
if (property == 'primary') {
|
||||
if (!$target.hasClass('active')) {
|
||||
this.$el.find('button.email-property[data-property-type="primary"]').removeClass('active').children().addClass('text-muted');
|
||||
$target.addClass('active').children().removeClass('text-muted');
|
||||
}
|
||||
|
||||
if ($block.find('input.email-address').val() != '') {
|
||||
this.$el.find('button.email-property[data-property-type="primary"]').removeClass('active').children().addClass('text-muted');
|
||||
$target.addClass('active').children().removeClass('text-muted');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($target.hasClass('active')) {
|
||||
$target.removeClass('active').children().addClass('text-muted');
|
||||
@@ -100,21 +120,37 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
$target.addClass('active').children().removeClass('text-muted');
|
||||
}
|
||||
}
|
||||
this.fetchEmailAddressData();
|
||||
this.fetchToModel();
|
||||
},
|
||||
'change input.email-address': function (e) {
|
||||
var $input = $(e.currentTarget);
|
||||
if ($input.val() == '') {
|
||||
var $block = $input.closest('div.email-address-block');
|
||||
|
||||
'click [data-action="removeEmailAddress"]': function (e) {
|
||||
var $block = $(e.currentTarget).closest('div.email-address-block');
|
||||
if ($block.parent().children().size() == 1) {
|
||||
$block.find('input.email-address').val('');
|
||||
} else {
|
||||
this.removeEmailAddressBlock($block);
|
||||
}
|
||||
this.fetchToModel();
|
||||
},
|
||||
|
||||
'change input.email-address': function (e) {
|
||||
var $input = $(e.currentTarget);
|
||||
var $block = $input.closest('div.email-address-block');
|
||||
|
||||
this.fetchEmailAddressData();
|
||||
if ($input.val() == '') {
|
||||
if ($block.parent().children().size() == 1) {
|
||||
$block.find('input.email-address').val('');
|
||||
} else {
|
||||
this.removeEmailAddressBlock($block);
|
||||
}
|
||||
}
|
||||
|
||||
this.fetchToModel();
|
||||
},
|
||||
|
||||
'click [data-action="addEmailAddress"]': function () {
|
||||
var data = this.model.get(this.dataFieldName) || [];
|
||||
|
||||
'click [data-action="addEmailAddress"]': function () {
|
||||
var data = this.fetchEmailAddressData();
|
||||
|
||||
o = {
|
||||
emailAddress: '',
|
||||
primary: data.length ? false : true,
|
||||
@@ -122,9 +158,9 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
invalid: false
|
||||
};
|
||||
|
||||
data.push(o);
|
||||
data.push(o);
|
||||
|
||||
this.model.set(data, {silent: true});
|
||||
this.model.set(this.dataFieldName, data, {silent: true});
|
||||
this.render();
|
||||
},
|
||||
|
||||
@@ -138,7 +174,7 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
$block.remove();
|
||||
|
||||
if (changePrimary) {
|
||||
this.$el.find('button[data-property-type="primary"]').first().addClass('active');
|
||||
this.$el.find('button[data-property-type="primary"]').first().addClass('active').children().removeClass('text-muted');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -157,10 +193,15 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
|
||||
setup: function () {
|
||||
this.dataFieldName = this.name + 'Data';
|
||||
},
|
||||
|
||||
if (this.mode == 'detail' || this.mode == 'edit') {
|
||||
this.listenTo(this.model, 'change:' + this.dataFieldName, function () {
|
||||
this.render();
|
||||
}, this);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
fetchEmailAddressData: function (dontUpdate) {
|
||||
fetchEmailAddressData: function () {
|
||||
var data = [];
|
||||
|
||||
var $list = this.$el.find('div.email-address-block');
|
||||
@@ -178,16 +219,7 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
row.invalid = $d.find('button[data-property-type="invalid"]').hasClass('active');
|
||||
data.push(row);
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
var o = {};
|
||||
o[this.dataFieldName] = data;
|
||||
|
||||
if (dontUpdate) {
|
||||
this.model.set(o, {silent: true});
|
||||
}
|
||||
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
@@ -195,7 +227,7 @@ Espo.define('Views.Fields.Email', 'Views.Fields.Base', function (Dep) {
|
||||
fetch: function () {
|
||||
var data = {};
|
||||
|
||||
var adderssData = this.fetchEmailAddressData(true);
|
||||
var adderssData = this.fetchEmailAddressData();
|
||||
data[this.dataFieldName] = adderssData;
|
||||
data[this.name] = null;
|
||||
if (adderssData.length) {
|
||||
|
||||
Reference in New Issue
Block a user