<commit_before><?php
/*
 * This file is part of the reditype package.
 * (c) 2009-2010 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * BasertShopVoucherAdminActions
 *
 * @package    rtShopPlugin
 * @subpackage modules
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 */
class BasertShopVoucherAdminActions extends sfActions
{
  public function preExecute()
  {
    rtTemplateToolkit::setTemplateForMode('backend');
    sfConfig::set('app_rt_node_title', 'Vouchers');
  }
  
  public function executeIndex(sfWebRequest $request)
  {
    $query = Doctrine::getTable('rtShopVoucher')->getQuery();
    $query->orderBy('p.created_at DESC');

    $this->pager = new sfDoctrinePager(
      'rtShopVoucher',
      $this->getCountPerPage($request)
    );

    $this->pager->setQuery($query);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
  }

  private function getCountPerPage(sfWebRequest $request)
  {
    $count = sfConfig::get('app_rt_admin_pagination_limit', 50);
    if($request->hasParameter('show_more'))
    {
      $count = sfConfig::get('app_rt_admin_pagination_per_page_multiple', 2) * $count;
    }

    return $count;
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new rtShopVoucherForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new rtShopVoucherForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($rt_shop_voucher = Doctrine::getTable('rtShopVoucher')->find(array($request->getParameter('id'))), sprintf('Object rt_shop_voucher does not exist (%s).', $request->getParameter('id')));
    $this->form = new rtShopVoucherForm($rt_shop_voucher);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($rt_shop_voucher = Doctrine::getTable('rtShopVoucher')->find(array($request->getParameter('id'))), sprintf('Object rt_shop_voucher does not exist (%s).', $request->getParameter('id')));
    $this->form = new rtShopVoucherForm($rt_shop_voucher);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($rt_shop_voucher = Doctrine::getTable('rtShopVoucher')->find(array($request->getParameter('id'))), sprintf('Object rt_shop_voucher does not exist (%s).', $request->getParameter('id')));
    $rt_shop_voucher->delete();

    $this->redirect('rtShopVoucherAdmin/index');
  }

  /**
   * Batch create vouchers
   *
   * @param sfWebRequest $request  Request object
   */
  public function executeBatchCreate(sfWebRequest $request)
  {
    $this->form = new rtShopVoucherBatchForm();

    if($request->isMethod('post'))
    {
      $this->processBatchForm($request, $this->form);
      
      $batch_data = $request->getParameter('rt_shop_voucher_batch');
      if ($this->form->isValid())
      {
        $form_values = $this->form->getValues();

        $reference = rtShopVoucherToolkit::generateBatch($this->form->getValues(),$form_values['batchsize']);

        if($reference)
        {
          $this->getUser()->setFlash('notice',$form_values['batchsize'].' vouchers created - ref. #'.$reference);
          $this->redirect('rtShopVoucherAdmin/index');
        }
        else
        {
          $this->getUser()->setFlash('error','An error occured while creating batch vouchers', false);
        }
      } 
      $this->getUser()->setFlash('default_error', true, false);
    }
  }

  /**
   * Delete batch vouchers
   *
   * @param sfWebRequest $request
   */
  public function executeBatchDelete(sfWebRequest $request)
  {
    if(!$request->hasParameter('id'))
    {
      $this->redirect('rtShopVoucherAdmin/index');
    }

    $q = Doctrine_Query::create()
          ->delete('rtShopVoucher v')
          ->andWhere('v.batch_reference = ?', $request->getParameter('id'))
          ->execute();

    $this->getUser()->setFlash('notice','Batch vouchers with reference '.$request->getParameter('id').' were deleted.');
    $this->redirect('rtShopVoucherAdmin/index');
  }

  /**
   * Delete batch vouchers
   *
   * @param sfWebRequest $request
   */
  public function executeBatchShow(sfWebRequest $request)
  {
    $this->batch = Doctrine::getTable('rtShopVoucher')->findInfoOnBatchReference($request->getParameter('id'));
  }

  /**
   * Create batch reports as CSV, XML and JSON formats
   * 
   * @param sfWebRequest $request 
   */
  public function executeBatchReport(sfWebRequest $request)
  {
    if(!$request->hasParameter('id'))
    {
      $this->redirect('rtShopVoucherAdmin/index');
    }

    $fields = 'v.code,v.title,v.batch_reference,v.reduction_type,v.reduction_value,v.count,v.mode,v.total_from,v.total_to,v.date_from,v.date_to';
    $fieldnames = preg_replace('/[\$.]/', '_', $fields);
    $this->key_order = explode(',', $fieldnames);
    $q = Doctrine_Query::create()
            ->select($fields)
            ->from('rtShopVoucher v')
            ->addWhere('v.batch_reference = ?', $request->getParameter('id'))
            ->orderBy('v.count ASC');
    $vouchers = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR);

    $this->vouchers = array();
    $i=0;
    foreach($vouchers as $voucher)
    {
      foreach($this->key_order as $key => $value)
      {
        if($value === 'v_title')
        {
          $this->vouchers[$i][$value] = preg_replace('/[^a-zA-Z0-9_ - . $]/s', '', $voucher[$value]);
        }
        else
        {
          $this->vouchers[$i][$value] = $voucher[$value];
        }
      }
      $i++;<commit_msg>Added created_at and updated_at fields to csv, xml and json<commit_after><?php
/*
 * This file is part of the reditype package.
 * (c) 2009-2010 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * BasertShopVoucherAdminActions
 *
 * @package    rtShopPlugin
 * @subpackage modules
 * @author     <NAME> <<EMAIL>>
 * @author     <NAME> <<EMAIL>>
 */
class BasertShopVoucherAdminActions extends sfActions
{
  public function preExecute()
  {
    rtTemplateToolkit::setTemplateForMode('backend');
    sfConfig::set('app_rt_node_title', 'Vouchers');
  }
  
  public function executeIndex(sfWebRequest $request)
  {
    $query = Doctrine::getTable('rtShopVoucher')->getQuery();
    $query->orderBy('p.created_at DESC');

    $this->pager = new sfDoctrinePager(
      'rtShopVoucher',
      $this->getCountPerPage($request)
    );

    $this->pager->setQuery($query);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
  }

  private function getCountPerPage(sfWebRequest $request)
  {
    $count = sfConfig::get('app_rt_admin_pagination_limit', 50);
    if($request->hasParameter('show_more'))
    {
      $count = sfConfig::get('app_rt_admin_pagination_per_page_multiple', 2) * $count;
    }

    return $count;
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new rtShopVoucherForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new rtShopVoucherForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($rt_shop_voucher = Doctrine::getTable('rtShopVoucher')->find(array($request->getParameter('id'))), sprintf('Object rt_shop_voucher does not exist (%s).', $request->getParameter('id')));
    $this->form = new rtShopVoucherForm($rt_shop_voucher);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($rt_shop_voucher = Doctrine::getTable('rtShopVoucher')->find(array($request->getParameter('id'))), sprintf('Object rt_shop_voucher does not exist (%s).', $request->getParameter('id')));
    $this->form = new rtShopVoucherForm($rt_shop_voucher);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($rt_shop_voucher = Doctrine::getTable('rtShopVoucher')->find(array($request->getParameter('id'))), sprintf('Object rt_shop_voucher does not exist (%s).', $request->getParameter('id')));
    $rt_shop_voucher->delete();

    $this->redirect('rtShopVoucherAdmin/index');
  }

  /**
   * Batch create vouchers
   *
   * @param sfWebRequest $request  Request object
   */
  public function executeBatchCreate(sfWebRequest $request)
  {
    $this->form = new rtShopVoucherBatchForm();

    if($request->isMethod('post'))
    {
      $this->processBatchForm($request, $this->form);
      
      $batch_data = $request->getParameter('rt_shop_voucher_batch');
      if ($this->form->isValid())
      {
        $form_values = $this->form->getValues();

        $reference = rtShopVoucherToolkit::generateBatch($this->form->getValues(),$form_values['batchsize']);

        if($reference)
        {
          $this->getUser()->setFlash('notice',$form_values['batchsize'].' vouchers created - ref. #'.$reference);
          $this->redirect('rtShopVoucherAdmin/index');
        }
        else
        {
          $this->getUser()->setFlash('error','An error occured while creating batch vouchers', false);
        }
      } 
      $this->getUser()->setFlash('default_error', true, false);
    }
  }

  /**
   * Delete batch vouchers
   *
   * @param sfWebRequest $request
   */
  public function executeBatchDelete(sfWebRequest $request)
  {
    if(!$request->hasParameter('id'))
    {
      $this->redirect('rtShopVoucherAdmin/index');
    }

    $q = Doctrine_Query::create()
          ->delete('rtShopVoucher v')
          ->andWhere('v.batch_reference = ?', $request->getParameter('id'))
          ->execute();

    $this->getUser()->setFlash('notice','Batch vouchers with reference '.$request->getParameter('id').' were deleted.');
    $this->redirect('rtShopVoucherAdmin/index');
  }

  /**
   * Delete batch vouchers
   *
   * @param sfWebRequest $request
   */
  public function executeBatchShow(sfWebRequest $request)
  {
    $this->batch = Doctrine::getTable('rtShopVoucher')->findInfoOnBatchReference($request->getParameter('id'));
  }

  /**
   * Create batch reports as CSV, XML and JSON formats
   * 
   * @param sfWebRequest $request 
   */
  public function executeBatchReport(sfWebRequest $request)
  {
    if(!$request->hasParameter('id'))
    {
      $this->redirect('rtShopVoucherAdmin/index');
    }

    $fields = 'v.code,v.title,v.batch_reference,v.reduction_type,v.reduction_value,v.count,v.mode,v.total_from,v.total_to,v.date_from,v.date_to,v.created_at,v.updated_at';
    $fieldnames = preg_replace('/[\$.]/', '_', $fields);
    $this->key_order = explode(',', $fieldnames);
    $q = Doctrine_Query::create()
            ->select($fields)
            ->from('rtShopVoucher v')
            ->addWhere('v.batch_reference = ?', $request->getParameter('id'))
            ->orderBy('v.count ASC');
    $vouchers = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR);

    $this->vouchers = array();
    $i=0;
    foreach($vouchers as $voucher)
    {
      foreach($this->key_order as $key => $value)
      {
        if($value === 'v_title')
        {
          $this->vouchers[$i][$value] = preg_replace('/[^a-zA-Z0-9_ - . $]/s', '', $voucher[$value]);
        }
        else
        {
          $this->vouchers[$i][$value] = $voucher[$value];
        }
      }
      $i++;
{'fraction_non_alphanumeric': 0.16942559178943883, 'fraction_numerical': 0.004966065220989902, 'mean_word_length': 3.6580570547417115, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 195, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '5326698', 'n_tokens_mistral': 4006, 'n_tokens_neox': 3874, 'n_words': 602}