-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement civicrm/timetrack/import (#9)
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
Showing
9 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => [], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Add any CSS rules for Angular module "timetrackImport" */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [<yyyy-mm-dd>] <hh:ss>+<duration> <alias/task> <comment> | ||
|
||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{* placeholder *} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{* placeholder *} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters