Skip to content

Commit

Permalink
Implement civicrm/timetrack/import (#9)
Browse files Browse the repository at this point in the history
This addresses the import scenario for #9.  It includes live-update, so that
you can fix syntax errors and data-errors while watching the preview of the
proposed import.
  • Loading branch information
totten authored and Aegir user committed Feb 28, 2019
1 parent 52bae4d commit 98291e4
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 0 deletions.
19 changes: 19 additions & 0 deletions CRM/Timetrack/Page/Import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
use CRM_Timetrack_ExtensionUtil as E;

class CRM_Timetrack_Page_Import extends CRM_Core_Page {


public function run() {
$loader = new \Civi\Angular\AngularLoader();
$loader->setModules(array('timetrackImport'));
$loader->setPageName('civicrm/timetrack/import');
$loader->useApp(array(
'defaultRoute' => '/import',
));
$loader->load();
CRM_Utils_System::setTitle('CiviCRM');
parent::run();
}

}
17 changes: 17 additions & 0 deletions ang/timetrackImport.ang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
// This file declares an Angular module which can be autoloaded
// in CiviCRM. See also:
// http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules

return array(
'js' => array(
'ang/timetrackImport.js',
'ang/timetrackImport/*.js',
'ang/timetrackImport/*/*.js',
),
'css' => ['ang/timetrackImport.css'],
'partials' => ['ang/timetrackImport'],
'requires' => ['crmUi', 'crmUtil', 'ngRoute'],
'settings' => [],
'basePages' => [],
);
1 change: 1 addition & 0 deletions ang/timetrackImport.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Add any CSS rules for Angular module "timetrackImport" */
4 changes: 4 additions & 0 deletions ang/timetrackImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(function(angular, $, _) {
// Declare a list of dependencies.
angular.module('timetrackImport', CRM.angRequires('timetrackImport'));
})(angular, CRM.$, CRM._);
76 changes: 76 additions & 0 deletions ang/timetrackImport/ImportCtrl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<div class="crm-container">
<h1 crm-page-title>{{ts('Timetrack: Personal import')}}</h1>

<div crm-ui-debug="importModel"></div>

<form name="timetrackImportForm" crm-ui-id-scope>

<div class="help">
<p><em>{{ts('Import a series of time-punches.')}}</em></p>

<pre>
<u>{{ts('Format:')}}</u>
!pi -s [&lt;yyyy-mm-dd&gt;] &lt;hh:ss&gt;+&lt;duration&gt; &lt;alias/task&gt; &lt;comment&gt;

<u>{{ts('Examples:')}}</u>
!pi -s 2019-02-03 12:34+1h alias/cat1 make the foo
!pi -s 11:00+1.5h alias/cat2 make the foobar
!pi -s 20:00+30m alias/cat3 make the foobarwhiz</pre>
</div>


<div crm-ui-accordion="{title: ts('Import')}">
<textarea ng-model="importModel.plaintext" style="width: 90%; height: 10em;"></textarea>
</div>

<div crm-ui-accordion="{title: ts('Errors')}" ng-show="importModel.errors.length > 0">
<table>
<thead>
<tr>
<th>{{ts('Line No')}}</th>
<th>{{ts('Content')}}</th>
<th>{{ts('Error')}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="punch in importModel.errors">
<td class="error">{{punch.lineNo}}</td>
<td class="error">{{punch.line}}</td>
<td class="error">{{punch.message}}</td>
</tr>
</tbody>
</table>
</div>

<div crm-ui-accordion="{title: ts('Preview')}" ng-show="importModel.punches.length > 0">
<table>
<thead>
<tr>
<th>{{ts('Contact ID')}}</th>
<th>{{ts('Start Time')}}</th>
<th>{{ts('Duration')}}</th>
<th>{{ts('Task Alias')}}</th>
<th>{{ts('Comment')}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="punch in importModel.punches">
<td>{{punch.contact_id}}</td>
<td>{{punch.begin}}</td>
<td>{{punch.duration / 60 | number:0}} min</td>
<td>{{punch.alias}}</td>
<td>{{punch.comment}}</td>
</tr>
</tbody>
</table>
</div>

<br/>

<div>
<button ng-click="submit()" ng-disabled="importModel.punches.length == 0 || importModel.errors.length > 0">{{ts('Import')}}</button>
</div>

</form>

</div>
68 changes: 68 additions & 0 deletions ang/timetrackImport/ImportCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(function(angular, $, _) {

angular.module('timetrackImport').config(function($routeProvider) {
$routeProvider.when('/import', {
controller: 'TimetrackImportImportCtrl',
templateUrl: '~/timetrackImport/ImportCtrl.html',
resolve: {}
});
}
);

angular.module('timetrackImport').controller('TimetrackImportImportCtrl', function($scope, crmApi, crmStatus, crmUiHelp, crmUiAlert) {
// The ts() and hs() functions help load strings for this module.
var ts = $scope.ts = CRM.ts('timetrack');
var hs = $scope.hs = crmUiHelp({file: 'CRM/timetrackImport/ImportCtrl'}); // See: templates/CRM/timetrackImport/ImportCtrl.hlp

function newModel() {
return {
plaintext: '',
punches: [],
errors: []
};
}

$scope.importModel = newModel();

function debounce(time, func) {
return _.debounce(function(){
var a = arguments;
$scope.$evalAsync(function(){
func.apply(a);
});
}, time);
}

$scope.updatePreview = debounce(100, function(){
crmApi('Timetrackpunchlist', 'preview', {
text: $scope.importModel.plaintext
}).then(function(apiResult){
console.log('rx ' + new Date(), apiResult);
$scope.importModel.punches = _.filter(apiResult.values, function(punch){ return !punch.error;});
$scope.importModel.errors = _.filter(apiResult.values, function(punch){ return !!punch.error;});
});
});

$scope.$watch('importModel.plaintext', function(plainText){
$scope.updatePreview();
});

$scope.submit = function submit() {
return crmStatus(
// Status messages. For defaults, just use "{}"
{start: ts('Importing...'), success: ts('Imported')},
// The save action. Note that crmApi() returns a promise.
crmApi('Timetrackpunchlist', 'import', {
text: $scope.importModel.plaintext
})
.then(function(apiResult){
$scope.lastImport = apiResult.values;

crmUiAlert({title: ts('Imported'), text: apiResult.values.length + ' record(s)', type: 'success'});
$scope.importModel = newModel();
})
);
};
});

})(angular, CRM.$, CRM._);
1 change: 1 addition & 0 deletions templates/CRM/Timetrack/Page/Import.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{* placeholder *}
1 change: 1 addition & 0 deletions templates/CRM/timetrackImport/ImportCtrl.hlp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{* placeholder *}
6 changes: 6 additions & 0 deletions xml/Menu/timetrack.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@
<title>Gitlab</title>
<access_callback>1</access_callback>
</item>
<item>
<path>civicrm/timetrack/import</path>
<page_callback>CRM_Timetrack_Page_Import</page_callback>
<title>Import</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
</menu>

0 comments on commit 98291e4

Please sign in to comment.