How to use tinymce plugin? - cakephp

I have tried many times to use this plugin and I failed.
I am following documentation, but it does not work for me.
I am posting the simple code here, to know what wrong I am doing.
1-I put this plugin in this folder app/plugins
2- I add TinyMce helper to articles_controller
<?php
class ArticlesController extends AppController {
// good practice to include the name variable
var $name = 'articles';
// load any helpers used in the views
var $helpers = array('Html', 'Form','TinyMce.TinyMce');
/**
* index()
* main index page of the formats page
* url: /formats/index
*/
function index(){
// get all formats from database where status = 1
$articles = $this->Article->find("all") ;
$this->set('articles', $articles);
}
function admin_add() {
// if the form data is not empty
if (!empty($this->data)) {
// initialise the format model
$this->Article->save($this->data);
// set a flash message
$this->Session->setFlash('The Format has been saved');
// redirect
$this->redirect(array('action'=>'index'));
} else {
// set a flash message
$this->Session->setFlash('The Format could not be saved. Please, try again.','default', array('class' => 'flash_bad'));
}
}
}
?>
3- in the view file articles/admin_add.ctp I added the editor
// i think the problem in this code
<?php $this->TinyMce->editor(array(
'theme' => 'advanced'
)); ?>
<div class="formats form">
<?php echo $form->create('Article');?>
<fieldset>
<legend>Add a article</legend>
<?php
// create the form inputs
echo $this->Form->input('title');
echo $this->Form->input('content'); ?>
</fieldset>
<?php echo $form->end('Add');?>
</div>
<ul class="actions">
<li><?php echo $html->link('List Articles', array('action'=>'index'));?></li>
</ul>

You need to put tinymce files into your js assets
Then you have to add into section of your layout.
Then you'll need to init tinymce according to example provided on tinymce website (ex: full tinymce layout) and configure it according to your requirements.
I'd personally would not rely on such cake plugins, when actions required to get things working are not many and they are simple enough.

Related

How to get HashTag in Cakephp Form Submit?

I am working in cakephp 2.x
my current url is http://sitename.com/users/edit_profile#step-2 when i submit page i am not getting #setp-2
Code:
//Setp-2
<?php echo $this->form->create('User',array('action'=>'edit_profile')); ?>
<label>Smoke</label>
<?php $smoke_option = array('Heavy'=>'Heavy','Moderate'=>'Moderate','Light'=>'Light','Occasional'=>'Occasional','Non Smoker'=>'Non Smoker','Herb'=>'Herb');
echo $this->form->input('smoke', array('options' => $smoke_option,'default'=>$getProfile['User_detail']['smoke'],'label'=>false,'div'=>false,'empty'=>'No Answer'));?>
</div>
<?php echo $this->form->end();?>
UsersController.php
public function edit_profile(){
$this->layout='default';
if($this->request->is('post')){
$data = $this->request->data;
$data['User']['id']=$this->Session->read('Auth.User.id');
$updateProfile=$this->User->add_details($data);
if($updateProfile){
$this->Session->setFlash(__('Your Profile Updated Successfully'), 'success_message');
$this->redirect($this->referer());
}
}
}
How to getting hashtag in cakephp form submit?
Use
array('action'=>'edit_profile', '#' => 'step-2')
It appends #step-2 as you need in the url, similarly you can use your for ? also
The hash is never sent to the server.
ref - http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url
though you can set a hidden field and set the value = .
you can access the hash by document.location.hash

Image uploading using cakephp and store it in a different folder accoring to respective id

I am using cakephp 2.4.1 for my project.I am having two tables casts and castimage. Now castimage having two columns cast id and cast_image_path. now i have to upload image with respective to cast id and store it in different folders according to cast ids? so how would i do that and how i store the image path in database?
You add in your view file
view.ctp
<?php if (!empty($this->data['Contact']['filepath'])): ?>
<div class="input">
<label>Uploaded File</label>
<?php
echo $this->Form->input('filepath', array('type'=>'hidden'));
echo $this->Html->link(basename($this->data['Contact']['filepath']), $this->data['Contact']['filepath']);
?>
</div>
<?php else: ?>
<?php echo $this->Form->input('filename',array(
'type' => 'file'
)); ?>
In your controller
public function add(){
if ($this->request->is('post')) {
// create
$this->Castimage->create();
// attempt to save
if ($this->Castimage->save($this->request->data)) {
$this->Session->setFlash('image has been successfully saved!');
$this->redirect(array('action' => 'index'));
// form validation failed
} else {
// check if file has been uploaded, if so get the file path
if (!empty($this->Castimage->data['Castimage']['filepath']) && is_string($this->Castimage->data['Castimage']['filepath'])) {
$this->request->data['Castimage']['filepath'] = $this->Castimage->data['Castimage']['filepath'];
}
}
}
}
This plugin will do what you want: https://github.com/josegonzalez/cakephp-upload
$cast_id = $this->request->data['Image']['cast_id];
if directory is not created, create new one
if(!is_dir($cast_id) {
$oldumask = umask(0);
mkdir($cast_id, 0777);
umask($oldumask);
}
$uniq = mt_rand();
move_uploaded_file($this->requset->data['Image']['tmp_name'],WWW_ROOT.DS.$cast_id.DS.$uniq.'.'$this->request->data['Image']['name']);
I hope it gives you some idea

Passing Data from Dropbox Chooser to controller/model in Cakephp

EDIT:
I am getting the JSON results from the dropbox chooser, but they are not being parsed properly to be put into the database. I can't figure out what I am doing wrong, if I should be doing a JSON decoder in my controller or if it is something else.
Controller Code:
<?php class DropboxfilesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file is now available :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="Dropboxfilechooser" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}?>
The actual problem is looking at the source code, the form action is not going to the proper controller, it is just going to the cake/homes. Just change that and the problem is solved.

Passing data via POST from an element to controller/model Cakephp

I am pretty new to cakephp, I have an uploader form and would like to pass all of the data using POST to the controller and model so that it will save to the database. So far there are no cakephp errors occuring but no data is being passed.
Controller Code:
<?php class Controller extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file has been liberated :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}
}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="uploader" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}
?>
Please, check input specification you can see there, that there are no "uploader" value for type attribute. Maybe you mean type="file".
Also, if you want to upload files, please, use
echo $this->Form->create('File', array('type' => 'file'));
this will produce correct enctype attribute enctype="multipart/form-data". such enctype needed if you send files by POST.
Also, you use style: style="visibility: hidden;" which should hide file input field, so, maybe you use some flash uploader or ajax, but this is not demonstrated in your code. If you provide more code, we can provide more concrete help to you.
and counter question: how you debug your code?

Yii file upload is not validating file type

I am validating the file type to be uploaded. The validation rule does not seem to work. I only want to accept jpg files but when I try putting pdf files, it still accepts it and does not give any error. Please help. I don't know what I'm doing wrong.
View:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'document-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
'htmlOptions'=>array('enctype'=>'multipart/form-data'),)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'barangay_clearance'); ?>
<?php echo $form->fileField($model,'barangay_clearance'); ?>
<?php echo $form->error($model,'barangay_clearance'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
Model:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('barangay_clearance', 'file', 'allowEmpty'=>true, 'types'=>'jpg', 'message'=>'Jpg files only', 'on'=>'insert'),
array('barangay_clearance, barangay_status, zoning_clearance, zoning_status, sanitary_clearance, sanitary_status, occupancy_permit, occupancy_status, fire_safety, fire_safety_status', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('document_id, business_id, barangay_clearance, barangay_status, zoning_clearance, zoning_status, sanitary_clearance, sanitary_status, occupancy_permit, occupancy_status, fire_safety, fire_safety_status', 'safe', 'on'=>'search'),
);
}
Controller: (Note: I haven't really changed the controller yet except for activating the ajaxvalidation function and creating an instance of another model)
public function actionCreate($id)
{
$model=new Document;
$busModel = Business::model()->findByPk($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Document']))
{
$model->attributes=$_POST['Document'];
if($model->save())
$this->redirect(array('view','id'=>$model->document_id));
}
$this->render('create',array(
'model'=>$model,
'busModel'=>$busModel,
));
}
This should work perfectly.
array('barangay_clearance', 'file','types'=>'jpg', 'allowEmpty'=>true, 'on'=>'update', 'on'=>'insert'),
You didn't assign any scenario to model. If model you use in form widget is instance of Document then this:
$model=new Document;
has to be replaced with this:
$model=new Document('insert');
since rule declaration
array('barangay_clearance', 'file', 'allowEmpty'=>true, 'types'=>'jpg', 'message'=>'Jpg files only', 'on'=>'insert'),
says that it will be applied only on "insert" scenario.
I hope it helps.

Resources