/**
  * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers
  *
  * @param string 	$filepath
  * @param array 	$data 2D array, 		first numerically-indexed,
  *                    						and next-level-down preferably indexed by string
  * @param boolean 	$write_column_headers 	whether or not we should add the keys in the bottom-most array
  * 											as a row for headers in the CSV.
  *                                            Eg, if $data looked like:
  *                                            array(
  *                                              	0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...),
  * 													1=>array(...,...)
  *                                            )
  *
  * @return boolean 		if we successfully wrote to the CSV or not. If there's no $data,
  * 						we consider that a success (because we wrote everything there was...nothing)
  * @throws EE_Error
  */
 public static function write_data_array_to_csv($filepath, $data, $write_column_headers = true)
 {
     $new_file_contents = '';
     //determine if $data is actually a 2d array
     if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) {
         //make sure top level is numerically indexed,
         if (EEH_Array::is_associative_array($data)) {
             throw new EE_Error(sprintf(__("top-level array must be numerically indexed. Does these look like numbers to you? %s", "event_espresso"), implode(",", array_keys($data))));
         }
         $item_in_top_level_array = EEH_Array::get_one_item_from_array($data);
         //now, is the last item in the top-level array of $data an associative or numeric array?
         if ($write_column_headers && EEH_Array::is_associative_array($item_in_top_level_array)) {
             //its associative, so we want to output its keys as column headers
             $keys = array_keys($item_in_top_level_array);
             $new_file_contents .= EEH_Export::get_csv_row($keys);
         }
         //start writing data
         foreach ($data as $data_row) {
             $new_file_contents .= EEH_Export::get_csv_row($data_row);
         }
         return EEH_File::write_to_file($filepath, EEH_File::get_file_contents($filepath) . $new_file_contents);
     } else {
         //no data TO write... so we can assume that's a success
         return true;
     }
 }
 /**
  * 		generates HTML for the Registration main meta box
  *		@access public
  *		@return void
  */
 public function _reg_attendees_meta_box()
 {
     $REG = EEM_Registration::instance();
     //get all other registrations on this transaction, and cache
     //the attendees for them so we don't have to run another query using force_join
     $registrations = $REG->get_all(array(array('TXN_ID' => $this->_registration->transaction_ID(), 'REG_ID' => array('!=', $this->_registration->ID())), 'force_join' => array('Attendee')));
     $this->_template_args['attendees'] = array();
     $this->_template_args['attendee_notice'] = '';
     EE_Registry::instance()->load_helper('Array');
     if (empty($registrations) || is_array($registrations) && !EEH_Array::get_one_item_from_array($registrations)) {
         EE_Error::add_error(__('There are no records attached to this registration. Something may have gone wrong with the registration', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         $this->_template_args['attendee_notice'] = EE_Error::get_notices();
     } else {
         $att_nmbr = 1;
         foreach ($registrations as $registration) {
             /* @var $registration EE_Registration */
             $attendee = $registration->attendee() ? $registration->attendee() : EEM_Attendee::instance()->create_default_object();
             $this->_template_args['attendees'][$att_nmbr]['fname'] = $attendee->fname();
             //( isset( $registration->ATT_fname ) & ! empty( $registration->ATT_fname ) ) ? $registration->ATT_fname : '';
             $this->_template_args['attendees'][$att_nmbr]['lname'] = $attendee->lname();
             //( isset( $registration->ATT_lname ) & ! empty( $registration->ATT_lname ) ) ? $registration->ATT_lname : '';
             $this->_template_args['attendees'][$att_nmbr]['email'] = $attendee->email();
             //( isset( $registration->ATT_email ) & ! empty( $registration->ATT_email ) ) ? $registration->ATT_email : '';
             $this->_template_args['attendees'][$att_nmbr]['final_price'] = $registration->final_price();
             //( isset( $registration->REG_final_price ) & ! empty( $registration->REG_final_price ) ) ? $registration->REG_final_price : '';
             $this->_template_args['attendees'][$att_nmbr]['address'] = implode(', ', $attendee->full_address_as_array());
             $this->_template_args['attendees'][$att_nmbr]['att_link'] = self::add_query_args_and_nonce(array('action' => 'edit_attendee', 'post' => $attendee->ID()), REG_ADMIN_URL);
             $att_nmbr++;
         }
         //EEH_Debug_Tools::printr( $attendees, '$attendees  <br /><span style="font-size:10px;font-weight:normal;">( file: '. __FILE__ . ' - line no: ' . __LINE__ . ' )</span>', 'auto' );
         $this->_template_args['event_name'] = $this->_registration->event_obj()->name();
         $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign;
         //			$this->_template_args['registration_form_url'] = add_query_arg( array( 'action' => 'edit_registration', 'process' => 'attendees'  ), REG_ADMIN_URL );
     }
     $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_attendees.template.php';
     echo EEH_Template::display_template($template_path, $this->_template_args, TRUE);
 }
 /**
  * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers
  * @param array $data 2D array, first numerically-indexed, and next-level-down preferably indexed by string
  * @param boolean $add_csv_column_names whether or not we should add the keys in the bottom-most array as a row for headers in the CSV.
  * Eg, if $data looked like array(0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...), 1=>array(...),...))
  * then the first row we'd write to the CSV would be "EVT_ID,EVT_name,..."
  * @return boolean if we successfully wrote to the CSV or not. If there's no $data, we consider that a success (because we wrote everything there was...nothing)
  */
 public function write_data_array_to_csv($filehandle, $data)
 {
     EE_Registry::instance()->load_helper('Array');
     //determine if $data is actually a 2d array
     if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) {
         //make sure top level is numerically indexed,
         if (EEH_Array::is_associative_array($data)) {
             throw new EE_Error(sprintf(__("top-level array must be numerically indexed. Does these look like numbers to you? %s", "event_espresso"), implode(",", array_keys($data))));
         }
         $item_in_top_level_array = EEH_Array::get_one_item_from_array($data);
         //now, is the last item in the top-level array of $data an associative or numeric array?
         if (EEH_Array::is_associative_array($item_in_top_level_array)) {
             //its associative, so we want to output its keys as column headers
             $keys = array_keys($item_in_top_level_array);
             echo $this->fputcsv2($filehandle, $keys);
         }
         //start writing data
         foreach ($data as $data_row) {
             echo $this->fputcsv2($filehandle, $data_row);
         }
         return true;
     } else {
         //no data TO write... so we can assume that's a success
         return true;
     }
     //		//if 2nd level is indexed by strings, use those as csv column headers (ie, the first row)
     //
     //
     //		$no_table = TRUE;
     //
     //		// loop through data and add each row to the file/stream as csv
     //		foreach ( $data as $model_name => $model_data ) {
     //			// test first row to see if it is data or a model name
     //			$model = 	EE_Registry::instance();->load_model($model_name);
     //			//if the model really exists,
     //			if ( $model ) {
     //
     //				// we have a table name
     //				$no_table = FALSE;
     //
     //				// put the tablename into an array cuz that's how fputcsv rolls
     //				$model_name_row = array( 'MODEL', $model_name );
     //
     //				// add table name to csv output
     //				echo self::fputcsv2($filehandle, $model_name_row);
     //
     //				// now get the rest of the data
     //				foreach ( $model_data as $row ) {
     //					// output the row
     //					echo self::fputcsv2($filehandle, $row);
     //				}
     //
     //			}
     //
     //			if ( $no_table ) {
     //				// no table so just put the data
     //				echo self::fputcsv2($filehandle, $model_data);
     //			}
     //		} 		//		END OF foreach ( $data )
 }