CRM add remaining missing comment blocks (autogenerated)
[civicrm-core.git] / CRM / Financial / BAO / ExportFormat.php
index 30a656c5a33fd66af0578b91ece56175c766c405..d6105f9dde414f1c27c76cc996afdd069591044a 100644 (file)
-<?php\r
-/*\r
- +--------------------------------------------------------------------+\r
- | CiviCRM version 4.3                                             |\r
- +--------------------------------------------------------------------+\r
- | Copyright CiviCRM LLC (c) 2004-2013                                |\r
- +--------------------------------------------------------------------+\r
- | This file is a part of CiviCRM.                                    |\r
- |                                                                    |\r
- | CiviCRM is free software; you can copy, modify, and distribute it  |\r
- | under the terms of the GNU Affero General Public License           |\r
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |\r
- |                                                                    |\r
- | CiviCRM is distributed in the hope that it will be useful, but     |\r
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |\r
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |\r
- | See the GNU Affero General Public License for more details.        |\r
- |                                                                    |\r
- | You should have received a copy of the GNU Affero General Public   |\r
- | License and the CiviCRM Licensing Exception along                  |\r
- | with this program; if not, contact CiviCRM LLC                     |\r
- | at info[AT]civicrm[DOT]org. If you have questions about the        |\r
- | GNU Affero General Public License or the licensing of CiviCRM,     |\r
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |\r
- +--------------------------------------------------------------------+\r
-*/\r
-\r
-/**\r
- *\r
- * @package CRM\r
- * @copyright CiviCRM LLC (c) 2004-2013\r
- * $Id$\r
- *\r
- */\r
-\r
-/**\r
- * Base class for Export Formats\r
- * Create a subclass for a specific format.\r
- * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation\r
- */\r
-\r
-class CRM_Financial_BAO_ExportFormat {\r
-\r
-  /*\r
-   * Array of data which the individual export formats will output in the desired format\r
-   */\r
-  protected $_exportParams;\r
-\r
-  /*\r
-   * smarty template\r
-   */\r
-  static protected $_template;\r
-\r
-  /**\r
-   * class constructor\r
-   */\r
-  function __construct() {\r
-    if ( !isset( self::$_template ) ) {\r
-      self::$_template = CRM_Core_Smarty::singleton();\r
-    }\r
-  }\r
-\r
-  // Override to assemble the appropriate subset of financial data for the specific export format\r
-  function export($exportParams) {\r
-    $this->_exportParams = $exportParams;\r
-    return $exportParams;\r
-  }\r
-\r
-  function output($fileName = NULL) {\r
-    switch ($this->getFileExtension()) {\r
-      case 'csv':\r
-        self::createActivityExport($this->_batchIds, $fileName);\r
-      break;\r
-\r
-      case 'iif':\r
-        $tplFile = $this->getTemplateFileName();\r
-        $out = self::getTemplate()->fetch( $tplFile );\r
-        $fileName = $this->putFile($out);\r
-        self::createActivityExport($this->_batchIds, $fileName);\r
-      break;\r
-    }\r
-  }\r
-\r
-  function getMimeType() {\r
-    return 'text/plain';\r
-  }\r
-\r
-  function getFileExtension() {\r
-    return 'txt';\r
-  }\r
-\r
-  // Override this if appropriate\r
-  function getTemplateFileName() {\r
-    return null;\r
-  }\r
-\r
-  static function &getTemplate() {\r
-    return self::$_template;\r
-  }\r
-\r
-  function assign($var, $value = NULL) {\r
-    self::$_template->assign($var, $value);\r
-  }\r
-\r
-  /*\r
-   * This gets called for every item of data being compiled before being sent to the exporter for output.\r
-   *\r
-   * Depending on the output format might want to override this, e.g. for IIF tabs need to be escaped etc,\r
-   * but for CSV it doesn't make sense because php has built in csv output functions.\r
-   */\r
-  static function format($s, $type = 'string') {\r
-    if (!empty($s)) {\r
-      return $s;\r
-    }\r
-    else {\r
-      return NULL;\r
-    }\r
-  }\r
-\r
-  function initiateDownload() {\r
-    $config = CRM_Core_Config::singleton();\r
-    //zip files if more than one.\r
-    if (count($this->_downloadFile)>1) {\r
-      $zip = $config->customFileUploadDir.'Financial_Transactions_'.date('YmdHis').'.zip';\r
-      $result = $this->createZip($this->_downloadFile, $zip, TRUE);\r
-      if ($result) {\r
-        header('Content-Type: application/zip');\r
-        header('Content-Disposition: attachment; filename='.CRM_Utils_File::cleanFileName(basename($zip)));\r
-        header('Content-Length: ' . filesize($zip));\r
-        ob_clean();\r
-        flush();\r
-        readfile($config->customFileUploadDir.CRM_Utils_File::cleanFileName(basename($zip)));\r
-        unlink($zip); //delete the zip to avoid clutter.\r
-        CRM_Utils_System::civiExit();\r
-      }\r
-    }\r
-    else {\r
-      header('Content-Type: '.mime_content_type($this->_downloadFile[0]));\r
-      header('Content-Disposition: attachment; filename='.CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0])));\r
-      header('Content-Length: ' . filesize($this->_downloadFile[0]));\r
-      ob_clean();\r
-      flush();\r
-      readfile($config->customFileUploadDir.CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0])));\r
-      CRM_Utils_System::civiExit();\r
-    }\r
-  }\r
-\r
-  static function createActivityExport($batchIds, $fileName) {\r
-    $session = CRM_Core_Session::singleton();\r
-    $values = array();\r
-    $params = array('id' => $batchIds);\r
-    CRM_Batch_BAO_Batch::retrieve($params, $values);\r
-    $createdBy = CRM_Contact_BAO_Contact::displayName($values['created_id']);\r
-    $modifiedBy = CRM_Contact_BAO_Contact::displayName($values['modified_id']);\r
-\r
-    $values['payment_instrument_id'] = '';\r
-    if (isset($values['payment_instrument_id'])) {\r
-      $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant::paymentInstrument('label'));\r
-      $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument);\r
-    }\r
-    $details = '<p>' . ts('Record: ') . $values['title'] . '</p><p>' . ts('Description: ') . '</p><p>' . ts('Created By: ') . $createdBy . '</p><p>' . ts('Created Date: ') . $values['created_date'] . '</p><p>' . ts('Last Modified By: ') . $modifiedBy . '</p><p>' . ts('Payment Instrument: ') . $values['payment_instrument_id'] . '</p>';\r
-    $subject = '';\r
-    if (CRM_Utils_Array::value('total', $values)) {\r
-      $subject .= ts('Total') . '['. CRM_Utils_Money::format($values['total']) .'],';\r
-    }\r
-    if (CRM_Utils_Array::value('item_count', $values)) {\r
-      $subject .= ' ' . ts('Count') . '['. $values['item_count'] .'],';\r
-    }\r
-\r
-    //create activity.\r
-    $subject .=  ' ' . ts('Batch') . '['. $values['title'] .']';\r
-    $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');\r
-    $activityParams = array(\r
-      'activity_type_id' => array_search('Export Accounting Batch', $activityTypes),\r
-      'subject' => $subject,\r
-      'status_id' => 2,\r
-      'activity_date_time' => date('YmdHis'),\r
-      'source_contact_id' => $session->get('userID'),\r
-      'source_record_id' => $values['id'],\r
-      'target_contact_id' => $session->get('userID'),\r
-      'details' => $details,\r
-      'attachFile_1' => array (\r
-        'uri' => $fileName,\r
-        'type' => 'text/csv',\r
-        'location' => $fileName,\r
-        'upload_date' => date('YmdHis'),\r
-      ),\r
-    );\r
-\r
-    CRM_Activity_BAO_Activity::create($activityParams);\r
-  }\r
-\r
-  function createZip($files = array(), $destination = NULL, $overwrite = FALSE) {\r
-    //if the zip file already exists and overwrite is false, return false\r
-    if (file_exists($destination) && !$overwrite) {\r
-      return FALSE;\r
-    }\r
-    $valid_files = array();\r
-    if (is_array($files)) {\r
-      foreach ($files as $file) {\r
-        //make sure the file exists\r
-        if (file_exists($file)) {\r
-          $validFiles[] = $file;\r
-        }\r
-      }\r
-    }\r
-    if (count($validFiles)) {\r
-      $zip = new ZipArchive();\r
-      if ($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) {\r
-        return FALSE;\r
-      }\r
-      foreach ($validFiles as $file) {\r
-        $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file)));\r
-      }\r
-      $zip->close();\r
-      return file_exists($destination);\r
-    }\r
-    else {\r
-        return FALSE;\r
-      }\r
-  }\r
-}
\ No newline at end of file
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.5                                             |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2014
+ * $Id$
+ *
+ */
+
+/**
+ * Base class for Export Formats
+ * Create a subclass for a specific format.
+ * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation
+ */
+
+class CRM_Financial_BAO_ExportFormat {
+
+  /*
+   * Array of data which the individual export formats will output in the desired format
+   */
+  protected $_exportParams;
+
+  /*
+   * smarty template
+   */
+  static protected $_template;
+
+  /**
+   * class constructor
+   */
+  function __construct() {
+    if ( !isset( self::$_template ) ) {
+      self::$_template = CRM_Core_Smarty::singleton();
+    }
+  }
+
+  // Override to assemble the appropriate subset of financial data for the specific export format
+  /**
+   * @param $exportParams
+   *
+   * @return mixed
+   */
+  function export($exportParams) {
+    $this->_exportParams = $exportParams;
+    return $exportParams;
+  }
+
+  /**
+   * @param null $fileName
+   */
+  function output($fileName = NULL) {
+    switch ($this->getFileExtension()) {
+      case 'csv':
+        self::createActivityExport($this->_batchIds, $fileName);
+      break;
+
+      case 'iif':
+        $tplFile = $this->getHookedTemplateFileName();
+        $out = self::getTemplate()->fetch($tplFile);
+        $fileName = $this->putFile($out);
+        self::createActivityExport($this->_batchIds, $fileName);
+      break;
+    }
+  }
+
+  /**
+   * @return string
+   */
+  function getMimeType() {
+    return 'text/plain';
+  }
+
+  /**
+   * @return string
+   */
+  function getFileExtension() {
+    return 'txt';
+  }
+
+  // Override this if appropriate
+  /**
+   * @return null
+   */
+  function getTemplateFileName() {
+    return null;
+  }
+
+  /**
+   * @return object
+   */
+  static function &getTemplate() {
+    return self::$_template;
+  }
+
+  /**
+   * @param $var
+   * @param null $value
+   */
+  function assign($var, $value = NULL) {
+    self::$_template->assign($var, $value);
+  }
+
+  /*
+   * This gets called for every item of data being compiled before being sent to the exporter for output.
+   *
+   * Depending on the output format might want to override this, e.g. for IIF tabs need to be escaped etc,
+   * but for CSV it doesn't make sense because php has built in csv output functions.
+   */
+  /**
+   * @param $s
+   * @param string $type
+   *
+   * @return null
+   */
+  static function format($s, $type = 'string') {
+    if (!empty($s)) {
+      return $s;
+    }
+    else {
+      return NULL;
+    }
+  }
+
+  function initiateDownload() {
+    $config = CRM_Core_Config::singleton();
+    //zip files if more than one.
+    if (count($this->_downloadFile)>1) {
+      $zip = $config->customFileUploadDir . 'Financial_Transactions_' . date('YmdHis') . '.zip';
+      $result = $this->createZip($this->_downloadFile, $zip, TRUE);
+      if ($result) {
+        header('Content-Type: application/zip');
+        header('Content-Disposition: attachment; filename=' . CRM_Utils_File::cleanFileName(basename($zip)));
+        header('Content-Length: ' . filesize($zip));
+        ob_clean();
+        flush();
+        readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($zip)));
+        unlink($zip); //delete the zip to avoid clutter.
+        CRM_Utils_System::civiExit();
+      }
+    }
+    else {
+      header('Content-Type: text/plain');
+      header('Content-Disposition: attachment; filename=' . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0])));
+      header('Content-Length: ' . filesize($this->_downloadFile[0]));
+      ob_clean();
+      flush();
+      readfile($config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($this->_downloadFile[0])));
+      CRM_Utils_System::civiExit();
+    }
+  }
+
+  /**
+   * @param $batchIds
+   * @param $fileName
+   *
+   * @throws CRM_Core_Exception
+   */
+  static function createActivityExport($batchIds, $fileName) {
+    $session = CRM_Core_Session::singleton();
+    $values = array();
+    $params = array('id' => $batchIds);
+    CRM_Batch_BAO_Batch::retrieve($params, $values);
+    $createdBy = CRM_Contact_BAO_Contact::displayName($values['created_id']);
+    $modifiedBy = CRM_Contact_BAO_Contact::displayName($values['modified_id']);
+
+    $values['payment_instrument_id'] = '';
+    if (isset($values['payment_instrument_id'])) {
+      $paymentInstrument = array_flip(CRM_Contribute_PseudoConstant::paymentInstrument('label'));
+      $values['payment_instrument_id'] = array_search($values['payment_instrument_id'], $paymentInstrument);
+    }
+    $details = '<p>' . ts('Record: ') . $values['title'] . '</p><p>' . ts('Description: ') . '</p><p>' . ts('Created By: ') . $createdBy . '</p><p>' . ts('Created Date: ') . $values['created_date'] . '</p><p>' . ts('Last Modified By: ') . $modifiedBy . '</p><p>' . ts('Payment Instrument: ') . $values['payment_instrument_id'] . '</p>';
+    $subject = '';
+    if (!empty($values['total'])) {
+      $subject .= ts('Total') . '['. CRM_Utils_Money::format($values['total']) .'],';
+    }
+    if (!empty($values['item_count'])) {
+      $subject .= ' ' . ts('Count') . '['. $values['item_count'] .'],';
+    }
+
+    //create activity.
+    $subject .=  ' ' . ts('Batch') . '['. $values['title'] .']';
+    $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
+    $activityParams = array(
+      'activity_type_id' => array_search('Export Accounting Batch', $activityTypes),
+      'subject' => $subject,
+      'status_id' => 2,
+      'activity_date_time' => date('YmdHis'),
+      'source_contact_id' => $session->get('userID'),
+      'source_record_id' => $values['id'],
+      'target_contact_id' => $session->get('userID'),
+      'details' => $details,
+      'attachFile_1' => array (
+        'uri' => $fileName,
+        'type' => 'text/csv',
+        'location' => $fileName,
+        'upload_date' => date('YmdHis'),
+      ),
+    );
+
+    CRM_Activity_BAO_Activity::create($activityParams);
+  }
+
+  /**
+   * @param array $files
+   * @param null $destination
+   * @param bool $overwrite
+   *
+   * @return bool
+   */
+  function createZip($files = array(), $destination = NULL, $overwrite = FALSE) {
+    //if the zip file already exists and overwrite is false, return false
+    if (file_exists($destination) && !$overwrite) {
+      return FALSE;
+    }
+    $valid_files = array();
+    if (is_array($files)) {
+      foreach ($files as $file) {
+        //make sure the file exists
+        if (file_exists($file)) {
+          $validFiles[] = $file;
+        }
+      }
+    }
+    if (count($validFiles)) {
+      $zip = new ZipArchive();
+      if ($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) {
+        return FALSE;
+      }
+      foreach ($validFiles as $file) {
+        $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file)));
+      }
+      $zip->close();
+      return file_exists($destination);
+    }
+    else {
+        return FALSE;
+      }
+  }
+}