{"action":"created","issue":{"url":"https://api.github.com/repos/compucorp/civicrm-core/issues/15","repository_url":"https://api.github.com/repos/compucorp/civicrm-core","labels_url":"https://api.github.com/repos/compucorp/civicrm-core/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/compucorp/civicrm-core/issues/15/comments","events_url":"https://api.github.com/repos/compucorp/civicrm-core/issues/15/events","html_url":"https://github.com/compucorp/civicrm-core/pull/15","id":313914342,"number":15,"title":"Pchr 3531 add category column to case types","user":{"login":"reneolivo","id":1642119,"avatar_url":"https://avatars1.githubusercontent.com/u/1642119?v=4","gravatar_id":"","url":"https://api.github.com/users/reneolivo","html_url":"https://github.com/reneolivo","followers_url":"https://api.github.com/users/reneolivo/followers","following_url":"https://api.github.com/users/reneolivo/following{/other_user}","gists_url":"https://api.github.com/users/reneolivo/gists{/gist_id}","starred_url":"https://api.github.com/users/reneolivo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/reneolivo/subscriptions","organizations_url":"https://api.github.com/users/reneolivo/orgs","repos_url":"https://api.github.com/users/reneolivo/repos","events_url":"https://api.github.com/users/reneolivo/events{/privacy}","received_events_url":"https://api.github.com/users/reneolivo/received_events","type":"User","site_admin":false},"labels":[{"id":126065666,"url":"https://api.github.com/repos/compucorp/civicrm-core/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-04-12T22:52:43Z","updated_at":"2018-04-13T06:54:31Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/compucorp/civicrm-core/pulls/15","html_url":"https://github.com/compucorp/civicrm-core/pull/15","diff_url":"https://github.com/compucorp/civicrm-core/pull/15.diff","patch_url":"https://github.com/compucorp/civicrm-core/pull/15.patch"},"body":"Overview\r\n----------------------------------------\r\nThis PR adds a \"category\" column to case types. For the \"Application\" case type the category is \"Vacancy\" and for all other case types it's \"Workflow\".\r\n\r\nThis category column will be useful in the future in order to separate the administration of vacancy and workflow case types.\r\n\r\nBefore\r\n----------------------------------------\r\n![anim](https://user-images.githubusercontent.com/1642119/38708358-efee2c30-3e83-11e8-9108-7adc6337874d.gif)\r\n\r\nAfter\r\n----------------------------------------\r\n![anim](https://user-images.githubusercontent.com/1642119/38708259-62307786-3e83-11e8-9a02-ca7a9ba9f4e6.gif)\r\n\r\nTechnical Details\r\n----------------------------------------\r\n\r\n### Adding the Vacancy and Workflow categories\r\n\r\nA new upgrader was added:\r\n\r\n```php\r\npublic function upgrade_5_0_2($rev) {\r\n  $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);\r\n\r\n  $this->_addCategoryColumnToCaseType();\r\n  $this->_createCaseTypeCategories();\r\n  $this->_setDefaultCategoriesForCaseTypes();\r\n}\r\n\r\nprotected function _addCategoryColumnToCaseType() {\r\n  // could not add this to the 5.0.2.mysql.tpl file because it seems the result is not ready by the\r\n  // time the PHP script runs.\r\n  CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_case_type\r\n    ADD COLUMN category VARCHAR(512)');\r\n}\r\n\r\nprotected function _createCaseTypeCategories() {\r\n  // creates the case type category group:\r\n  CRM_Core_BAO_OptionGroup::ensureOptionGroupExists([\r\n    'name' => 'case_type_category',\r\n    'title' => ts('Case Type Category'),\r\n    'is_reserved' => 1,\r\n  ]);\r\n\r\n  CRM_Core_PseudoConstant::flush();\r\n\r\n  $options = [\r\n    ['name' => 'WORKFLOW', 'label' => ts('Workflow')],\r\n    ['name' => 'VACANCY', 'label' => ts('Vacancy')]\r\n  ];\r\n\r\n  foreach ($options as $option) {\r\n    // creates the workflow and vacancy option values:\r\n    CRM_Core_BAO_OptionValue::ensureOptionValueExists([\r\n      'option_group_id' => 'case_type_category',\r\n      'name' => $option['name'],\r\n      'label' => $option['label'],\r\n      'is_active' => TRUE,\r\n      'is_reserved'=> TRUE\r\n    ]);\r\n  }\r\n}\r\n\r\nprotected function _setDefaultCategoriesForCaseTypes() {\r\n  $caseTypes = civicrm_api3('CaseType', 'get', [\r\n    'options' => [ 'limit' => 0 ]\r\n  ]);\r\n\r\n  foreach ($caseTypes['values'] as $caseType) {\r\n    $category = $caseType['name'] === 'Application' ? 'VACANCY' : 'WORKFLOW';\r\n\r\n    // updates all current case types. By default they have the \"Workflow\" category, except for the\r\n    // Application case type, which has the \"Vacancy\" category:\r\n    civicrm_api3('CaseType', 'create', [\r\n      'id' => $caseType['id'],\r\n      'category' => $category\r\n    ]);\r\n  }\r\n}\r\n```\r\n\r\n### Updating the Case Type Schema and DAO\r\n\r\nThe Case Type Schema was updated so it includes the new category field and references the `case_type_category` pseudo constant (option value).\r\n\r\n```xml\r\n<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<table>\r\n  <name>civicrm_case_type</name>\r\n  <!-- ... -->\r\n  <field>\r\n    <name>category</name>\r\n    <title>Category</title>\r\n    <type>int unsigned</type>\r\n    <required>true</required>\r\n    <add>5</add>\r\n    <comment>FK to a civicrm_option_value (case_type_category)</comment>\r\n    <pseudoconstant>\r\n      <optionGroupName>case_type_category</optionGroupName>\r\n    </pseudoconstant>\r\n    <html>\r\n      <type>Select</type>\r\n    </html>\r\n  </field>\r\n  <!-- ... -->\r\n</table>\r\n```\r\n\r\nThe DAO file was updated by running `php GenCode.php` at the `xml` folder.\r\n\r\n### Displaying the category at the case type administration screen\r\n\r\nThe list of case type categories is provided to the `CaseTypeListCtrl`:\r\n\r\n```js\r\n$routeProvider.when('/caseType', {\r\n  templateUrl: '~/crmCaseType/list.html',\r\n  controller: 'CaseTypeListCtrl',\r\n  resolve: {\r\n    // ..\r\n    caseTypeCategories: function (crmApi) {\r\n      return crmApi('OptionValue', 'get', {options: {limit: 0}})\r\n    }\r\n  }\r\n});\r\n```\r\n\r\nThe result is indexed by value:\r\n\r\n```js\r\ncrmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes, caseTypeCategories) {\r\n  // ...\r\n  $scope.caseTypeCategoriesIndex = _.indexBy(caseTypeCategories.values, 'value');\r\n  // ...\r\n});\r\n```\r\n\r\nAnd the category label is displayed on the view:\r\n\r\n```html\r\n<td>{{caseTypeCategoriesIndex[caseType.category].label}}</td>\r\n```\r\n\r\n### Case Type Category Field\r\n\r\nThe case type categories were passed in a similar fashion to `CaseTypeCtrl`.\r\n\r\nTo display the options the following was done to the `caseTypeDetails.html` file:\r\n\r\n```html\r\n<div crm-ui-field=\"{name: 'caseTypeDetailForm.category', title: ts('Category')}\">\r\n  <label ng-repeat=\"category in caseTypeCategories | orderBy:'+weight'\">\r\n    <input type=\"radio\" name=\"category\" value=\"{{category.value}}\" ng-model=\"caseType.category\" />\r\n    <span>{{category.label}}</span>\r\n  </label>\r\n</div>\r\n```\r\n\r\nFor new case types the Workflow category is automatically selected:\r\n\r\n```js\r\nfunction initCaseType() {\r\n  if (apiCalls.caseType) {\r\n    // edit case type\r\n    $scope.caseType = apiCalls.caseType;\r\n  } else {\r\n    // new case type\r\n    var defaultCategory = _.find($scope.caseTypeCategories, { name: 'WORKFLOW' }) || {};\r\n\r\n    $scope.caseType = _.cloneDeep(newCaseTypeTemplate);\r\n    $scope.caseType.category = defaultCategory.value; // <--- here\r\n    // ...\r\n  }\r\n}\r\n```\r\n\r\n\r\nComments\r\n----------------------------------------\r\nPlease take into consideration that the code style of CiviCRM is being considered. This mean the Angular Styleguide is not being followed.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/compucorp/civicrm-core/issues/comments/381041957","html_url":"https://github.com/compucorp/civicrm-core/pull/15#issuecomment-381041957","issue_url":"https://api.github.com/repos/compucorp/civicrm-core/issues/15","id":381041957,"user":{"login":"deb1990","id":5058867,"avatar_url":"https://avatars1.githubusercontent.com/u/5058867?v=4","gravatar_id":"","url":"https://api.github.com/users/deb1990","html_url":"https://github.com/deb1990","followers_url":"https://api.github.com/users/deb1990/followers","following_url":"https://api.github.com/users/deb1990/following{/other_user}","gists_url":"https://api.github.com/users/deb1990/gists{/gist_id}","starred_url":"https://api.github.com/users/deb1990/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deb1990/subscriptions","organizations_url":"https://api.github.com/users/deb1990/orgs","repos_url":"https://api.github.com/users/deb1990/repos","events_url":"https://api.github.com/users/deb1990/events{/privacy}","received_events_url":"https://api.github.com/users/deb1990/received_events","type":"User","site_admin":false},"created_at":"2018-04-13T06:54:31Z","updated_at":"2018-04-13T06:54:31Z","author_association":"NONE","body":"@reneolivo Should not we send a PR to the civicrm core also? Or 3369 will be sent at once?"}}
{"actor":{"display_login":"deb1990"}}