Symfony2: Edit file upload - file-upload

I am using the cookbook article from symfony.com to implement a file upload option for images.
Now I want to load up other images to the entity.
The default strategy for editing is:
1. Fetch out of DB
2. Inject into Form
3. Persist
Somehow this strategy doesn't work anymore when using file uploads (doctrine doesn't execute the events)
What else could I do to make the articles with picture editable?

The cookbook does not handle updates, in particular in the case where only the file changes.
In this case, the PreUpdate event is not triggered, so you need to trigger $entity->preUpload() manually before the $em->persist($entity), so that the file upload gets handled in any case (preUpload will alter $entity->path so the persisting will occur)

If you change only the upload field the lifecycle not run the upload method, In the cookbook is reported the solution in a quote box as below:
The PreUpdate and PostUpdate callbacks are only triggered if there is
a change in one of the entity's field that are persisted. This means
that, by default, if you modify only the $file property, these events
will not be triggered, as the property itself is not directly
persisted via Doctrine. One solution would be to use an updated field
that's persisted to Doctrine, and to modify it manually when changing
the file.
add a dummy field to update in the controller before persist event as suggest by this duscussion:
https://github.com/symfony/symfony-docs/pull/564
public function setFile(UploadedFile $file)
{
$this->file = $file;
$this->updatedAt = new \DateTime();
}

I have was in similar situation.
I try to edit existing record in database with path to the file.
When i edit record i must upload new file, what is not comfortable for users.
In my solution i use variable tmp file for file hash and variable file name.
All needed operation i made in Action edit class.
Full example action class in bellow link
https://github.com/marekz/php_examples/wiki/Symfony-how-to-edit-attachment-form

Related

How to restore/load a DB saved state in Datatables?

I am using stateSaveCallback to save a Datatables state into the database. It submits the current state via AJAX to a script so I can save the JSON.
I can attach the save function to a button click like so:
$( ".save_state" ).click(function(e) {
e.preventDefault();
table.state.save();
});
But I cannot find any documentation on how to restore or load that script using a click. The closest reference I can find is on this page
https://datatables.net/reference/api/state.loaded() which references restoring the saved state.
I am using stateLoadCallback but I think this just does it on load. I would like to do it via a click, so users can save their state in the database and click restore to load it back later (we will offer multiple state saves, like a Saved Search, so they click to restore a saved search).
Thanks
After inspecting the source code, it looks like state loading is performed only once during initialization. Therefore your best bet is to use destroy option as destroy: true and re-initialize your data table on click.
I assume you have defined a callback using stateLoadCallback option that query the server upon re-initialization.

How do I create an Image component to upload an image and write it to the DAM?

I am trying to write a component based on the foundation Image component that will write the image to the DAM instead of the "local" jcr node on file upload. I also want it to activate the "DAM Update Asset" workflow so that it will create the different size renditions. Can you use a listener to write it to the DAM or is there another or better way to accomplish this?
I don't see a way to just do it within the component itself. But an EventListener could be triggered if someone uploads an image. In this EventListener you can move the image to a defined folder in DAM and start the workflows you want programmatically. Then you update the component so it references the new DAM asset instead of a directly attached nt:file.
Depending on which configuration of the image component you use and which browser is used, the upload is a bit different. The file usually first gets stored in /tmp and then moved. I am not sure it this only happens if the dialog is closed. So the safest way would be to await this event, eg. a add/change event on the jcr:lastModified property.

Jive 7: How to change profile data / action?

I am writing a plugin for Jive (SBS) 7 and want to add more data to the template for the user profile Overview page (i.e. /people/admin ). In Jive 6 I did overwrite the profile path in struts and added my own ViewProfile action. But this action seems to be called no more.
I also cannot even figure out where the templates I changed get their data from (soy/people/profile/{userProfile, header, head}.soy) or what action is responsible for.
So how can I add another property to the soy file that gets a custom property for the targetUser? (custom property = property saved in the database table jiveuserprop)
You need to create a plugin and then use the option. Then, you simply use jquery to add the extra stuff.
you can create an action that takes in information using getters or post and throw it into the user's extended properties. You can create another action that'll retrieve that info in json.
then, simply use jquery's getJson to grab the info and use selectors to show the data in the user profile.
Don't forget to use the $j(document).ready(function(){ // your code here }); to show the info
simple example:
<scipt>
$j(document).ready(function(){
$j("div#j-profile-header-details").append("<p>hello World</p>");
});
</script>
will append "hello world" under the user's email address / job title.
hope this helps. feel free to ask more questions if it doesn't make sense. here's a good link on writing the javascript part of the plugin: http://docs.jivesoftware.com/jive/7.0/community_admin/index.jsp?topic=/com.jivesoftware.help.sbs.online/developer/PluginXMLReference.html
I got an answer in the Jive Developer community:
profile is an action in Struts2. /people/username is a URL Mapper permutation
https://community.jivesoftware.com/thread/263660

Yii - Route without Controller

I'm using an existing upload script that require user authentication. However since I did not write the upload script, it's nearly impossible for me to read the source code and make it into separate view and controller file. The problem is if the script does not get routed by the bootstrap file, it has no access to the Yii variable and thus user log in information.
I tried to set a custom session variable when the user login. However it work barely because my custom session would expire before the session set by Yii.
Any help would be appreciated.
Because of the way the script is written I've only been able to find one way of doing this. It will involve re-writing some elements of the script.
Save the filemanager in protected/vendors.
You need a controller to handle the routing of the request. This will also give you the access control that you need. Call it FileUpload and create it where you normally create controllers in your project. Right at the start of the controller, before the class is declared, import the fileUpload files from it's previously saved location; Yii::import('application.vendors.*');
You need an action to handle the incoming request. Call this actionIndex. Give it the following code.
public function actionIndex() {
//Start capturing the output from the script
ob_start();
require_once('filemanager/dialog.php');
//Finish capturing output, and save to a variable
$output = ob_end_clean();
$this->render('index', array('output' => $output));
}
Then you need a view file. Call it 'output.php' and it just contains one line; <?php echo $output; ?>
This will render the html generated by the script, and hopefully contain it within your existing template.
Your first problem is that the script sends headers which aren't discarded by ob_start. You will need to delete these from the script. It also starts a session, which will throw an error 'Session already started', which can be cured by changing the first line of config.php to
if(!isset($_SESSION))
{
session_start();
}
Your next problem will be that none of the scripts and stylesheets are loaded, because the vendor hasn't used relative filepaths, and also because you've just deleted the headers. You will need to re-write lots of the script to include the necessary files. Fortunately, you now have access to Yii functions, so can use the asset manager to publish all the js and css files needed by the script.
Your final (hopefully!) problem will be the urls used by the script page. Currently they are all pointing to files within the script. You will need to rewrite these to use Yii routing. Fortunately, inside the main file dialog.php you should have access to all the normal Yii functions, so you can set $baseUrl as $this->createUrl() etc. If you need to add extra actions to the controller you can follow the pattern above to call other files, like the upload.php file in the script.
Hope that all works for you!
You are using a Framework with mvc pattern so controllers are preferred way to route requests .As per your problem i would suggest you to use htaccess file to do the routing to the required file and handle other files by Yii
copy code from existing source to new Yii Controler/Action ... done :D

Adding a custom field type to SugarCRM?

I'm trying to add a custom field type to our SugarCRM 6.2 instance. I want to add an "email link" type field. I want it to function like the URL field, but prefix every address with "mailto://" prefix instead of "http://".
It will be good if that field type is available in studio while creating new fields or in minimum, some custom code to achieve it.
What I did up to now:
- I've copied include/SugarFields/Fields/URL to include/SugarFields/Fields/Email
- In modules/ModuleBuilder/language/en_us.lang.php I've added a title for the email field
But unable to get anything working. Any help?
First of all you do not want to make any coding changes outside of the custom directory. Doing so outside of this directory is not upgrade safe.
Create a custom fields directory in your custom folder
Create new template files (.tpl) for your custom field
then you can just add a regular textfield in studio
then edit your viewdefs in your custom directory
in your tpl file concatenate the mailto:// to your text and just add the persons email address and it will take care of the rest for you
why you need custom field type for such small functionality? Just add your custom JS file in editviewdef.php. Then use some jQuery selector to perform certain action. For detail view, you can use view.detail.php to add required text in run time.
This will help you to store less data in database and display more in frontend.