label configuration form fixes, CRM-12965
authorKurund Jalmi <kurund@civicrm.org>
Sat, 29 Jun 2013 09:52:49 +0000 (15:22 +0530)
committerKurund Jalmi <kurund@civicrm.org>
Sat, 29 Jun 2013 09:52:49 +0000 (15:22 +0530)
CRM/Admin/Form/LabelFormats.php
CRM/Admin/Page/LabelFormats.php
CRM/Core/BAO/LabelFormat.php

index 328d5315f0ae82688b05e091bad364e829a82a1f..0c668f351f2c80e90e97ada5c2fa27f82aad3751 100644 (file)
@@ -45,6 +45,21 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
    */
   protected $_id = NULL;
 
+  /**
+   * Group name, label format or name badge
+   */
+  protected $_group = NULL;
+
+  function preProcess() {
+    $this->_id = $this->get('id');
+    $this->_group = CRM_Utils_Request::retrieve('group', 'String', $this, FALSE, 'label_format');
+    $this->_values = array();
+    if (isset($this->_id)) {
+      $params = array('id' => $this->_id);
+      CRM_Core_BAO_LabelFormat::retrieve($params, $this->_values, $this->_group);
+    }
+  }
+
   /**
    * Function to build the form
    *
@@ -60,8 +75,8 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
       return;
     }
 
-    $disabled    = array();
-    $required    = TRUE;
+    $disabled = array();
+    $required = TRUE;
     $is_reserved = $this->_id ? CRM_Core_BAO_LabelFormat::getFieldValue('CRM_Core_BAO_LabelFormat', $this->_id, 'is_reserved') : FALSE;
     if ($is_reserved) {
       $disabled['disabled'] = 'disabled';
@@ -74,16 +89,19 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
     $this->add('checkbox', 'is_default', ts('Is this Label Format the default?'));
     $this->add('select', 'paper_size', ts('Sheet Size'),
       array(
-        0 => ts('- default -')) + CRM_Core_BAO_PaperSize::getList(TRUE), FALSE,
+        0 => ts('- default -')
+      ) + CRM_Core_BAO_PaperSize::getList(TRUE), FALSE,
       array(
-        'onChange' => "selectPaper( this.value );") + $disabled
+        'onChange' => "selectPaper( this.value );"
+      ) + $disabled
     );
     $this->add('static', 'paper_dimensions', NULL, ts('Sheet Size (w x h)'));
     $this->add('select', 'orientation', ts('Orientation'), CRM_Core_BAO_LabelFormat::getPageOrientations(), FALSE,
       array(
-        'onChange' => "updatePaperDimensions();") + $disabled
+        'onChange' => "updatePaperDimensions();"
+      ) + $disabled
     );
-    $this->add('select', 'font_name', ts('Font Name'), CRM_Core_BAO_LabelFormat::getFontNames());
+    $this->add('select', 'font_name', ts('Font Name'), CRM_Core_BAO_LabelFormat::getFontNames($this->_group));
     $this->add('select', 'font_size', ts('Font Size'), CRM_Core_BAO_LabelFormat::getFontSizes());
     $this->add('static', 'font_style', ts('Font Style'));
     $this->add('checkbox', 'bold', ts('Bold'));
@@ -103,7 +121,10 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
     $this->add('text', 'tPadding', ts('Top Padding'), array('size' => 8, 'maxlength' => 8), $required);
     $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'weight'), TRUE);
 
-    $this->addRule('label', ts('Name already exists in Database.'), 'objectExists', array('CRM_Core_BAO_LabelFormat', $this->_id));
+    $this->addRule('label', ts('Name already exists in Database.'), 'objectExists', array(
+      'CRM_Core_BAO_LabelFormat',
+      $this->_id
+    ));
     $this->addRule('NX', ts('Must be an integer'), 'integer');
     $this->addRule('NY', ts('Must be an integer'), 'integer');
     $this->addRule('tMargin', ts('Must be numeric'), 'numeric');
@@ -119,7 +140,7 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
 
   function setDefaultValues() {
     if ($this->_action & CRM_Core_Action::ADD) {
-      $defaults['weight'] = CRM_Utils_Array::value('weight', CRM_Core_BAO_LabelFormat::getDefaultValues(), 0);
+      $defaults['weight'] = CRM_Utils_Array::value('weight', CRM_Core_BAO_LabelFormat::getDefaultValues($this->_group), 0);
     }
     else {
       $defaults = $this->_values;
@@ -148,16 +169,16 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
   public function postProcess() {
     if ($this->_action & CRM_Core_Action::DELETE) {
       // delete Label Format
-      CRM_Core_BAO_LabelFormat::del($this->_id);
+      CRM_Core_BAO_LabelFormat::del($this->_id, $this->_group);
       CRM_Core_Session::setStatus(ts('Selected Label Format has been deleted.'), ts('Record Deleted'), 'success');
       return;
     }
     if ($this->_action & CRM_Core_Action::COPY) {
       // make a copy of the Label Format
-      $labelFormat = CRM_Core_BAO_LabelFormat::getById($this->_id);
-      $list        = CRM_Core_BAO_LabelFormat::getList(TRUE);
-      $count       = 1;
-      $prefix      = ts('Copy of ');
+      $labelFormat = CRM_Core_BAO_LabelFormat::getById($this->_id, $this->_group);
+      $list = CRM_Core_BAO_LabelFormat::getList(TRUE);
+      $count = 1;
+      $prefix = ts('Copy of ');
       while (in_array($prefix . $labelFormat['label'], $list)) {
         $prefix = ts('Copy') . ' (' . ++$count . ') ' . ts('of ');
       }
@@ -165,8 +186,9 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
       $labelFormat['grouping'] = CRM_Core_BAO_LabelFormat::customGroupName();
       $labelFormat['is_default'] = 0;
       $labelFormat['is_reserved'] = 0;
+
       $bao = new CRM_Core_BAO_LabelFormat();
-      $bao->saveLabelFormat($labelFormat);
+      $bao->saveLabelFormat($values, NULL, $this->_group);
       CRM_Core_Session::setStatus($labelFormat['label'] . ts(' has been created.'), ts('Saved'), 'success');
       return;
     }
@@ -198,7 +220,7 @@ class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
     $values['font-style'] = $style;
 
     $bao = new CRM_Core_BAO_LabelFormat();
-    $bao->saveLabelFormat($values, $this->_id);
+    $bao->saveLabelFormat($values, $this->_id, $this->_group);
 
     $status = ts('Your new Label Format titled <strong>%1</strong> has been saved.', array(1 => $values['label']));
     if ($this->_action & CRM_Core_Action::UPDATE) {
index 0e088ffe62708cf02d8c13e5d6c65906e186c8cf..060b3b4f1eca58a4ffac263ca8c721877f34388e 100644 (file)
@@ -68,19 +68,19 @@ class CRM_Admin_Page_LabelFormats extends CRM_Core_Page_Basic {
         CRM_Core_Action::UPDATE => array(
           'name' => ts('Edit'),
           'url' => 'civicrm/admin/labelFormats',
-          'qs' => 'action=update&id=%%id%%&reset=1',
+          'qs' => 'action=update&id=%%id%%&group=%%group%%&reset=1',
           'title' => ts('Edit Label Format'),
         ),
         CRM_Core_Action::COPY => array(
           'name' => ts('Copy'),
           'url' => 'civicrm/admin/labelFormats',
-          'qs' => 'action=copy&id=%%id%%',
+          'qs' => 'action=copy&id=%%id%%&group=%%group%%&reset=1',
           'title' => ts('Copy Label Format'),
         ),
         CRM_Core_Action::DELETE => array(
           'name' => ts('Delete'),
           'url' => 'civicrm/admin/labelFormats',
-          'qs' => 'action=delete&id=%%id%%',
+          'qs' => 'action=delete&id=%%id%%&group=%%group%%&reset=1',
           'title' => ts('Delete Label Format'),
         ),
       );
@@ -125,7 +125,8 @@ class CRM_Admin_Page_LabelFormats extends CRM_Core_Page_Basic {
    */
   function browse($action = NULL) {
     // Get list of configured Label Formats
-    $labelFormatList = CRM_Core_BAO_LabelFormat::getList();
+    $labelFormatList= CRM_Core_BAO_LabelFormat::getList();
+    $nameFormatList= CRM_Core_BAO_LabelFormat::getList(false, 'name_badge');
 
     // Add action links to each of the Label Formats
     foreach ($labelFormatList as & $format) {
@@ -133,9 +134,22 @@ class CRM_Admin_Page_LabelFormats extends CRM_Core_Page_Basic {
       if (CRM_Utils_Array::value('is_reserved', $format)) {
         $action -= CRM_Core_Action::DELETE;
       }
-      $format['action'] = CRM_Core_Action::formLink(self::links(), $action, array('id' => $format['id']));
+      $format['action'] = CRM_Core_Action::formLink(self::links(), $action,
+        array('id' => $format['id'], 'group' => 'label_format'));
     }
 
+    // Add action links to each of the Label Formats
+    foreach ($nameFormatList as & $format) {
+      $action = array_sum(array_keys($this->links()));
+      if (CRM_Utils_Array::value('is_reserved', $format)) {
+        $action -= CRM_Core_Action::DELETE;
+      }
+      $format['action'] = CRM_Core_Action::formLink(self::links(), $action,
+        array('id' => $format['id'], 'group' => 'name_badge'));
+    }
+
+    $labelFormatList = array_merge($labelFormatList, $nameFormatList);
+
     // Order Label Formats by weight
     $returnURL = CRM_Utils_System::url(self::userContext());
     CRM_Core_BAO_LabelFormat::addOrder($labelFormatList, $returnURL);
index 535ad47db93c753861c7b9cd4035301388e9a86a..c6e1a2fe3ecb87f5857ee7977feb267f35fd4ffb 100644 (file)
@@ -173,14 +173,14 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
   /**
    * Get font names supported by the TCPDF package used to create PDF labels.
    *
-   * @param void
+   * @param string $name group name
    *
    * @return array   array of font names
    * @access public
    * @static
    */
-  public static function getFontNames() {
-    $label = new CRM_Utils_PDF_Label(self::getDefaultValues());
+  public static function getFontNames($name='label_format') {
+    $label = new CRM_Utils_PDF_Label(self::getDefaultValues($name));
     return $label->getFontNames();
   }
 
@@ -234,14 +234,14 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    * @return int  Group ID (null if Group ID doesn't exist)
    * @access private
    */
-  private static function _getGid() {
-    if (!self::$_gid) {
-      self::$_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'label_format', 'id', 'name');
-      if (!self::$_gid) {
+  private static function _getGid($name='label_format') {
+    if (!isset(self::$_gid[$name]) || !self::$_gid[$name]) {
+      self::$_gid[$name] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $name, 'id', 'name');
+      if (!self::$_gid[$name]) {
         CRM_Core_Error::fatal(ts('Label Format Option Group not found in database.'));
       }
     }
-    return self::$_gid;
+    return self::$_gid[$name];
   }
 
   /**
@@ -263,49 +263,50 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    * Retrieve list of Label Formats.
    *
    * @param bool    $namesOnly    return simple list of names
+   * @param string  $name         name of the option group
    *
    * @return array  (reference)   label format list
    * @static
    * @access public
    */
-  static function &getList($namesOnly = FALSE) {
+  static function &getList($namesOnly = FALSE, $name='label_format') {
     static $list = array();
-    if (self::_getGid()) {
+    if (self::_getGid($name)) {
       // get saved label formats from Option Value table
       $dao = new CRM_Core_DAO_OptionValue();
-      $dao->option_group_id = self::_getGid();
+      $dao->option_group_id = self::_getGid($name);
       $dao->is_active = 1;
       $dao->orderBy('weight');
       $dao->find();
       while ($dao->fetch()) {
         if ($namesOnly) {
-          $list[$dao->name] = $dao->label;
+          $list[$name][$dao->name] = $dao->label;
         }
         else {
-          CRM_Core_DAO::storeValues($dao, $list[$dao->id]);
+          CRM_Core_DAO::storeValues($dao, $list[$name][$dao->id]);
         }
       }
     }
-    return $list;
+    return $list[$name];
   }
 
   /**
    * retrieve the default Label Format values
    *
-   * @param NULL
+   * @param string $groupName label format group name
    *
    * @return array   Name/value pairs containing the default Label Format values.
    * @static
    * @access public
    */
-  static function &getDefaultValues() {
+  static function &getDefaultValues($groupName = 'label_format') {
     $params = array('is_active' => 1, 'is_default' => 1);
     $defaults = array();
-    if (!self::retrieve($params, $defaults)) {
+    if (!self::retrieve($params, $defaults, $groupName)) {
       foreach (self::$optionValueFields as $name => $field) {
         $defaults[$name] = $field['default'];
       }
-      $filter = array('option_group_id' => self::_getGid());
+      $filter = array('option_group_id' => self::_getGid($groupName));
       $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
     }
     return $defaults;
@@ -320,14 +321,14 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    * @return array  $values (reference) associative array of name/value pairs
    * @access public
    */
-  static function &getLabelFormat($field, $val) {
+  static function &getLabelFormat($field, $val, $groupName = 'label_format') {
     $params = array('is_active' => 1, $field => $val);
     $labelFormat = array();
-    if (self::retrieve($params, $labelFormat)) {
+    if (self::retrieve($params, $labelFormat, $groupName)) {
       return $labelFormat;
     }
     else {
-      return self::getDefaultValues();
+      return self::getDefaultValues($groupName);
     }
   }
 
@@ -347,12 +348,13 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    * Get Label Format by ID
    *
    * @param int    $id   label format id. 0 = get default label format
+   * @param string $groupName group name
    *
    * @return array  $values (reference) associative array of name/value pairs
    * @access public
    */
-  static function &getById($id) {
-    return self::getLabelFormat('id', $id);
+  static function &getById($id, $groupName = 'label_format') {
+    return self::getLabelFormat('id', $id, $groupName);
   }
 
   /**
@@ -397,10 +399,10 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    * @access public
    * @static
    */
-  static function retrieve(&$params, &$values) {
+  static function retrieve(&$params, &$values, $groupName='label_format') {
     $optionValue = new CRM_Core_DAO_OptionValue();
     $optionValue->copyValues($params);
-    $optionValue->option_group_id = self::_getGid();
+    $optionValue->option_group_id = self::_getGid($groupName);
     if ($optionValue->find(TRUE)) {
       // Extract fields that have been serialized in the 'value' column of the Option Value table.
       $values = json_decode($optionValue->value, TRUE);
@@ -440,13 +442,14 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    *
    * @param array (reference)   $values    associative array of name/value pairs
    * @param int                 $id        id of the database record (null = new record)
+   * @param string $groupName   group name of the label format
    *
    * @return void
    * @access public
    */
-  function saveLabelFormat(&$values, $id = NULL) {
+  function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') {
     // get the Option Group ID for Label Formats (create one if it doesn't exist)
-    $group_id = self::_getGid();
+    $group_id = self::_getGid($groupName);
 
     // clear other default if this is the new default label format
     if ($values['is_default']) {
@@ -462,7 +465,7 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
     }
     else {
       // new record
-      $list = self::getList(TRUE);
+      $list = self::getList(TRUE,$groupName);
       $cnt = 1;
       while (array_key_exists("custom_$cnt", $list)) $cnt++;
       $values['name'] = "custom_$cnt";
@@ -502,17 +505,17 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
    * Function to delete a Label Format
    *
    * @param  int  $id     ID of the label format to be deleted.
-   *
+   * @param  string $groupName group name
    * @access public
    * @static
    */
-  static function del($id) {
+  static function del($id, $groupName) {
     if ($id) {
       $dao = new CRM_Core_DAO_OptionValue();
       $dao->id = $id;
       if ($dao->find(TRUE)) {
-        if ($dao->option_group_id == self::_getGid()) {
-          $filter = array('option_group_id' => self::_getGid());
+        if ($dao->option_group_id == self::_getGid($groupName)) {
+          $filter = array('option_group_id' => self::_getGid($groupName));
           CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
           $dao->delete();
           return;