<commit_before><?php

require_once 'CRM/Core/Form.php';
require_once 'wci-helper-functions.php';
require_once 'CRM/Wci/BAO/ProgressBar.php';
?>

<script type="text/javascript">
cj(function ( $ ) {
  function setState() {
    if ($('#override').is(':checked') == true) {
      $('#custom_template').attr("disabled",false);
    }
    else {
      $('#custom_template').attr("disabled",true);
    }  
  }
  $(document).ready(setState)
  $('#override').click(setState);

});
</script>

<?php
/**
 * Form controller class
 *
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
 */
class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
  
  /**
   * the widget id saved to the session for an update
   *
   * @var int
   * @access protected
   */
  protected $_id;
  
  function preProcess() {
    parent::preProcess();
    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, 
          FALSE, NULL, 'REQUEST' );

    $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
        'text',
        FALSE,
        '#2786C2',
      ),
      'color_title_bg' => array(ts('Widget title background color'),
        'text',
        FALSE,
        '#FFFFFF',
      ),
      'color_bar' => array(ts('Progress Bar Color'),
        'text',
        FALSE,
        '#FFFFFF',
      ),
      'color_widget_bg' => array(ts('Widget background color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_description' => array(ts('Widget description color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_border' => array(ts('Widget border color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_button' => array(ts('Widget button text color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_button_bg' => array(ts('Widget button background color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_button_bg' => array(ts('Widget button background color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      );
  }

  function setDefaultValues() {
    $defaults = array();
    
    $defaults['size_variant'] = 'normal';
    foreach ($this->_colorFields as $name => $val) {
      $defaults[$name] = $val[3];
    }

    return $defaults;
  }

  function buildQuickForm() {
    // add form elements
    $this->add('text', 'title', ts('Title'),true);
    $this->add('text', 'logo_image', ts('Logo image'));
    $this->add('text', 'image', ts('Image'));
    $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
    $this->add('text', 'button_title', ts('Contribution button title'));
    $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
    $this->addWysiwyg('description', ts('Description'), '');
    $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
    $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
    foreach ($this->_colorFields as $name => $val) {
      $this->add($val[1],
        $name,
        $val[0],
        $name,
        $val[2]
      );
    }
    $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
    $this->add('checkbox', 'override', ts('Override default template'));
    $this->add('textarea', 'custom_template', ts('Custom template'));
    //$this->addWysiwyg('custom_template', ts('Custom template'), '');
    $this->addButtons(array(
      array(
        'type' => 'submit',
        'name' => ts('Save'),
        'isDefault' => TRUE,
      ),
    ));

    // export form elements
    $this->assign('elementNames', $this->getRenderableElementNames());

    $smarty = CRM_Core_Smarty::singleton();
    if (isset($this->_id)) {  
      /** Updating existing widget*/

      //$query = "SELECT * FROM civicrm_wci_widget where id=" . $this->_id;
      $query = "SELECT pb.id as pbid, w.*  FROM civicrm_wci_widget w INNER JOIN civicrm_wci_progress_bar pb on pb.id = w.progress_bar_id 
where w.id=" . $this->_id;
      $params = array();
      
      $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_WCI_DAO_Widget');

      while ($dao->fetch()) {
        $wid_page[$dao->id] = array();
        CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);

        $this->setDefaults(array(
              'title' => $wid_page[$dao->id]['title']));
        $this->setDefaults(array(
              'logo_image' => $wid_page[$dao->id]['logo_image']));
        $this->setDefaults(array(
              'image' => $wid_page[$dao->id]['image']));
        $this->setDefaults(array(
              'button_link_to' => $wid_page[$dao->id]['button_link_to']));
        $this->setDefaults(array(
              'button_title' => $wid_page[$dao->id]['button_title']));

        $this->setDefaults(array(
              'progress_bar' => $dao->pbid));
        $description = base64_decode($wid_page[$dao->id]['description']);
        $this->setDefaults(array(
              'description' => $description));
        $this->setDefaults(array(
              'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
        $this->setDefaults(array(
              'size_variant' => $wid_page[$dao->id]['size_variant']));
        $this->setDefaults(array(
              'color_title' => $wid_page[$dao->id]['color_title']));
        $this->setDefaults(array(
              'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
        $this->setDefaults(array(
              'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
        $this->setDefaults(array(
              'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
        $this->setDefaults(array(
              'color_description' => $wid_page[$dao->id]['color_description']));
        $this->setDefaults(array(
              'color_border' => $wid_page[$dao->id]['color_border']));
        $this->setDefaults(array(
              'color_button' => $wid_page[$dao->id]['color_button']));
        $this->setDefaults(array(
              'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
        $this->setDefaults(array(
              'style_rules' => $wid_page[$dao->id]['style_rules']));
        $this->setDefaults(array(
              'override' => $wid_page[$dao->id]['override']));
        $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);

        $this->setDefaults(array(
              'custom_template' => $cust_templ));
      }
    }
    else {
      /** Keep template in civicrm-wci/templates folder*/
      $output = $smarty->fetch('Widget.tpl');
      $output = file_get_contents('sites/all/modules/civicrm/extensions/civicrm-wci/templates/Widget.tpl');
      $elem = $this->getElement('custom_template');
      $elem->setValue($output); 
    }
    <commit_msg>#29762 Custom template integration in widget creation form<commit_after><?php

require_once 'CRM/Core/Form.php';
require_once 'wci-helper-functions.php';
require_once 'CRM/Wci/BAO/ProgressBar.php';
?>

<script type="text/javascript">
cj(function ( $ ) {
  function setState() {
    if ($('#override').is(':checked') == true) {
      $('#custom_template').attr("disabled",false);
    }
    else {
      $('#custom_template').attr("disabled",true);
    }  
  }
  $(document).ready(setState)
  $('#override').click(setState);

});
</script>

<?php
/**
 * Form controller class
 *
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
 */
class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
  
  /**
   * the widget id saved to the session for an update
   *
   * @var int
   * @access protected
   */
  protected $_id;
  
  function preProcess() {
    parent::preProcess();
    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, 
          FALSE, NULL, 'REQUEST' );

    $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
        'text',
        FALSE,
        '#2786C2',
      ),
      'color_title_bg' => array(ts('Widget title background color'),
        'text',
        FALSE,
        '#FFFFFF',
      ),
      'color_bar' => array(ts('Progress Bar Color'),
        'text',
        FALSE,
        '#FFFFFF',
      ),
      'color_widget_bg' => array(ts('Widget background color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_description' => array(ts('Widget description color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_border' => array(ts('Widget border color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_button' => array(ts('Widget button text color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_button_bg' => array(ts('Widget button background color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      'color_button_bg' => array(ts('Widget button background color'),
        'text',
        FALSE,
        '#96C0E7',
      ),
      );
  }

  function setDefaultValues() {
    $defaults = array();
    
    $defaults['size_variant'] = 'normal';
    foreach ($this->_colorFields as $name => $val) {
      $defaults[$name] = $val[3];
    }

    return $defaults;
  }

  function buildQuickForm() {
    // add form elements
    $this->add('text', 'title', ts('Title'),true);
    $this->add('text', 'logo_image', ts('Logo image'));
    $this->add('text', 'image', ts('Image'));
    $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
    $this->add('text', 'button_title', ts('Contribution button title'));
    $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
    $this->addWysiwyg('description', ts('Description'), '');
    $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
    $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
    foreach ($this->_colorFields as $name => $val) {
      $this->add($val[1],
        $name,
        $val[0],
        $name,
        $val[2]
      );
    }
    $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
    $this->add('checkbox', 'override', ts('Override default template'));
    $this->add('textarea', 'custom_template', ts('Custom template:<br><SMALL><font color="red">Please customize the smarty v2 template only if you know what you are doing</font></SMALL>'));

    $this->addButtons(array(
      array(
        'type' => 'submit',
        'name' => ts('Save'),
        'isDefault' => TRUE,
      ),
    ));

    // export form elements
    $this->assign('elementNames', $this->getRenderableElementNames());

    $smarty = CRM_Core_Smarty::singleton();
    if (isset($this->_id)) {  
      /** Updating existing widget*/

      $query = "SELECT pb.id as pbid, w.*  FROM civicrm_wci_widget w INNER JOIN civicrm_wci_progress_bar pb on pb.id = w.progress_bar_id 
where w.id=" . $this->_id;
      $params = array();
      
      $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_WCI_DAO_Widget');

      while ($dao->fetch()) {
        $wid_page[$dao->id] = array();
        CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);

        $this->setDefaults(array(
              'title' => $wid_page[$dao->id]['title']));
        $this->setDefaults(array(
              'logo_image' => $wid_page[$dao->id]['logo_image']));
        $this->setDefaults(array(
              'image' => $wid_page[$dao->id]['image']));
        $this->setDefaults(array(
              'button_link_to' => $wid_page[$dao->id]['button_link_to']));
        $this->setDefaults(array(
              'button_title' => $wid_page[$dao->id]['button_title']));

        $this->setDefaults(array(
              'progress_bar' => $dao->pbid));
        $description = base64_decode($wid_page[$dao->id]['description']);
        $this->setDefaults(array(
              'description' => $description));
        $this->setDefaults(array(
              'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
        $this->setDefaults(array(
              'size_variant' => $wid_page[$dao->id]['size_variant']));
        $this->setDefaults(array(
              'color_title' => $wid_page[$dao->id]['color_title']));
        $this->setDefaults(array(
              'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
        $this->setDefaults(array(
              'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
        $this->setDefaults(array(
              'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
        $this->setDefaults(array(
              'color_description' => $wid_page[$dao->id]['color_description']));
        $this->setDefaults(array(
              'color_border' => $wid_page[$dao->id]['color_border']));
        $this->setDefaults(array(
              'color_button' => $wid_page[$dao->id]['color_button']));
        $this->setDefaults(array(
              'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
        $this->setDefaults(array(
              'style_rules' => $wid_page[$dao->id]['style_rules']));
        $this->setDefaults(array(
              'override' => $wid_page[$dao->id]['override']));
        $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);

        $this->setDefaults(array(
              'custom_template' => $cust_templ));
      }
    }
    else {
      /** Keep template in civicrm-wci/templates folder*/
      $output = file_get_contents('sites/all/modules/civicrm/extensions/civicrm-wci/templates/Widget.tpl');
      $elem = $this->getElement('custom_template');
      $elem->setValue($output); 
    }
{'fraction_non_alphanumeric': 0.19075777485764345, 'fraction_numerical': 0.006132282084975909, 'mean_word_length': 2.824399776661083, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 225, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2107891', 'n_tokens_mistral': 4450, 'n_tokens_neox': 4410, 'n_words': 699}