CRM-16211 fix - 'Activity Subject' not filled in when creating PDF letters from template
[civicrm-core.git] / js / angular-crm-util.js
index 2349fcff97c472730b277d5b6fe75d44ca104b1f..f64292762bdb1f21d01a01e6aeacd5375c599224 100644 (file)
   //     console.log('The fields are:', options);
   //   });
   angular.module('crmUtil').factory('crmMetadata', function($q, crmApi) {
+
+    // Convert {key:$,value:$} sequence to unordered {$key: $value} map.
+    function convertOptionsToMap(options) {
+      var result = {};
+      angular.forEach(options, function(o) {
+        result[o.key] = o.value;
+      });
+      return result;
+    }
+
     var cache = {}; // cache[entityName+'::'+action][fieldName].title
     var deferreds = {}; // deferreds[cacheKey].push($q.defer())
     var crmMetadata = {
         deferreds[cacheKey].push(deferred);
 
         if (needFetch) {
-          crmApi(entity, 'getfields', {action: action, options: {get_options: 'all'}})
+          crmApi(entity, 'getfields', {action: action, sequential: 1, options: {get_options: 'all'}})
             .then(
             // on success:
             function(fields) {
-              cache[cacheKey] = fields.values;
+              cache[cacheKey] = _.indexBy(fields.values, 'name');
+              angular.forEach(cache[cacheKey],function (field){
+                if (field.options) {
+                  field.optionsMap = convertOptionsToMap(field.options);
+                }
+              });
               angular.forEach(deferreds[cacheKey], function(dfr) {
-                dfr.resolve(fields.values);
+                dfr.resolve(cache[cacheKey]);
               });
               delete deferreds[cacheKey];
             },
     };
   }]);
 
-  angular.module('crmUtil').factory('crmNow', function($q){
-    // FIXME: surely there's already some helper which can do this in one line?
-    // @return string "YYYY-MM-DD hh:mm:ss"
-    return function crmNow() {
-      var currentdate = new Date();
-      var yyyy = currentdate.getFullYear();
-      var mm = currentdate.getMonth() + 1;
-      mm = mm < 10 ? '0' + mm : mm;
-      var dd = currentdate.getDate();
-      dd = dd < 10 ? '0' + dd : dd;
-      var hh = currentdate.getHours();
-      hh = hh < 10 ? '0' + hh : hh;
-      var min = currentdate.getMinutes();
-      min = min < 10 ? '0' + min : min;
-      var sec = currentdate.getSeconds();
-      sec = sec < 10 ? '0' + sec : sec;
-      return yyyy + "-" + mm + "-" + dd + " " + hh + ":" + min + ":" + sec;
-    };
-  });
-
   // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
   // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
   angular.module('crmUtil').factory('crmStatus', function($q){