Add a file upload to Single Page in Dashboard

Permalink 2 users found helpful
I've created a single page for the dashboard following the example here:https://www.concrete5.org/documentation/how-tos/developers/build-a-s...

Everything works great and I was very happy to find this tutorial, but now I'm trying to add a file upload option to the page I'm creating... I am currently trying to use a "File" page attribute to store a file for the created pages, but I can't seem to figure out how to do it. I've searched quite a bit online and have looked at different core blocks that upload a file but I'm still stumped.

Hopefully what I'm trying to do makes sense! Can anyone provide a code snippet as to how to save a file attachment as a page attribute for single pages in the dashboard (using a 'view' and 'controller' file? Or if there's a better way to save single or multiple files to a page I'm open for using any method that works!

Thanks!

joemc
 
jordanlev replied on at Permalink Reply
jordanlev
I don't have a complete solution for you, but hopefully this gets you on the right track:

1) Provide a file upload field in your single page form (use the C5 form helper for this).

2) When processing the form, use the File Importer class to bring in that uploaded file to C5's file manager.

3) Set the page attribute to the file id of that file that was added to the file manager in step 2.

Hope that helps (at least a little bit).
joemc replied on at Permalink Reply 1 Attachment
joemc
Thanks for the reply Jordan.

I'm still having issues figuring this out... I'll supply a little code to hopefully help show what I'm doing.

1. In my 'view.php' file I currently have 2 page attributes working (they are select attributes):
<? 
Loader::model("attribute/categories/collection");
$news_categories = CollectionAttributeKey::getByHandle('news_categories');
$news_subjects = CollectionAttributeKey::getByHandle('news_subjects');
if (is_object($news)) {
   $news_categories_value = $news->getAttributeValueObject($news_categories);
   $news_subjects_value = $news->getAttributeValueObject($news_subjects);
}
?>
<div class="news-categories">
   <div>
      <strong><?=$news_categories->render('label');?></strong>
      <?=$news_categories->render('form', $news_categories_value, true);?>
   </div>
</div>


And this is what I'm attempting for my File attribute (I can get the upload field to show up, but I think my issue is that I can't figure out how to save the ID... and then also populate the File field with an existing image if one exists):
<strong><?=$form->label('cParentID', t('File'))?></strong>
<?php
$page_file = CollectionAttributeKey::getByHandle('page_file');
if (is_object($news)) {
   $page_file_value = $news->getAttributeValueObject($page_file);
}
$al = Loader::helper('concrete/asset_library'); 
echo $al->file('ccm-b-file', 'page_file', t('Choose File'));
?>
<br/>



2. In my 'controller.php' file there is a function called 'saveData' that saves the page info for a new page and an updated page. This works like a charm for all my existing fields, but I'm doing something wrong when it comes to my file upload field:
private function saveData($p) {
   $blocks = $p->getBlocks('Main');
   foreach($blocks as $b) {
      $b->deleteBlock();
   }
   $bt = BlockType::getByHandle('content');
   $data = array('content' => $this->post('newsBody'));
   $p->addBlock($bt, 'Main', $data);
   Loader::model("attribute/categories/collection");
   $news_categories = CollectionAttributeKey::getByHandle('news_categories');
   $news_categories->saveAttributeForm($p);   
   $news_subjects = CollectionAttributeKey::getByHandle('news_subjects');
   $news_subjects->saveAttributeForm($p);      
   // This is the file upload attempt
   $page_file = CollectionAttributeKey::getByHandle('page_file');


Hopefully this is enough info to help out... I haven't spent a lot of time with C5, so I haven't worked a whole lot with helpers and specific C5 calls.

Thanks again for all the help!
joemc replied on at Permalink Best Answer Reply
joemc
Ok, I figured out what I needed to do.

For anyone interested, here's the code I'm using in the 'view.php' file ($news is my page object):
$page_file = $news->getCollectionAttributeValue('page_file');
$fObj = null;
if (!empty($page_file))
   $fObj = File::getByID($page_file->fID);
$al = Loader::helper('concrete/asset_library'); ?>
<?=$al->image('ccm-b-image', 'fID', t('Choose Image'), $fObj);?>


Here's the code I'm using in the 'controller.php' file within the 'saveData' function (check out the link in my first posting to get the full set of files I used to base this dashboard page off of):
$fObj = File::getByID($this->post('fID'));
$p->setAttribute('page_file', $fObj);
LotosNile replied on at Permalink Reply
LotosNile
I'm trying to add a file upload to the registration page. Would this work there?
joemc replied on at Permalink Reply
joemc
I'm not sure. Are you talking about a front-end registration page or in the dashboard->Add User page?

I'm guessing this would be a good starting point at least, but I have not looked at the code for the registration page, so I can't say for sure whether you could just copy and paste this and have it work.
LotosNile replied on at Permalink Reply
LotosNile
Front end user registration page.
joemc replied on at Permalink Reply
joemc
I guess I don't really have an answer for you... I've never worked with the front-end registration. You'll just have to give it a try and/or search for more answers on the forum.

I'm guessing that since you're trying to attach a file to a use profile, it will be different than attaching an image as an attribute to a page. But, the code above may be a good starting point to try and figure it out.
jordanlev replied on at Permalink Reply
jordanlev