Answers.Body: <p>I hope I'm not barking up the wrong tree, but could you pawsibly try <a href="https://docs.civicrm.org/user/en/latest/searching/searchkit/what-is-searchkit/" rel="nofollow noreferrer">SearchKit</a>? It's a doggone helpful tool that can render your table without any of that custom Smarty code (which can be ruff to maintain), and it has a built-in &quot;Download Spreadsheet&quot; action to fetch and retrieve CSV, XLSX, or ODS.</p>
  <p>Bone Appétit!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T19:57:46.247Z
Answers.Text: I did look at it but the SQL is too complex for it (I think).  It has a couple of joins to other custom entities created by my custom extensions.
Answers.UserDisplayName: Norm Dressler
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T19:58:49.130Z
Answers.Text: ... from civicrm_dog_scores
      inner join civicrm_registered_dogs ON civicrm_dog_scores.DogId = civicrm_registered_dogs.id
      inner join civicrm_contact ON civicrm_contact.id = civicrm_registered_dogs.contact_id
      inner join civicrm_trial_levels ON civicrm_dog_scores.Rules = civicrm_trial_levels.Id
      where DogId=".$this->_id." ORDER by Event
Answers.UserDisplayName: Norm Dressler
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T20:00:34.230Z
Answers.Text: I also didn't find a SAVE CSV for the results pulled in from the Search.
Answers.UserDisplayName: Norm Dressler
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T20:06:19.057Z
Answers.Text: If you retrofit xml for those tables then you can generate BAO/DAO and you get all the magic of API4 and SearchKit. https://docs.civicrm.org/dev/en/latest/framework/database/schema-definition/  https://docs.civicrm.org/dev/en/latest/extensions/civix/#generate-entity
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T20:54:47.850Z
Answers.Text: Right, SearchKit can easily handle a query like that as long as you've got up-to-date metadata describing all the field specs and FKs. The download action is available in the "Actions" dropdown just above the results table in SearchKit.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T19:51:22.830Z
Answers.LastActivityDate: 2023-10-30T20:50:12.127Z
Answers.LastEditDate: 2023-10-30T20:50:12.127Z
Answers.UserDisplayName: Coleman
Body: <p>I have a function that lists score for a dog:</p>
  <pre><code>&lt;h3&gt;Your Dog's Scores&lt;/h3&gt;
  {crmButton href=&quot;#&quot; class=&quot;button-name&quot; title=&quot;Download CSV&quot; icon=&quot;icon-class-name&quot;}Download CSV{/crmButton}
  &lt;table border='1' cellpadding='5' cellspacing='5'&gt;&lt;tr class=&quot;crm-entity&quot; id=&quot;Scores&quot; style='border-bottom: 1px solid black'&gt;&lt;th&gt;Registered Name&lt;/th&gt;&lt;th&gt;Event&lt;/th&gt;&lt;th&gt;Level&lt;/th&gt;&lt;th&gt;Stream&lt;/th&gt;&lt;th&gt;Score&lt;/th&gt;&lt;th&gt;Max Points&lt;/th&gt;&lt;th&gt;Pass&lt;/th&gt;&lt;/td&gt;&lt;/tr&gt;
  {*{crmAPI var='dogs' entity='RegisteredDogs' action='get' contact_id = $contactid} *}
  {foreach from=$scores key=arrayIndex item=score}
    &lt;tr&gt;
    &lt;td&gt;{$score-&gt;DogName}&lt;/td&gt;
    &lt;td&gt;{$score-&gt;Event}&lt;/td&gt;
    &lt;td&gt;{$score-&gt;LevelDescript}&lt;/td&gt;
    &lt;td&gt;{$score-&gt;Stream}&lt;/td&gt;
    &lt;td&gt;{$score-&gt;Score}&lt;/td&gt;
    &lt;td&gt;{$score-&gt;MaxPoints}&lt;/td&gt;
    &lt;td&gt;{$score-&gt;Pass}&lt;/td&gt;
    &lt;/tr&gt;
  {/foreach} 
  
  &lt;/table&gt;
  </code></pre>
  <p>This works really well.  The pop-up window shows the scores and lists them out.  The issue is I'm being asked if I can export the displayed data as a CSV file.  I see on the top bar that there is a 'print' icon - is there a 'Download' option available?  My second option was to use a separate button (as you can see the crmButton on the second line).</p>
  <p>The array $scores has the results from a SQL query in the PHP (above is the smarty template code).<br />
  Is there an easy way to add the csv download option?
  Can I call a function with the href in the button?  Perhaps I just need to write a php function to do this?  Any help would be appreciated.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-30T19:14:58.940Z
Comments.Text: I'm intrigued by these tests; do the dogs write essays or is it multiple-choice? How do the dogs hold their pencils? And how can you read their handwriting? Or is the test administered via computer? Can the dogs touch-type or is there bark-to-text recognition software? Perhaps you could recruit the dogs with the highest scores to write code for you?
Comments.UserDisplayName: Coleman
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-30T19:28:28.717Z
Comments.Text: LOL that made my day.  We are the largest scent detection association in Canada.  We are a non-profit that sanctions sport scent detection events :)  So we register dogs and handlers and keep track of their scores, awarding titles when they achieve certain levels.
Comments.UserDisplayName: Norm Dressler
Tags: <extension><php><smarty><csv><custom>
Title: CSV Download from a custom pop up
ThreadText: # CSV Download from a custom pop up
  
  (Asked by: username_0 on 2023-10-30)
  
  I have a function that lists score for a dog:
  Your Dog's Scores
  {crmButton href="#" class="button-name" title="Download CSV" icon="icon-class-name"}Download CSV{/crmButton}
  Registered NameEventLevelStreamScoreMax PointsPass
  {*{crmAPI var='dogs' entity='RegisteredDogs' action='get' contact_id = $contactid} *}
  {foreach from=$scores key=arrayIndex item=score}
    
    {$score->DogName}
    {$score->Event}
    {$score->LevelDescript}
    {$score->Stream}
    {$score->Score}
    {$score->MaxPoints}
    {$score->Pass}
    
  {/foreach} 
  
  
  
  This works really well.  The pop-up window shows the scores and lists them out.  The issue is I'm being asked if I can export the displayed data as a CSV file.  I see on the top bar that there is a 'print' icon - is there a 'Download' option available?  My second option was to use a separate button (as you can see the crmButton on the second line).
  The array $scores has the results from a SQL query in the PHP (above is the smarty template code).
  Is there an easy way to add the csv download option?
  Can I call a function with the href in the button?  Perhaps I just need to write a php function to do this?  Any help would be appreciated.
  
  **Tags:** extension, php, smarty, csv, custom
  
  > **Comments:**
  > **username_1**: I'm intrigued by these tests; do the dogs write essays or is it multiple-choice? How do the dogs hold their pencils? And how can you read their handwriting? Or is the test administered via computer? Can the dogs touch-type or is there bark-to-text recognition software? Perhaps you could recruit the dogs with the highest scores to write code for you?  
  > **username_0**: LOL that made my day.  We are the largest scent detection association in Canada.  We are a non-profit that sanctions sport scent detection events :)  So we register dogs and handlers and keep track of their scores, awarding titles when they achieve certain levels.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-30. Score: 2 | Accepted answer
  
  I hope I'm not barking up the wrong tree, but could you pawsibly try [SearchKit](https://docs.civicrm.org/user/en/latest/searching/searchkit/what-is-searchkit/)? It's a doggone helpful tool that can render your table without any of that custom Smarty code (which can be ruff to maintain), and it has a built-in "Download Spreadsheet" action to fetch and retrieve CSV, XLSX, or ODS.
  
  Bone Appétit!
  
  > **Comments:**
  > **username_0**: I did look at it but the SQL is too complex for it (I think).  It has a couple of joins to other custom entities created by my custom extensions.  
  > **username_0**: ... from civicrm_dog_scores     inner join civicrm_registered_dogs ON civicrm_dog_scores.DogId = civicrm_registered_dogs.id     inner join civicrm_contact ON civicrm_contact.id = civicrm_registered_dogs.contact_id     inner join civicrm_trial_levels ON civicrm_dog_scores.Rules = civicrm_trial_levels.Id     where DogId=".$this->_id." ORDER by Event  
  > **username_0**: I also didn't find a SAVE CSV for the results pulled in from the Search.  
  > **username_2**: If you retrofit xml for those tables then you can generate BAO/DAO and you get all the magic of API4 and SearchKit. https://docs.civicrm.org/dev/en/latest/framework/database/schema-definition/  https://docs.civicrm.org/dev/en/latest/extensions/civix/#generate-entity  
  > **username_1**: Right, SearchKit can easily handle a query like that as long as you've got up-to-date metadata describing all the field specs and FKs. The download action is available in the "Actions" dropdown just above the results table in SearchKit.  
  
  ---
  
  