Skip to Content Skip to Menu

CB Auto action to move a file

  • GUEST
7 years 9 months ago #284316 by
Hi guys

Does anyone have an example of a CB auto action PHP Method to copy or move a file.

We are using the file upload type field so users can upload a resume/cv.

I want to rename that file after upload adding a prefix of the users [firstname]_[lastname]_existingfilename.suffix

I have a FIELD action working on AftreUserUpdate and that adds the prefix to the field value fine.

So now just need the PHP to go in the new CODE auto action to check if file exists and if so MOVE it jfile::move giving it a name based on users name

Our resume field is called cb_resume

Here's what I've been trying in the PHP box and RETURN is set to SILENT

But page just crashes with no output so my syntax must be wrong :(
Code:
jimport('joomla.filesystem.file'); if (JFile::exists(JPATH_SITE.DS.'images'.DS.'comprofiler'.DS.'plug_cbfilefield'.DS.'[user_id]'.DS.'[cb_resume]')) { JFile::move(JPATH_SITE.DS.'images'.DS.'comprofiler'.DS.'plug_cbfilefield'.DS.'[user_id]'.DS.'[cb_resume]',JPATH_SITE.DS.'images'.DS.'comprofiler'.DS.'plug_cbfilefield'.DS.'[user_id]'.DS.'[firstname]_[lastname]_[userid]_[cb_resume]' ); }

Ideally we want to just grab the file extension (doc, docx, or pdf) and then add that after the [userid] so an example would be

jonathan_appleton_478.pdf

On future uploads we'd like to try and clean out previous files too but want to focus on getting this first bit working :)

Thanks in advance for any advice.
Jonathan

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 68609
  • Thanks: 9108
  • Karma: 1434
7 years 9 months ago #284340 by krileon
Replied by krileon on topic CB Auto action to move a file
I would just use normal PHP functions to rename it. Below will probably work (didn't test, adjust as needed). You should also have a condition to ensure cb_resume has a value to begin with as well. Note you need to enable format functions for the below to work as it utilizes a few to sanitize values.

Code:
$basePath = JPATH_SITE . '/images/comprofiler/plug_cbfilefield/[user_id]'; $oldPath = $basePath . '/[cb_resume]'; $newFilename = '[cb:parse function="clean" method="string"][firstname][/cb:parse]_[cb:parse function="clean" method="string"][lastname][/cb:parse]_[user_id]_[cb_resume]'; if ( file_exists( $oldPath ) ) { @rename( $oldPath, $basePath . '/' . $newFilename ); $user->set( 'cb_resume', $newFilename ); $user->store(); }

Please understand we are not here to write custom code for you. Examples of how to move or rename files are just standard PHP. You can find several tutorials for PHP regarding renaming, moving, etc.. This is not CB specific beyond plugging in the data via substitutions or finding the locations.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You:

Please Log in or Create an account to join the conversation.

  • GUEST
7 years 9 months ago #284343 by
Replied by on topic CB Auto action to move a file
Krileon - YOU ARE THE BEST! :)

THANK YOU so much...... I will try this today

I know you aren't here to offer customised code but I just needed that helping hand to show me what was possible and correct syntax as I had no idea you could use $user-> set $user->store etc as would have written it all from scratch.

I spent over 6 hours attempting various things yesterday going around in circles....

That code probably took you ten minutes as you are the CB guru so I THANK YOU SO MUCH.

I want to delete previous versions of the file upload as well as have noticed that the file upload doesnt do that so I can probably do that now using a onbeforeupdate type action to grab the old filename.

PLEASE DO PM me if you have a paypal email address I can send you some beer money to!

you've really been very helpful

Thanks again

Jonathan

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 68609
  • Thanks: 9108
  • Karma: 1434
7 years 9 months ago #284352 by krileon
Replied by krileon on topic CB Auto action to move a file

I know you aren't here to offer customised code but I just needed that helping hand to show me what was possible and correct syntax as I had no idea you could use $user-> set $user->store etc as would have written it all from scratch.

Code actions have access to $trigger, $user, and $vars variables. The $vars variable actually has direct access to the variables sent by the trigger as well and if set to be references under parameters can actually be modified by reference. There's a lot of nifty usages that can come of this.

I want to delete previous versions of the file upload as well as have noticed that the file upload doesnt do that so I can probably do that now using a onbeforeupdate type action to grab the old filename.

It's using a rename so you don't need to worry about duplicate copies of a file. The old file anytime a profile is updated is also automatically deleted when a new file is uploaded so there should be no issues with leftover files. If it's not cleaning up old files please let me know and will take a look at source to see if that process maybe failing.

PLEASE DO PM me if you have a paypal email address I can send you some beer money to!

Donations are always welcomed and appreciated. See the donation plan on the subscription tab of your profile. ;) Glad I could help :)


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You:

Please Log in or Create an account to join the conversation.

  • GUEST
7 years 9 months ago #284370 by
Replied by on topic CB Auto action to move a file
Beer money just donated thanks again!

Is their any online documentation about all the CB functions etc you can use... $trigger, $user, and $vars

I'll test this out tomorrow :)

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 68609
  • Thanks: 9108
  • Karma: 1434
7 years 9 months ago - 7 years 9 months ago #284373 by krileon
Replied by krileon on topic CB Auto action to move a file

Beer money just donated thanks again!

Thank you :)

Is their any online documentation about all the CB functions etc you can use... $trigger, $user, and $vars

Unfortunately no, not at this time. $trigger is the auto action object and $user is the CB user object built from the trigger. $vars however contains var1, var2, var3, etc.. as an array which outputs each variable sent to the trigger. So $vars gives you whatever the first variable is for the trigger used. This can be useful if lets say the trigger has an object you want to manipulate by reference. You could in theory do $vars->set( 'name', $value ); type usage.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
Last edit: 7 years 9 months ago by krileon.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum

Facebook Twitter LinkedIn