dev/core#55: Hide "Add Option" for locked custom field option groups
authorMichael Devery <michael@compucorp.co.uk>
Mon, 9 Apr 2018 11:33:50 +0000 (12:33 +0100)
committerMichael Devery <michael@compucorp.co.uk>
Mon, 9 Apr 2018 12:38:28 +0000 (13:38 +0100)
CRM/Custom/Page/Option.php
templates/CRM/Custom/Page/Option.tpl

index 6e4b6e8ec059b738962759015dfb183428edac1d..8766e2cb04275e3ae417641d41929c07f2af93f0 100644 (file)
@@ -232,6 +232,11 @@ WHERE  option_group_id = %1";
       CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
     }
 
+    $optionGroupId = $this->getOptionGroupId($this->_fid);
+    $isOptionGroupLocked = $optionGroupId ? $this->isOptionGroupLocked($optionGroupId) : FALSE;
+    $this->assign('optionGroupId', $optionGroupId);
+    $this->assign('isOptionGroupLocked', $isOptionGroupLocked);
+
     //as url contain $gid so append breadcrumb dynamically.
     $breadcrumb = array(
       array(
@@ -281,4 +286,34 @@ WHERE  option_group_id = %1";
     return parent::run();
   }
 
+  /**
+   * Gets the "is_locked" status for the provided option group
+   *
+   * @param int $optionGroupId
+   *
+   * @return bool
+   */
+  private function isOptionGroupLocked($optionGroupId) {
+    return (bool) CRM_Core_DAO::getFieldValue(
+      CRM_Core_DAO_OptionGroup::class,
+      $optionGroupId,
+      'is_locked'
+    );
+  }
+
+  /**
+   * Gets the associated "option_group_id" for a custom field
+   *
+   * @param int $customFieldId
+   *
+   * @return int
+   */
+  private function getOptionGroupId($customFieldId) {
+    return (int) CRM_Core_DAO::getFieldValue(
+      CRM_Core_DAO_CustomField::class,
+      $customFieldId,
+      'option_group_id'
+    );
+  }
+
 }
index 2322e6a3d83b933b9392867be05b6d20a78b6b5d..fa99b71fe02d7002011663c8cf0b02f1d2d659f0 100644 (file)
 
       <div class="action-link">
           {crmButton q="reset=1&action=map&fid=$fid&gid=$gid" class="action-item open-inline-noreturn" icon="sort-alpha-asc"}{ts}Alphabetize Options{/ts}{/crmButton}
-          {crmButton q="reset=1&action=add&fid=$fid&gid=$gid" class="action-item" icon="plus-circle"}{ts}Add Option{/ts}{/crmButton}
+          {if !$isOptionGroupLocked}
+            {crmButton q="reset=1&action=add&fid=$fid&gid=$gid" class="action-item" icon="plus-circle"}{ts}Add Option{/ts}{/crmButton}
+          {/if}
           {crmButton p="civicrm/admin/custom/group/field" q="reset=1&action=browse&gid=$gid" class="action-item cancel" icon="times"}{ts}Done{/ts}{/crmButton}
       </div>
     </div>