0

Sorry for the newbie question...

I modified CRM_Contact_Form_Task_Label class, to create my own Task. It creates necessary data for printing membership cards. If a contact has all necessary data, then these are printed to a file, if not, the contact should be shown as result of a Custom Search.

I can't figure out why, but this code doesn't shows up the file, only redirects the page. File is shown up when I add exit; after the $fileReady = self::createMembershipCard($rows); row.

Please advise me, how can I show up the file and then redirect the page.

Thank you!

    function postProcess($params = NULL) {

        [...]

        $fileReady = FALSE;
        $fileReady = self::createMembershipCard($rows);
        if($fileReady) {
            $csvListOfIds = implode(",", $failed);
            CRM_Utils_System::redirect( CRM_Utils_System::url('civicrm/contact/search/custom', "reset=1&force=1&csid=19&cid={$csvListOfIds}"));
        }
     }

  /**
   * Create export file for Membership Cards.
   *
   * @param array $contactRows
   *   Associated array of contact data.
   */
    public function createMembershipCard(&$contactRows) {
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=data.txt');

        $output = fopen('php://output', 'w');
        
        if(sizeof($contactRows) != 0) {
            foreach($contactRows as $item) {
                $toOutput = $item['display_name'].';'.date('Y.m.d.').';2099.21.31.'.PHP_EOL;
                fwrite($output, $toOutput);
            }
        }
        fclose($output);
        
        return TRUE;
    }

1 Answer 1

1

It's not possible in a web browser application / HTTP to do a download and then redirect.

Since you're passing the list of cids in the redirect url, what you can do is in buildform add a link on the page to a download that uses the list of cids if present. Then add a "page" (civix generate:page) in your extension that responds to that link and does the same thing your createMembershipCard() function does.

e.g. civix generate:page CRM_Myextension_MembershipCard civicrm/myextension/membershipcard

and then in buildForm you'd do something like

$cids = CRM_Utils_Request::retrieve('cids', 'String', $this);
$url = CRM_Utils_System::url('civicrm/myextension/membershipcard', "reset=1&cids={$cids}");
$form->assign('url', $url);
CRM_Core_Region::instance('page-body')->add(array(
    'template' => 'CRM/Myextension/MembershipCard.tpl',
  ));

and in MembershipCard.tpl you'd have a link like <a href="{$url}">{ts}Download membership card{/ts}</a>

and in CRM_Myextension_MembershipCard::run you do the same thing as createMembershipCard().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.