CRM-12965, worked on form building and postprocess
authorKurund Jalmi <kurund@civicrm.org>
Sun, 30 Jun 2013 14:37:16 +0000 (20:07 +0530)
committerKurund Jalmi <kurund@civicrm.org>
Sun, 30 Jun 2013 14:37:16 +0000 (20:07 +0530)
CRM/Badge/BAO/Layout.php
CRM/Badge/Form/Layout.php
CRM/Core/BAO/LabelFormat.php
templates/CRM/Badge/Form/Layout.tpl
templates/CRM/Badge/Page/Layout.tpl
xml/schema/Core/PrintLabel.xml

index b5357fe7f06aab5b28098efdb626a409bd726bef..f7c5fc1d7b2cdceb577b9d81a594aab780b82393 100644 (file)
@@ -94,6 +94,20 @@ class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel {
     $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
     $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
 
+    $params['label_type_id'] = CRM_Core_OptionGroup::getValue('label_type', 'Event Badge', 'name');
+
+    // check if new layout is create, if so set the created_id (if not set)
+    if (empty($params['id'])) {
+      if (empty($params['created_id'])) {
+        $session = CRM_Core_Session::singleton();
+        $params['created_id'] = $session->get('userID');
+      }
+    }
+
+    if (!isset($params['id']) && !isset($params['name'])) {
+      $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
+    }
+
     // action is taken depending upon the mode
     $printLabel = new CRM_Core_DAO_PrintLabel();
     $printLabel->copyValues($params);
index 485610e290e042e3929d1faf95755077718a8e3d..12a0282b75c3d96b17b78c4a48bc1720e7cb470c 100644 (file)
@@ -54,30 +54,82 @@ class CRM_Badge_Form_Layout extends CRM_Admin_Form {
     }
 
     $this->applyFilter('__ALL__', 'trim');
-    $this->add('text',
-      'name',
-      ts('Name'),
-      CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'name'),
-      TRUE
-    );
-    $this->addRule('name',
-      ts('Name already exists in Database.'),
-      'objectExists',
-      array('CRM_Core_DAO_PrintLabel', $this->_id)
-    );
-    $this->addRule('name',
-      ts('Name can only consist of alpha-numeric characters'),
-      'variable'
-    );
-
-    $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'display_name'));
-    $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'description'));
-    $this->add('checkbox', 'is_active', ts('Enabled?'));
+
+    $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'), true);
+
+    $labelStyle = CRM_Core_PseudoConstant::get('CRM_Core_DAO_PrintLabel', 'label_format_id');
+    $this->add('select', 'label_format_id', ts('Label Style'), array('' => ts('- select -')) + $labelStyle, TRUE);
+
+    $this->add('text', 'description', ts('Description'),
+      CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'description'));
+
+    // get the tokens
+    $tokens = CRM_Core_SelectValues::contactTokens();
+    asort($tokens);
+
+    $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes();
+    $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge');
+    $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments();
+
+    $rowCount = 3;
+    for ( $i =1; $i <= $rowCount; $i++ ) {
+      $this->add('select', "token[$i]", ts('Token'), array('' => ts('- none -')) + $tokens);
+      $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames);
+      $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes);
+      $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment);
+    }
+    $rowCount++;
+    $this->assign('rowCount', $rowCount);
+
+    $this->add('checkbox', 'add_barcode', ts('Barcode?'));
+    unset($textAlignment['J']);
+    $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment);
+
     $this->add('checkbox', 'is_default', ts('Default?'));
+    $this->add('checkbox', 'is_active', ts('Enabled?'));
+    $this->add('checkbox', 'is_reserved', ts('Reserved?'));
 
     if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PrintLabel', $this->_id, 'is_reserved')) {
-      $this->freeze(array('name', 'description', 'is_active'));
+      $this->freeze(array('title', 'description', 'is_active'));
+    }
+  }
+
+  /**
+   * This function sets the default values for the form. MobileProvider that in edit/view mode
+   * the default values are retrieved from the database
+   *
+   * @access public
+   *
+   * @return None
+   */
+  function setDefaultValues() {
+    if (isset($this->_id) && empty($this->_values)) {
+      $this->_values = array();
+      $params = array('id' => $this->_id);
+      CRM_Badge_BAO_Layout::retrieve($params, $this->_values );
+    }
+
+    $defaults = $this->_values;
+
+    $data = get_object_vars(json_decode($this->_values['data']));
+
+    $specialFields = array('token', 'font_name', 'font_size', 'text_alignment');
+    foreach($specialFields as $field) {
+      $defaults[$field] = get_object_vars($data[$field]);
     }
+
+    $defaults['add_barcode'] = $data['add_barcode'];
+    $defaults['barcode_alignment'] = $data['barcode_alignment'];
+    $defaults['label_format_id'] = $data['label_format_id'];
+
+    if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['title'])) {
+      $this->assign('delName', $defaults['title']);
+    }
+
+    // its ok if there is no element called is_active
+    $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1;
+
+    return $defaults;
   }
 
   /**
@@ -88,41 +140,26 @@ class CRM_Badge_Form_Layout extends CRM_Admin_Form {
    * @return None
    */
   public function postProcess() {
-    /*
-        if ($this->_action & CRM_Core_Action::DELETE) {
-          CRM_Core_BAO_LocationType::del($this->_id);
-          CRM_Core_Session::setStatus(ts('Selected Location type has been deleted.'), ts('Record Deleted'), 'success');
-          return;
-        }
-
-        // store the submitted values in an array
-        $params = $this->exportValues();
-        $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
-        $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
-
-        // action is taken depending upon the mode
-        $locationType = new CRM_Core_DAO_PrintLabel();
-        $locationType->name = $params['name'];
-        $locationType->display_name = $params['display_name'];
-        $locationType->vcard_name = $params['vcard_name'];
-        $locationType->description = $params['description'];
-        $locationType->is_active = $params['is_active'];
-        $locationType->is_default = $params['is_default'];
-
-        if ($params['is_default']) {
-          $query = "UPDATE civicrm_location_type SET is_default = 0";
-          CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
-        }
-
-        if ($this->_action & CRM_Core_Action::UPDATE) {
-          $locationType->id = $this->_id;
-        }
-
-        $locationType->save();
-
-    CRM_Core_Session::setStatus(ts("The location type '%1' has been saved.",
-        array(1 => $locationType->name)
-      ), ts('Saved'), 'success');
-    */
+    if ($this->_action & CRM_Core_Action::DELETE) {
+      CRM_Badge_BAO_Layout::del($this->_id);
+      CRM_Core_Session::setStatus(ts('Selected badge layout has been deleted.'), ts('Record Deleted'), 'success');
+      return;
+    }
+
+    $params = $data = $this->controller->exportValues($this->_name);
+
+    unset($data['qfKey']);
+    $params['data'] = json_encode($data);
+
+    if ($this->_id) {
+      $params['id'] = $this->_id;
+    }
+
+    // store the submitted values in an array
+    CRM_Badge_BAO_Layout::create($params);
+
+    CRM_Core_Session::setStatus(ts("The badge layout '%1' has been saved.",
+      array(1 => $params['title'])
+    ), ts('Saved'), 'success');
   }
 }
index c1d62ab64d5eedcf4bf871e95e296b3059baac1f..3b16781c2aa0cac49513d45904db8c271ebd80dd 100644 (file)
@@ -226,6 +226,23 @@ class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue {
     );
   }
 
+  /**
+   * Get text alignment recognized by the TCPDF package used to create PDF labels.
+   *
+   * @param void
+   *
+   * @return array   array of alignments
+   * @access public
+   * @static
+   */
+  public static function getTextAlignments() {
+    return array(
+      'R' => ts('Right'),
+      'L' => ts('Left'),
+      'J' => ts('Justify'),
+    );
+  }
+
   /**
    * Get Option Group ID for Label Formats
    *
index c3734ae4c6457c4f0f06d853d8b3d321a3dc1ce3..34ec4568c5c6414e496af36f505056dc29eac64d 100644 (file)
   {if $action eq 8}
     <div class="messages status no-popup">
       <div class="icon inform-icon"></div>
-      {ts}WARNING: Deleting this option will result in the loss of all location type records which use the option.{/ts} {ts}This may mean the loss of a substantial amount of data, and the action cannot be undone.{/ts} {ts}Do you want to continue?{/ts}
+      {ts}This may mean the loss of a substantial amount of data, and the action cannot be undone.{/ts} {ts}Do you want to continue?{/ts}
     </div>
   {else}
     <table class="form-layout-compressed">
-      <!--tr class="crm-badge-layout-form-block-label">
-        <td class="label">{$form.name.label}</td>
-        <td>{$form.name.html}<br/>
-          <span class="description">{ts}WARNING: Do NOT use spaces in the Location Name.{/ts}</span>
-        </td>
-      </tr-->
       <tr class="crm-badge-layout-form-block-title">
         <td class="label">{$form.title.label}</td>
         <td>{$form.title.html}</td>
       </tr>
+      <tr class="crm-badge-layout-form-block-label_format_id">
+        <td class="label">{$form.label_format_id.label}</td>
+        <td>{$form.label_format_id.html}</td>
+      </tr>
       <tr class="crm-badge-layout-form-block-description">
         <td class="label">{$form.description.label}</td>
         <td>{$form.description.html}</td>
       </tr>
+      <tr class="crm-badge-layout-form-block-elements">
+        <td class="label">{ts}Elements{/ts}</td>
+        <td>
+          <table class="form-layout-compressed">
+            <tr>
+              <td>{ts}Row{/ts}</td>
+              <td>{ts}Label{/ts}</td>
+              <td>{ts}Font{/ts}</td>
+              <td>{ts}Size{/ts}</td>
+              <td>{ts}Alignment{/ts}</td>
+            </tr>
+            {section name='i' start=1 loop=$rowCount}
+              {assign var='rowNumber' value=$smarty.section.i.index}
+              <tr>
+                <td>#{$rowNumber}</td>
+                <td>{$form.token.$rowNumber.html|crmAddClass:twenty}</td>
+                <td>{$form.font_name.$rowNumber.html}</td>
+                <td>{$form.font_size.$rowNumber.html}</td>
+                <td>{$form.text_alignment.$rowNumber.html}</td>
+              </tr>
+            {/section}
+          </table>
+        </td>
+      </tr>
+      <tr class="crm-badge-layout-form-block-attachment">
+        <td colspan="2">
+
+        </td>
+      </tr>
+      <tr class="crm-badge-layout-form-block-add_barcode">
+        <td class="label">{$form.add_barcode.label}</td>
+        <td>{$form.add_barcode.html}&nbsp;&nbsp;&nbsp;{ts}on{/ts}&nbsp;&nbsp;&nbsp;{$form.barcode_alignment.html}</td>
+      </tr>
       <tr class="crm-badge-layout-form-block-is_active">
         <td class="label">{$form.is_active.label}</td>
         <td>{$form.is_active.html}</td>
index 1c3d2e9a802190cfe6f94e88609dae970f2568f3..75df9831a28812195687df58c96a5f216d1d3b86 100644 (file)
@@ -24,7 +24,7 @@
  +--------------------------------------------------------------------+
 *}
 <div id="help">
-  {ts}Badge Layout.{/ts}
+  {ts}Badge Layout screen for creating custom labels{/ts}
 </div>
 
 {if $action eq 1 or $action eq 2 or $action eq 8}
index 6a79f1ab43d2fb839f111989111a1dae63b05be9..c511b690e9e530c05d8fb6b2bda04ffb57dc0c6a 100644 (file)
   <field>
     <name>label_format_id</name>
     <type>int unsigned</type>
-    <comment>Implicit FK to civicrm_option_value row in label_format option group</comment>
+    <pseudoconstant>
+      <optionGroupName>name_badge</optionGroupName>
+    </pseudoconstant>
+    <comment>Implicit FK to civicrm_option_value row in name_badge option group</comment>
     <add>4.4</add>
   </field>
   <field>
     <name>label_type_id</name>
     <type>int unsigned</type>
+    <pseudoconstant>
+      <optionGroupName>label_type</optionGroupName>
+    </pseudoconstant>
     <comment>Implicit FK to civicrm_option_value row in NEW label_type option group</comment>
     <add>4.4</add>
   </field>