As per the requirement of
Sergio I shared the code from an earlier work requirement and then we made some changes to the same.
The final code is in this
.
I am sharing the communication between us:
MAIL #1Dear Sergio,
Greetings from the CouchCMS Community and GXCPL (CTR Dept)! I came to CouchCMS with the same intent in mind as yours and have been hooked to it over a decade, delivering projects from Basic dynamic sites to enterprise level solutions and products. We have also been the designated "Authorized Technology Training Associate for CouchCMS" since 2015, by KK Sir himself.
I hope you too find CouchCMS useful and the community helpful. I have attached a text file (in zip, as that is the allowed method in this message, named "CouchCMS All Links.zip") in this mail that comes in pretty handy at times. This file includes the links from the forum that are really helpful, always. Hope you find it useful, too. Also, I have inserted links to CouchCMS concepts along the way to point you to the appropriate resource.
In response to your post "How to create a file download system".
I will first breakdown your question in order to be sure that I have understood it correctly:
1. Create a file download system.
2. Segregate the files using Categories.
3. Display attributes such as Title, Description, author, publication date, associated icon (pdf, zip, ...etc) for each downloadable file.
4. All content should be dynamically manageable.
I will make some assumptions before I start to explain the approach to you, viz:
1. You are using the latest version of CouchCMS (i.e. v2.4) [Downloadable link:
Github]
2. You are only using the CouchCMS admin panel to manage the content dynamically.
3. You will have a single file for download under each set of attributes.
4. Multiple files can be made available under each category, for download.
5. I will use tabulated format for display, as that will help you strip the code and use it in your design,easily.
6. You have set up your project, created DB and installed CouchCMS.
7. You are not, currently , using the Extended Users module.
With that said, I will try to explain the approach, along with the code on the way.
Step #1 (Back-end): Create a clonable template with Dynamic Folders activated, that can manage the files1.a. With the file named with a PHP extension (as CouchCMS's 101, I am calling this file/ template 'sample-download.php') and boiler plates in place, we will define a template using the <cms:template> tag. We will make the template clonable and activate the dynamic folders, that will be used to create categories.
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title="Download System" clonable="1" dynamic_folders="1">
<!-- Editables will be defined here -->
</cms:template>
<?php COUCH::invoke(); ?>
1.b. Clonable templates present to us two fields, Title (k_page_title) and Name (k_page_name). Name is a required field for CouchCMS (if left blank, the Title value will be sanitized and the k_page_name will be generated using it). So you could use the pre-defined Title as one of the attributes that you want. We will then define the editable for remaining attributes.
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title="Download System" clonable="1" dynamic_folders="1">
<!-- Title (k_page_title): Can be used as one of the required attributes -->
<cms:editable name="description" label="Description" type="richtext" order="1" />
<cms:editable name="author" label="Author" type="text" order="2" />
<cms:editable name="publication_date" label="Publication Date" type="datetime" format="Y-m-d" order="3" />
<cms:editable name="file_to_download" label="Upload the file" desc="This file will be downloaded from the front end" type="securefile" allowed_ext="jpg,jpeg,png,doc,docx,ppt,pptx,xls,xlsx,pdf,zip" max_size="5120" order="4" />
</cms:template>
<?php COUCH::invoke(); ?>
- I have made assumptions on file extensions that you will want to provide for download. (
You can find the complete list of extensions in the CouchCMS core file 'couch/addons/data-bound-form/securefile.php' under the '$default_allowed_ext' array. For your quick reference, I am also mentioning them here, '7z', 'aiff', 'asf', 'avi', 'bmp', 'csv', 'doc', 'docx', 'fla', 'flv', 'gif', 'gz', 'gzip', 'jpeg', 'jpg', 'mid', 'mov', 'mp3', 'mp4', 'mpc', 'mpeg', 'mpg', 'ods', 'odt', 'pdf', 'png', 'ppt', 'pptx', 'pxd', 'qt', 'ram', 'rar', 'rm', 'rmi', 'rmvb', 'rtf', 'sdc', 'sitd', 'svg', 'swf', 'sxc', 'sxw', 'tar', 'tgz', 'tif', 'tiff', 'txt', 'vsd', 'wav', 'webm', 'wma', 'wmv', 'xls', 'xlsx', 'xml', 'zip')
- I have considered that the maximum file size to be 5Mb (max_size attribute specifies the size).
- The above can be achieved using type="file" in the editable region, but for security I have used the
securefile region.
- I have left out the associated file icon, which we will use in a different way. The approach would be to use a font-text rather than an image, that states the file type. (
Font Awesome would be a good option)
That sets up the template that can now be managed through the back end (admin panel) of CouchCMS.
Step #2 (Front-end): Displaying the files and categorizing them2.a. I have created a Category display for displaying the available categories using
<cms:folders> tag- Code: Select all
<div class="col-12 col-md-3 mb-5">
<h4 class="mb-2">Categories</h4>
<div class="list-group shadow">
<cms:folders masterpage=k_template_name>
<cms:set current_folder_id="<cms:gpc 'f' method='get' />" scope='global' />
<a href="<cms:show k_folder_link />" class="list-group-item d-flex justify-content-between align-items-start list-group-item-action <cms:if k_folder_id=current_folder_id>active</cms:if>">
<div class="me-auto">
<div class="fw-bold"><cms:show k_folder_title /></div>
</div>
<span class="badge text-bg-warning rounded-pill"><cms:show k_folder_pagecount /></span>
</a>
</cms:folders>
</div>
</div>
2.b. Displayed the following using a table:
- If there is no file uploaded, display "- No record found -"
- If there is a file available, display the title, attributes and the download link.
- According to the file_ext (as in securefile), display the icon in the download button, using font-text (Font Awesome)
- Code: Select all
<table class="table">
<thead>
<tr>
<th class="text-center" width="50px">
#
</th>
<th width="*">
File Details
</th>
<th class="text-center" width="120px">
Download
</th>
</tr>
</thead>
<tbody>
<cms:pages masterpage=k_template_name>
<cms:no_results>
<tr>
<td colspan='3' class="text-center">
- No record found -
</td>
</tr>
</cms:no_results>
<tr>
<td class="text-center">
<!-- Displays sequential serial number -->
<cms:show k_absolute_count />
</td>
<td>
<!-- Start: File Attributes -->
<div>
<cms:show k_page_title />
</div>
<div>
<small class="text-muted">
<cms:show description />
</small>
</div>
<div>
<small>
Author: <cms:show author />
</small>
</div>
<div>
<small>
Published On: <cms:date publication_date format='M d, Y' />
</small>
</div>
<!-- End: File Attributes -->
</td>
<td class="text-center align-middle">
<!-- Start: Download Link -->
<cms:show_securefile 'file_to_download' >
<cms:set my_file_link = "<cms:securefile_link file_id />" />
<a href="<cms:show my_file_link />" class="btn btn-outline-dark p-3" >
<cms:if (file_ext eq 'jpg' || file_ext eq 'jpeg' || file_ext eq 'png')>
<i class="fa-regular fa-image fa-lg"></i>
<cms:else_if file_ext eq 'pdf' />
<i class="fa-regular fa-file-pdf fa-lg"></i>
<cms:else_if (file_ext eq 'doc' || file_ext eq 'docx') />
<i class="fa-regular fa-file-word fa-lg"></i>
<cms:else_if (file_ext eq 'ppt' || file_ext eq 'pptx') />
<i class="fa-regular fa-file-powerpoint fa-lg"></i>
<cms:else_if (file_ext eq 'xls' || file_ext eq 'xlsx') />
<i class="fa-regular fa-file-excel fa-lg"></i>
<cms:else />
<i class="fa-regular fa-file-zipper fa-lg"></i>
</cms:if>
</a>
</cms:show_securefile>
<!-- Start: Download Link -->
</td>
</tr>
</cms:pages>
</tbody>
</table>
In the zip file attached, I have put the text file with links i mentioned earlier and also this template named sample-download.php.
Step #3: Testing it at your endAll you need to do to test the code:
3.a. Extract the "sample-download.php" file from the "CouchCMS All Links.zip"
3.b. Place it in your project root folder
3.c. Visit the template when logged into the CouchCMS admin panel as a super admin.
3.d. As per the CouchCMS <cms:pages> tag suggestion, unpublish the default entry.
3.e. On the clonable list view (in the admin panel) you will find a "Manage Folder" button on the top right corner. Click on it and add the categories of your choice. Just fill the title and hit save at the bottom.
3.f. Return to the clonable list view (by clicking the template name on the left side menu) and then add a new page. On the Add New page you will see the categories you created in 3.e. under a dropdown named "Folder".
3.g. Put the title, select the folder (category), fill the remaining regions, upload the file (with any of the following extensions: jpg,jpeg,png,doc,docx,ppt,pptx,xls,xlsx,pdf,zip).
3.h. Save the page entry.
3.I. visit the front end and refresh it and you can see the entire Download system in action.
Feel free to test it and let me know if this is what you are looking for.
Regards,
GXCPL (CTR)
NOTE: The can be an error during uploading a file, stating that "[b]The uploaded file exceeds the upload_max_filesize directive in php.ini[/b]", in this case you will need to use a smaller file or a file with the size defined in your local/ online server php.ini directive file or you can make changes to the php.ini file to allow uploading 5Mb or more size file.RESPONSE TO MAIL #1Genxcoders,
thanks for your reply and courtesy in sending me the example file and your step-by-step instructions.
Yes, you have correctly interpreted what I need to manage the files.
Having said that, I am amazed at the simplicity with which Couch with its few tags in the template can manage all this.
It is true that what I want to achieve is simple but for now I have not seen any other cms that through its tags can natively manage all this, most of the time you have to rely on plug-ins or worse, get your hands on the PHP code or other languages.
I did a test here locally on my PC (think with IIS which is not even officially supported and an old version of MySQL and everything worked wonderfully).
I take advantage of your experience with Couch to ask you this:
1) With this logic that you proposed, can I make sub-categories for a category?
2) Is it possible to keep the name of the original file after its upload?
3) Is it possible to change the path of the folders for uploading files based on the selected category?
Thanks again Genxcoders for your availability and kindness, attached you will find a screendump of the test done locally on my PC
Sergio
MAIL #2Greetings Sergio!
Thank you for getting back to me and confirming that I was able to correctly understand your requirement.
One of the reasons of me being with Couch for so long is indeed the fact that it is so simple to natively achieve a lot of things without the plug-ins. One of the reason I shifted from Wordpress/Joomla type of CMS's is the same, let aside their bloating up issue.
As you mentioned that you got things to work on IIS, which is not officially supported, is due to the fact that Microsoft allows running PHP on IIS. It has improved its support to PHP on its IIS and hence all works well. Also, for the MySQL version, Couch internally manages the support on latest and older versions. Henceeven MySQL did not give you trouble. Nevertheless, I would say, you should consider using an AMP stack for your operating system like WAMP (on Windows), LAMP (on Linux), MAMP (on Apple) or if you are looking for a cross platform solution XAMPP would be a great option.
I will now try to answer your questions:
1. With this logic that you proposed, can I make sub-categories for a category?Well the sub category creation is already in place. Go to the "manage folder" where you created Cat1 and Cat2. Now click "Add New" and create a category, say "Cat1.1", but before you save it, look at the second label "Parent Folder" click on the dropdown menu there, select, say, Cat1, and then save it. If you come to the front end, and refresh it, you will find that Cat1.1 has appeared there. Not only has it appeared, but it will appear right under Cat1. You can play along from there to see what happens when you create another category with a different "Parent Folder". Now if you add a download file entry to the new categories, you will find that the files will start appearing in their respective categories. That will bring us to a small glitch, or rather I should say an unexpected UX. When you add a file to download under Cat1.1, and you click on Cat1, the files under Cat1 and Cat1.1 will be displayed under Cat 1 and if you click on Cat1.1 only the file belonging to Cat1.1 will be displayed. There can be two possible solutions to this, viz.:
a. you can only upload files to the sub-categories, in this scenario you should remove the "href" from the main category.
b. we could handle it by showing sub-categories separately when viewing a main category. these sub-categories when clicked will display their own files to download list. I have attached a file with these changes (and the changes for your 2nd query too). Hope it helps. I have also added the folder level in " [ ] " to help understand the depth (you can remove it by removing the "<cms:show k_level />" code) and breadcrumbs for navigation (using Couch tags only).
2. Is it possible to keep the name of the original file after its upload?It is really simple. If you see line #112 I have used the following:
- Code: Select all
<cms:set my_file_link = "<cms:securefile_link file_id />" />
All you need to do is change this line to:
- Code: Select all
<cms:set my_file_link = "<cms:cloak_url file_id force_download='1' />" />
and your files will be downloaded with their original names.
3. Is it possible to change the path of the folders for uploading files based on the selected category?Internally Couch is keeping a track of all the files for you. So I really don't feel you will need to do this. Also, since we are using the "securefile region" the files are save to the following location "/couch/uploads/attachments/". I understand for the ease of management you would want to set segregate the files in folders, but I am afraid this wont be possible with securefile region. And as a suggestion I would request you to keep the securefile region in use as not only it doesnot allow access to files directly, and with cloak url the physical path of the file on the server is hidden, it also helps us to get the file extension easily and hence handle the file type icons.
Feel free to test it and let me know how it goes. I hope i have been able to answer all your queries to your satisfaction
Regards,
GXCPL (CTR)
RESPONSE TO MAIL #2Thanks again,
I confirm that your second example also works.
...
Lastly if you want and you think it's appropriate you can publish the solution you proposed to me on the forum so that it becomes available to everyone.
MAIL #3Dear Sergio,
Thanks for the confirmation.
Regards,
GXCPL (CTR)