| 1 | (function (angular, $, _) { |
| 2 | |
| 3 | // The representation of from/reply-to addresses is inconsistent in the mailing data-model, |
| 4 | // so the UI must do some adaptation. The crmFromAddresses provides a richer way to slice/dice |
| 5 | // the available "From:" addrs. Records are like the underlying OptionValues -- but add "email" |
| 6 | // and "author". |
| 7 | angular.module('crmMailing').factory('crmFromAddresses', function ($q, crmApi) { |
| 8 | var emailRegex = /^"(.*)" <([^@>]*@[^@>]*)>$/; |
| 9 | var addrs = _.map(CRM.crmMailing.fromAddress, function (addr) { |
| 10 | var match = emailRegex.exec(addr.label); |
| 11 | return angular.extend({}, addr, { |
| 12 | email: match ? match[2] : '(INVALID)', |
| 13 | author: match ? match[1] : '(INVALID)' |
| 14 | }); |
| 15 | }); |
| 16 | |
| 17 | function first(array) { |
| 18 | return (array.length === 0) ? null : array[0]; |
| 19 | } |
| 20 | |
| 21 | return { |
| 22 | getAll: function getAll() { |
| 23 | return addrs; |
| 24 | }, |
| 25 | getByAuthorEmail: function getByAuthorEmail(author, email, autocreate) { |
| 26 | var result = null; |
| 27 | _.each(addrs, function (addr) { |
| 28 | if (addr.author == author && addr.email == email) { |
| 29 | result = addr; |
| 30 | } |
| 31 | }); |
| 32 | if (!result && autocreate) { |
| 33 | result = { |
| 34 | label: '(INVALID) "' + author + '" <' + email + '>', |
| 35 | author: author, |
| 36 | email: email |
| 37 | }; |
| 38 | addrs.push(result); |
| 39 | } |
| 40 | return result; |
| 41 | }, |
| 42 | getByEmail: function getByEmail(email) { |
| 43 | return first(_.where(addrs, {email: email})); |
| 44 | }, |
| 45 | getByLabel: function (label) { |
| 46 | return first(_.where(addrs, {label: label})); |
| 47 | }, |
| 48 | getDefault: function getDefault() { |
| 49 | return first(_.where(addrs, {is_default: "1"})); |
| 50 | } |
| 51 | }; |
| 52 | }); |
| 53 | |
| 54 | angular.module('crmMailing').factory('crmMsgTemplates', function ($q, crmApi) { |
| 55 | var tpls = _.map(CRM.crmMailing.mesTemplate, function (tpl) { |
| 56 | return angular.extend({}, tpl, { |
| 57 | //id: tpl parseInt(tpl.id) |
| 58 | }); |
| 59 | }); |
| 60 | window.tpls = tpls; |
| 61 | var lastModifiedTpl = null; |
| 62 | return { |
| 63 | // Get a template |
| 64 | // @param id MessageTemplate id (per APIv3) |
| 65 | // @return Promise MessageTemplate (per APIv3) |
| 66 | get: function get(id) { |
| 67 | return crmApi('MessageTemplate', 'getsingle', { |
| 68 | "return": "id,msg_subject,msg_html,msg_title,msg_text", |
| 69 | "id": id |
| 70 | }); |
| 71 | }, |
| 72 | // Save a template |
| 73 | // @param tpl MessageTemplate (per APIv3) For new templates, omit "id" |
| 74 | // @return Promise MessageTemplate (per APIv3) |
| 75 | save: function (tpl) { |
| 76 | return crmApi('MessageTemplate', 'create', tpl).then(function (response) { |
| 77 | if (!tpl.id) { |
| 78 | tpl.id = '' + response.id; //parseInt(response.id); |
| 79 | tpls.push(tpl); |
| 80 | } |
| 81 | lastModifiedTpl = tpl; |
| 82 | return tpl; |
| 83 | }); |
| 84 | }, |
| 85 | // @return Object MessageTemplate (per APIv3) |
| 86 | getLastModifiedTpl: function () { |
| 87 | return lastModifiedTpl; |
| 88 | }, |
| 89 | getAll: function getAll() { |
| 90 | return tpls; |
| 91 | } |
| 92 | }; |
| 93 | }); |
| 94 | |
| 95 | // The crmMailingMgr service provides business logic for loading, saving, previewing, etc |
| 96 | angular.module('crmMailing').factory('crmMailingMgr', function ($q, crmApi, crmFromAddresses, crmQueue) { |
| 97 | var qApi = crmQueue(crmApi); |
| 98 | var pickDefaultMailComponent = function pickDefaultMailComponent(type) { |
| 99 | var mcs = _.where(CRM.crmMailing.headerfooterList, { |
| 100 | component_type: type, |
| 101 | is_default: "1" |
| 102 | }); |
| 103 | return (mcs.length >= 1) ? mcs[0].id : null; |
| 104 | }; |
| 105 | |
| 106 | return { |
| 107 | // @param scalar idExpr a number or the literal string 'new' |
| 108 | // @return Promise|Object Mailing (per APIv3) |
| 109 | getOrCreate: function getOrCreate(idExpr) { |
| 110 | return (idExpr == 'new') ? this.create() : this.get(idExpr); |
| 111 | }, |
| 112 | // @return Promise Mailing (per APIv3) |
| 113 | get: function get(id) { |
| 114 | var crmMailingMgr = this; |
| 115 | var mailing; |
| 116 | return qApi('Mailing', 'getsingle', {id: id}) |
| 117 | .then(function (getResult) { |
| 118 | mailing = getResult; |
| 119 | return $q.all([ |
| 120 | crmMailingMgr._loadGroups(mailing), |
| 121 | crmMailingMgr._loadJobs(mailing) |
| 122 | ]); |
| 123 | }) |
| 124 | .then(function () { |
| 125 | return mailing; |
| 126 | }); |
| 127 | }, |
| 128 | // Call MailingGroup.get and merge results into "mailing" |
| 129 | _loadGroups: function (mailing) { |
| 130 | return crmApi('MailingGroup', 'get', {mailing_id: mailing.id}) |
| 131 | .then(function (groupResult) { |
| 132 | mailing.recipients = {}; |
| 133 | mailing.recipients.groups = {include: [], exclude: [], base: []}; |
| 134 | mailing.recipients.mailings = {include: [], exclude: []}; |
| 135 | _.each(groupResult.values, function (mailingGroup) { |
| 136 | var bucket = (/^civicrm_group/.test(mailingGroup.entity_table)) ? 'groups' : 'mailings'; |
| 137 | var entityId = parseInt(mailingGroup.entity_id); |
| 138 | mailing.recipients[bucket][mailingGroup.group_type.toLowerCase()].push(entityId); |
| 139 | }); |
| 140 | }); |
| 141 | }, |
| 142 | // Call MailingJob.get and merge results into "mailing" |
| 143 | _loadJobs: function (mailing) { |
| 144 | return crmApi('MailingJob', 'get', {mailing_id: mailing.id, is_test: 0}) |
| 145 | .then(function (jobResult) { |
| 146 | mailing.jobs = mailing.jobs || {}; |
| 147 | angular.extend(mailing.jobs, jobResult.values); |
| 148 | }); |
| 149 | }, |
| 150 | // @return Object Mailing (per APIv3) |
| 151 | create: function create(params) { |
| 152 | var defaults = { |
| 153 | jobs: {}, // {jobId: JobRecord} |
| 154 | recipients: { |
| 155 | groups: {include: [], exclude: [], base: []}, |
| 156 | mailings: {include: [], exclude: []} |
| 157 | }, |
| 158 | name: "", |
| 159 | campaign_id: null, |
| 160 | replyto_email: "", |
| 161 | subject: "", |
| 162 | body_html: "", |
| 163 | body_text: "" |
| 164 | }; |
| 165 | return angular.extend({}, defaults, params); |
| 166 | }, |
| 167 | |
| 168 | // @param mailing Object (per APIv3) |
| 169 | // @return Promise |
| 170 | 'delete': function (mailing) { |
| 171 | if (mailing.id) { |
| 172 | return qApi('Mailing', 'delete', {id: mailing.id}); |
| 173 | } |
| 174 | else { |
| 175 | var d = $q.defer(); |
| 176 | d.resolve(); |
| 177 | return d.promise; |
| 178 | } |
| 179 | }, |
| 180 | |
| 181 | // Search the body, header, and footer for required tokens. |
| 182 | // ex: var msgs = findMissingTokens(mailing, 'body_html'); |
| 183 | findMissingTokens: function(mailing, field) { |
| 184 | var missing = {}; |
| 185 | if (!_.isEmpty(mailing[field]) && !CRM.crmMailing.disableMandatoryTokensCheck) { |
| 186 | var body = ''; |
| 187 | if (mailing.footer_id) { |
| 188 | var footer = _.where(CRM.crmMailing.headerfooterList, {id: mailing.footer_id}); |
| 189 | body = body + footer[0][field]; |
| 190 | |
| 191 | } |
| 192 | body = body + mailing[field]; |
| 193 | if (mailing.header_id) { |
| 194 | var header = _.where(CRM.crmMailing.headerfooterList, {id: mailing.header_id}); |
| 195 | body = body + header[0][field]; |
| 196 | } |
| 197 | |
| 198 | angular.forEach(CRM.crmMailing.requiredTokens, function(value, token) { |
| 199 | if (!_.isObject(value)) { |
| 200 | if (body.indexOf('{' + token + '}') < 0) { |
| 201 | missing[token] = value; |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | var count = 0; |
| 206 | angular.forEach(value, function(nestedValue, nestedToken) { |
| 207 | if (body.indexOf('{' + nestedToken + '}') >= 0) { |
| 208 | count++; |
| 209 | } |
| 210 | }); |
| 211 | if (count === 0) { |
| 212 | angular.extend(missing, value); |
| 213 | } |
| 214 | } |
| 215 | }); |
| 216 | } |
| 217 | return missing; |
| 218 | }, |
| 219 | |
| 220 | // Copy all data fields in (mailingFrom) to (mailingTgt) -- except for (excludes) |
| 221 | // ex: crmMailingMgr.mergeInto(newMailing, mailingTemplate, ['subject']); |
| 222 | mergeInto: function mergeInto(mailingTgt, mailingFrom, excludes) { |
| 223 | var MAILING_FIELDS = [ |
| 224 | // always exclude: 'id' |
| 225 | 'name', |
| 226 | 'campaign_id', |
| 227 | 'from_name', |
| 228 | 'from_email', |
| 229 | 'replyto_email', |
| 230 | 'subject', |
| 231 | 'dedupe_email', |
| 232 | 'recipients', |
| 233 | 'body_html', |
| 234 | 'body_text', |
| 235 | 'footer_id', |
| 236 | 'header_id', |
| 237 | 'visibility', |
| 238 | 'url_tracking', |
| 239 | 'dedupe_email', |
| 240 | 'forward_replies', |
| 241 | 'auto_responder', |
| 242 | 'open_tracking', |
| 243 | 'override_verp', |
| 244 | 'optout_id', |
| 245 | 'reply_id', |
| 246 | 'resubscribe_id', |
| 247 | 'unsubscribe_id' |
| 248 | ]; |
| 249 | if (!excludes) { |
| 250 | excludes = []; |
| 251 | } |
| 252 | _.each(MAILING_FIELDS, function (field) { |
| 253 | if (!_.contains(excludes, field)) { |
| 254 | mailingTgt[field] = mailingFrom[field]; |
| 255 | } |
| 256 | }); |
| 257 | }, |
| 258 | |
| 259 | // @param mailing Object (per APIv3) |
| 260 | // @return Promise an object with "subject", "body_text", "body_html" |
| 261 | preview: function preview(mailing) { |
| 262 | if (CRM.crmMailing.workflowEnabled && !CRM.checkPerm('create mailings') && !CRM.checkPerm('access CiviMail')) { |
| 263 | return qApi('Mailing', 'preview', {id: mailing.id}).then(function(result) { |
| 264 | return result.values; |
| 265 | }); |
| 266 | } |
| 267 | else { |
| 268 | // Protect against races in saving and previewing by chaining create+preview. |
| 269 | var params = angular.extend({}, mailing, mailing.recipients, { |
| 270 | options: {force_rollback: 1}, |
| 271 | 'api.Mailing.preview': { |
| 272 | id: '$value.id' |
| 273 | } |
| 274 | }); |
| 275 | delete params.recipients; // the content was merged in |
| 276 | return qApi('Mailing', 'create', params).then(function(result) { |
| 277 | // changes rolled back, so we don't care about updating mailing |
| 278 | return result.values[result.id]['api.Mailing.preview'].values; |
| 279 | }); |
| 280 | } |
| 281 | }, |
| 282 | |
| 283 | // @param mailing Object (per APIv3) |
| 284 | // @param int previewLimit |
| 285 | // @return Promise for a list of recipients (mailing_id, contact_id, api.contact.getvalue, api.email.getvalue) |
| 286 | previewRecipients: function previewRecipients(mailing, previewLimit) { |
| 287 | // To get list of recipients, we tentatively save the mailing and |
| 288 | // get the resulting recipients -- then rollback any changes. |
| 289 | var params = angular.extend({}, mailing, mailing.recipients, { |
| 290 | name: 'placeholder', // for previewing recipients on new, incomplete mailing |
| 291 | subject: 'placeholder', // for previewing recipients on new, incomplete mailing |
| 292 | options: {force_rollback: 1}, |
| 293 | 'api.mailing_job.create': 1, // note: exact match to API default |
| 294 | 'api.MailingRecipients.get': { |
| 295 | mailing_id: '$value.id', |
| 296 | options: {limit: previewLimit}, |
| 297 | 'api.contact.getvalue': {'return': 'display_name'}, |
| 298 | 'api.email.getvalue': {'return': 'email'} |
| 299 | } |
| 300 | }); |
| 301 | delete params.recipients; // the content was merged in |
| 302 | return qApi('Mailing', 'create', params).then(function (recipResult) { |
| 303 | // changes rolled back, so we don't care about updating mailing |
| 304 | return recipResult.values[recipResult.id]['api.MailingRecipients.get'].values; |
| 305 | }); |
| 306 | }, |
| 307 | |
| 308 | previewRecipientCount: function previewRecipientCount(mailing) { |
| 309 | // To get list of recipients, we tentatively save the mailing and |
| 310 | // get the resulting recipients -- then rollback any changes. |
| 311 | var params = angular.extend({}, mailing, mailing.recipients, { |
| 312 | name: 'placeholder', // for previewing recipients on new, incomplete mailing |
| 313 | subject: 'placeholder', // for previewing recipients on new, incomplete mailing |
| 314 | options: {force_rollback: 1}, |
| 315 | 'api.mailing_job.create': 1, // note: exact match to API default |
| 316 | 'api.MailingRecipients.getcount': { |
| 317 | mailing_id: '$value.id' |
| 318 | } |
| 319 | }); |
| 320 | delete params.recipients; // the content was merged in |
| 321 | return qApi('Mailing', 'create', params).then(function (recipResult) { |
| 322 | // changes rolled back, so we don't care about updating mailing |
| 323 | return recipResult.values[recipResult.id]['api.MailingRecipients.getcount']; |
| 324 | }); |
| 325 | }, |
| 326 | |
| 327 | // Save a (draft) mailing |
| 328 | // @param mailing Object (per APIv3) |
| 329 | // @return Promise |
| 330 | save: function(mailing) { |
| 331 | var params = angular.extend({}, mailing, mailing.recipients); |
| 332 | |
| 333 | // Angular ngModel sometimes treats blank fields as undefined. |
| 334 | angular.forEach(mailing, function(value, key) { |
| 335 | if (value === undefined || value === null) { |
| 336 | mailing[key] = ''; |
| 337 | } |
| 338 | }); |
| 339 | |
| 340 | // WORKAROUND: Mailing.create (aka CRM_Mailing_BAO_Mailing::create()) interprets scheduled_date |
| 341 | // as an *intent* to schedule and creates tertiary records. Saving a draft with a scheduled_date |
| 342 | // is therefore not allowed. Remove this after fixing Mailing.create's contract. |
| 343 | delete params.scheduled_date; |
| 344 | |
| 345 | delete params.jobs; |
| 346 | |
| 347 | delete params.recipients; // the content was merged in |
| 348 | |
| 349 | return qApi('Mailing', 'create', params).then(function(result) { |
| 350 | if (result.id && !mailing.id) { |
| 351 | mailing.id = result.id; |
| 352 | } // no rollback, so update mailing.id |
| 353 | // Perhaps we should reload mailing based on result? |
| 354 | return mailing; |
| 355 | }); |
| 356 | }, |
| 357 | |
| 358 | // Schedule/send the mailing |
| 359 | // @param mailing Object (per APIv3) |
| 360 | // @return Promise |
| 361 | submit: function (mailing) { |
| 362 | var crmMailingMgr = this; |
| 363 | var params = { |
| 364 | id: mailing.id, |
| 365 | approval_date: 'now', |
| 366 | scheduled_date: mailing.scheduled_date ? mailing.scheduled_date : 'now' |
| 367 | }; |
| 368 | return qApi('Mailing', 'submit', params) |
| 369 | .then(function (result) { |
| 370 | angular.extend(mailing, result.values[result.id]); // Perhaps we should reload mailing based on result? |
| 371 | return crmMailingMgr._loadJobs(mailing); |
| 372 | }) |
| 373 | .then(function () { |
| 374 | return mailing; |
| 375 | }); |
| 376 | }, |
| 377 | |
| 378 | // Immediately send a test message |
| 379 | // @param mailing Object (per APIv3) |
| 380 | // @param to Object with either key "email" (string) or "gid" (int) |
| 381 | // @return Promise for a list of delivery reports |
| 382 | sendTest: function (mailing, recipient) { |
| 383 | var params = angular.extend({}, mailing, mailing.recipients, { |
| 384 | // options: {force_rollback: 1}, // Test mailings include tracking features, so the mailing must be persistent |
| 385 | 'api.Mailing.send_test': { |
| 386 | mailing_id: '$value.id', |
| 387 | test_email: recipient.email, |
| 388 | test_group: recipient.gid |
| 389 | } |
| 390 | }); |
| 391 | |
| 392 | // WORKAROUND: Mailing.create (aka CRM_Mailing_BAO_Mailing::create()) interprets scheduled_date |
| 393 | // as an *intent* to schedule and creates tertiary records. Saving a draft with a scheduled_date |
| 394 | // is therefore not allowed. Remove this after fixing Mailing.create's contract. |
| 395 | delete params.scheduled_date; |
| 396 | |
| 397 | delete params.jobs; |
| 398 | |
| 399 | delete params.recipients; // the content was merged in |
| 400 | |
| 401 | return qApi('Mailing', 'create', params).then(function (result) { |
| 402 | if (result.id && !mailing.id) { |
| 403 | mailing.id = result.id; |
| 404 | } // no rollback, so update mailing.id |
| 405 | return result.values[result.id]['api.Mailing.send_test'].values; |
| 406 | }); |
| 407 | } |
| 408 | }; |
| 409 | }); |
| 410 | |
| 411 | // The preview manager performs preview actions while putting up a visible UI (e.g. dialogs & status alerts) |
| 412 | angular.module('crmMailing').factory('crmMailingPreviewMgr', function (dialogService, crmMailingMgr, crmStatus) { |
| 413 | return { |
| 414 | // @param mode string one of 'html', 'text', or 'full' |
| 415 | // @return Promise |
| 416 | preview: function preview(mailing, mode) { |
| 417 | var templates = { |
| 418 | html: '~/crmMailing/PreviewMgr/html.html', |
| 419 | text: '~/crmMailing/PreviewMgr/text.html', |
| 420 | full: '~/crmMailing/PreviewMgr/full.html' |
| 421 | }; |
| 422 | var result = null; |
| 423 | var p = crmMailingMgr |
| 424 | .preview(mailing) |
| 425 | .then(function (content) { |
| 426 | var options = CRM.utils.adjustDialogDefaults({ |
| 427 | autoOpen: false, |
| 428 | title: ts('Subject: %1', { |
| 429 | 1: content.subject |
| 430 | }) |
| 431 | }); |
| 432 | result = dialogService.open('previewDialog', templates[mode], content, options); |
| 433 | }); |
| 434 | crmStatus({start: ts('Previewing...'), success: ''}, p); |
| 435 | return result; |
| 436 | }, |
| 437 | |
| 438 | // @param to Object with either key "email" (string) or "gid" (int) |
| 439 | // @return Promise |
| 440 | sendTest: function sendTest(mailing, recipient) { |
| 441 | var promise = crmMailingMgr.sendTest(mailing, recipient) |
| 442 | .then(function (deliveryInfos) { |
| 443 | var count = Object.keys(deliveryInfos).length; |
| 444 | if (count === 0) { |
| 445 | CRM.alert(ts('Could not identify any recipients. Perhaps the group is empty?')); |
| 446 | } |
| 447 | }) |
| 448 | ; |
| 449 | return crmStatus({start: ts('Sending...'), success: ts('Sent')}, promise); |
| 450 | } |
| 451 | }; |
| 452 | }); |
| 453 | |
| 454 | angular.module('crmMailing').factory('crmMailingStats', function (crmApi, crmLegacy) { |
| 455 | var statTypes = [ |
| 456 | // {name: 'Recipients', title: ts('Intended Recipients'), searchFilter: '', eventsFilter: '&event=queue'}, |
| 457 | {name: 'Delivered', title: ts('Successful Deliveries'), searchFilter: '&mailing_delivery_status=Y', eventsFilter: '&event=delivered'}, |
| 458 | {name: 'Opened', title: ts('Tracked Opens'), searchFilter: '&mailing_open_status=Y', eventsFilter: '&event=opened'}, |
| 459 | {name: 'Unique Clicks', title: ts('Click-throughs'), searchFilter: '&mailing_click_status=Y', eventsFilter: '&event=click&distinct=1'}, |
| 460 | // {name: 'Forward', title: ts('Forwards'), searchFilter: '&mailing_forward=1', eventsFilter: '&event=forward'}, |
| 461 | // {name: 'Replies', title: ts('Replies'), searchFilter: '&mailing_reply_status=Y', eventsFilter: '&event=reply'}, |
| 462 | {name: 'Bounces', title: ts('Bounces'), searchFilter: '&mailing_delivery_status=N', eventsFilter: '&event=bounce'}, |
| 463 | {name: 'Unsubscribers', title: ts('Unsubscribes'), searchFilter: '&mailing_unsubscribe=1', eventsFilter: '&event=unsubscribe'} |
| 464 | // {name: 'OptOuts', title: ts('Opt-Outs'), searchFilter: '&mailing_optout=1', eventsFilter: '&event=optout'} |
| 465 | ]; |
| 466 | |
| 467 | return { |
| 468 | getStatTypes: function() { |
| 469 | return statTypes; |
| 470 | }, |
| 471 | |
| 472 | /** |
| 473 | * @param mailingIds object |
| 474 | * List of mailing IDs ({a: 123, b: 456}) |
| 475 | * @return Promise |
| 476 | * List of stats for each mailing |
| 477 | * ({a: ...object..., b: ...object...}) |
| 478 | */ |
| 479 | getStats: function(mailingIds) { |
| 480 | var params = {}; |
| 481 | angular.forEach(mailingIds, function(mailingId, name) { |
| 482 | params[name] = ['Mailing', 'stats', {mailing_id: mailingId}]; |
| 483 | }); |
| 484 | return crmApi(params).then(function(result) { |
| 485 | var stats = {}; |
| 486 | angular.forEach(mailingIds, function(mailingId, name) { |
| 487 | stats[name] = result[name].values[mailingId]; |
| 488 | }); |
| 489 | return stats; |
| 490 | }); |
| 491 | }, |
| 492 | |
| 493 | /** |
| 494 | * Determine the legacy URL for a report about a given mailing and stat. |
| 495 | * |
| 496 | * @param mailing object |
| 497 | * @param statType object (see statTypes above) |
| 498 | * @param view string ('search', 'event', 'report') |
| 499 | * @param returnPath string|null Return path (relative to Angular base) |
| 500 | * @return string|null |
| 501 | */ |
| 502 | getUrl: function getUrl(mailing, statType, view, returnPath) { |
| 503 | switch (view) { |
| 504 | case 'events': |
| 505 | var retParams = returnPath ? '&context=angPage&angPage=' + returnPath : ''; |
| 506 | return crmLegacy.url('civicrm/mailing/report/event', |
| 507 | 'reset=1&mid=' + mailing.id + statType.eventsFilter + retParams); |
| 508 | |
| 509 | case 'search': |
| 510 | return crmLegacy.url('civicrm/contact/search/advanced', |
| 511 | 'force=1&mailing_id=' + mailing.id + statType.searchFilter); |
| 512 | |
| 513 | // TODO: case 'report': |
| 514 | default: |
| 515 | return null; |
| 516 | } |
| 517 | } |
| 518 | }; |
| 519 | }); |
| 520 | |
| 521 | // crmMailingSimpleDirective is a template/factory-function for constructing very basic |
| 522 | // directives that accept a "mailing" argument. Please don't overload it. If one continues building |
| 523 | // this, it risks becoming a second system that violates Angular architecture (and prevents one |
| 524 | // from using standard Angular docs+plugins). So this really shouldn't do much -- it is really |
| 525 | // only for simple directives. For something complex, suck it up and write 10 lines of boilerplate. |
| 526 | angular.module('crmMailing').factory('crmMailingSimpleDirective', function ($q, crmMetadata, crmUiHelp) { |
| 527 | return function crmMailingSimpleDirective(directiveName, templateUrl) { |
| 528 | return { |
| 529 | scope: { |
| 530 | crmMailing: '@' |
| 531 | }, |
| 532 | templateUrl: templateUrl, |
| 533 | link: function (scope, elm, attr) { |
| 534 | scope.$parent.$watch(attr.crmMailing, function(newValue){ |
| 535 | scope.mailing = newValue; |
| 536 | }); |
| 537 | scope.crmMailingConst = CRM.crmMailing; |
| 538 | scope.ts = CRM.ts(null); |
| 539 | scope.hs = crmUiHelp({file: 'CRM/Mailing/MailingUI'}); |
| 540 | scope[directiveName] = attr[directiveName] ? scope.$parent.$eval(attr[directiveName]) : {}; |
| 541 | $q.when(crmMetadata.getFields('Mailing'), function(fields) { |
| 542 | scope.mailingFields = fields; |
| 543 | }); |
| 544 | } |
| 545 | }; |
| 546 | }; |
| 547 | }); |
| 548 | |
| 549 | })(angular, CRM.$, CRM._); |