Class 'JTable' not found - joomla

I am using Joomla 2.5.11 . I have a php file stored in the /public_html/joomtest/components/com_jumi/files which is pasted below.
I have a PHP form which is stored in the same location i.e /public_html/joomtest/components/com_jumi/files.
I want the PHP form to call the PHP script so that an article is created in Joomla. But whenever the PHP script is called, I receive the below error
Fatal error: Class 'JTable' not found
and the line on which Joomla throws error is
$table = JTable::getInstance('Content', 'JTable', array());
PHP script
<?php
$table = JTable::getInstance('Content', 'JTable', array());
$data = array(
'catid' => 8,
'title' => 'SOME TITLE',
'introtext' => 'SOME TEXT',
'fulltext' => 'SOME TEXT',
'state' => 0,
);
if (!$table->bind($data))
{
$this->setError($table->getError());
return false;
}
if (!$table->check())
{
$this->setError($table->getError());
return false;
}
if (!$table->store())
{
$this->setError($table->getError());
return false;
}
?>
</body>
</html>
I tried putting in
require_once('/libraries/joomla/database/table.php');
but this again didnt work. Please help.

You need to define path of table file you want to use. Use the following code for include the specific table. For example:
JTable::addIncludePath(JPATH_SITE.DS.'components'.DS.'com_content'.DS.'tables');
And then call your table like below:
$con_table = JTable::getInstance('Content', 'JTable', array());
Hope this will work. Good Luck.

Related

problem (my_form_validation) not working properly for upload input

hoping someone can help me solve this problem, thanks a lot for providing the solution
i recently had a problem with my library called my_form_validation on codeigniter version 3. i don't know why it can enter validation true even though it was wrong in function photo check
I have done several options but none of the results I want. my example already used the callback it's the same
example code controller
public function validation_photo()
{
// load custom library Validation
$this->my_form_validation->config_photo();
$this->my_form_validation->photo_checker($_FILES['photo']);
// Conditional validation form
if ($this->form_validation->run() == FALSE) {
// does not pass validation
$data['Title'] = 'Profile';
$data['Content'] = 'management/V_Profile';
$this->load->view('dinamis/Layout', $data);
} else {
// passed validation
var_dump('passed');
}
}
the code above is my loading from my_form_validation library which works for configuring and validating the upload format
example code my_form_validation (config_photo)
function config_photo()
{
$ci = get_instance();
$config = array(
array(
'field' => 'photo',
'label' => 'photo',
'rules' => 'photo_checker',
),
);
$ci->form_validation->set_rules($config);
}
the code above is to set the rules to be used, I use photo_checker
example code my_form_validation (photo_checker)
function photo_checker($photo)
{
$ci = get_instance();
$allowed_mime_type_arr = array('image/jpeg', 'image/png', 'image/x-png', 'image/jpg');
$mime = $photo['type'];
if (in_array($mime, $allowed_mime_type_arr)) {
return TRUE;
} else {
$ci->form_validation->set_message('photo_checker', 'Please select a supported format (jpeg,png,jpg).');
return FALSE;
}
}
The above code is for validating if it doesn't match the desired format then it returns false with the message set
very very thank you very much for helping me.
I hope this issue gets resolved well in this forum, I'm sure there are really great people here

Attach TCPDF to mail in Laravel

I want to attach pdf generated with tcpdf library without save.
I'm able to attach the pdf generated but it's corrupt.
I search a lot examples but any seems don't work
This my code:
public function index($id) {
$viaje = Viaje::find($id);
$users = User::orderBy('id', 'asc')->get();
// usersPdf is the view that includes the downloading content
$view = \View::make('usersPdf', ['viaje' => $viaje, 'users' => $users]);
$html_content = $view->render();
// Set title in the PDF
PDF::SetTitle("List of users");
PDF::AddPage();
PDF::writeHTML($html_content, true, false, true, false, '');
//PDF::Output('userlist.pdf');
$fileatt = PDF::Output($name='yourfilename.pdf', $dest='E');
$pdf = chunk_split($fileatt);
$contactopro = Contactoviajespro::find($id);
$data = [
'link' => 'http://',
'contacto' => $contactopro->name,
];
Mail::send('emails.notificacion', $data, function($msg) use($pdf) {
$msg->from('administracion#buendialogistica.com', 'Javier');
$msg->to('xavieeee#gmail.com')->subject('Notificación');
$msg->attachData($pdf, 'orden.pdf');
});
return redirect()->route('home')
->with(['message' => 'Email enviado correctamene']);
}
Use "S" to generate pdf and do not do chunk_split(), Laravel will do that. Additionally, if you are using queue() instead of send(), it will fail because of the attachment. To queue, write a job and send with the job queue.

laravel 5.3 sendgrid categories

In laravel 5.2 this worked just fine, but since migrating to 5.3 i'm having issues getting the category sent in my email.
public function build()
{
return $this->view('mail.enquiry')
->getSwiftMessage()->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))))
->subject('Website Enquiry')
->to(env('MAIL_DEFAULT_TO_EMAIL'), env('MAIL_DEFAULT_TO_NAME'))
->from(env('MAIL_DEFAULT_FROM_EMAIL'), env('MAIL_DEFAULT_FROM_NAME'))
->replyTo(\Request::get('email'), \Request::get('full_name'));
}
i get this error
BadMethodCallException in Mailable.php line 525:
Method [getSwiftMessage] does not exist on mailable.
every thing in this code works fine, but breaks as soon as I add this line:
->getSwiftMessage()->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))))
To achieve what you're after you can use the withSwiftMessage() method.
This method takes a callback which will be passed the instance of SwiftMessage:
->withSwiftMessage(function ($message) {
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))));
})
So your method would look something like:
public function build()
{
return $this->view('mail.enquiry')
->withSwiftMessage(function ($message) {
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array("category" => array(env('BUSINESS_NAME')))));
})
->subject('Website Enquiry')
->to(env('MAIL_DEFAULT_TO_EMAIL'), env('MAIL_DEFAULT_TO_NAME'))
->from(env('MAIL_DEFAULT_FROM_EMAIL'), env('MAIL_DEFAULT_FROM_NAME'))
->replyTo(\Request::get('email'), \Request::get('full_name'));
}
Hope this helps!
Since I visited this question when looking for a solution on Laravel 8 I'm adding some more info to the already accepted answer:
In Laravel 8 now you can follow instructions here: https://sendgrid.com/docs/for-developers/sending-email/laravel/ and check section titled "Adding a category or custom field".
It does still use withSwifthMessage() - more info for this on laravel's site: https://laravel.com/docs/8.x/mail#customizing-the-swiftmailer-message
The gist of it is simillar to the already accepted answer, but provides some nice helpers too and clearer code:
In your build() function:
$headerData = [
'category' => 'category',
'unique_args' => [
'variable_1' => 'abc'
]
];
$header = $this->asString($headerData);
$this->withSwiftMessage(function ($message) use ($header) {
$message->getHeaders()
->addTextHeader('X-SMTPAPI', $header);
});
return $this->view('emails.test')
->from($address, $name)
Helpers:
private function asJSON($data)
{
$json = json_encode($data);
$json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);
return $json;
}
private function asString($data)
{
$json = $this->asJSON($data);
return wordwrap($json, 76, "\n ");
}
Might be useful to someone.

Drupal 7 - Trying to add form to list view

sorry if this has been asked before, I looked around but haven't found this specific question on StackOverFlow.com.
I have a view called 'view-post-wall' which I'm trying to add the form that submits posts to this view called 'post' via ajax submit, though I haven't begun adding ajax yet.
My module's name is 'friendicate'
I don't understand what I'm missing here, I'm following a tutorial and have been unable to get past this issue for 2 days now.
I don't get any errors either.
Here is the module code in full
function _form_post_ajax_add() {
$form = array();
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Title of post',
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => 'description',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit post',
);
return $form;
}
function post_ajax_preprocess_page(&$variables) {
//krumo($variables);
$arg = arg();
if($arg[0] == 'view-post-wall') {
$variables['page']['content']['system_main']['main']['#markup'] = drupal_render(drupal_get_form('_form_post_ajax_add'));
}
}
There are multiple ways to accomplish this, and I'll outline those methods below. Also, if nothing works from my suggestions below, it's possible that you have an invalid form function name. Im not sure if that throws off Drupal or not. The correct format for the function name should end in _form and contain the arguments $form and $form_state, like so:
_form_post_ajax_add_form($form, &$form_state) { ... }
Also, if you want to use a hook, Steff mentioned in a comment to your question that you'll need to use your module name in the function name.
friendicate_preprocess_page(&$variables) { ... }
Ok, now for a few ideas how to get the form on the page.
Block
You can create a custom block within your module, and then assign it to a region in admin/structure/blocks
<?php
/**
* Implements hook_block_info().
*/
function friendicate_block_info() {
$blocks = array();
$blocks['post_ajax'] = array(
'info' => t('Translation Set Links'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function friendicate_block_view($delta = '') {
$block = array();
if ($delta == 'post_ajax') {
$form = drupal_get_form('_form_post_ajax_add_form');
$block['content'] = $form;
}
return $block;
}
Clear the cache and your block should appear in admin/structure/blocks
Views attachment before/after
You can add markup before and after a view using the Views hook hook_views_pre_render()
<?php
/**
* Implements hook_view_pre_render().
*/
function frendicate_views_pre_render(&$view) {
if($view->name == 'view_post_wall') { // the machine name of your view
$form = drupal_get_form('_form_post_ajax_add_form');
$view->attachment_before = render($form);
}
}
Or maybe use view post render
function friendicate_views_post_render(&$view, &$output, &$cache) {
//use the machine name of your view
if ($view->name == 'view_post_wall') {
$output .= drupal_render(drupal_get_form('_form_post_ajax_add'));
}
}

Yii validation with file upload failing

I'm having what looks to me some strange behaviour within Yii.
I have a simple file upload, that takes a name and the file itself.
If I just submit the form with no name or file I can bypass the validation (i.e - My controller action is called and is trying to process the uploaded file) I think my rules() are setup accordingly to stop this. These are my relevant rules:
public function rules() {
return array(
array('name file', 'required', 'message' => 'This field is required'),
array('file', 'file', 'on' => 'insert', 'allowEmpty' => false, 'safe'=>true,
'maxSize'=> 512000,
'maxFiles'=> 1,
'mimeTypes' => 'application/msword, text/plain',
'tooLarge'=> 'file cannot be larger than 500KB.',
'wrongMimeType'=> 'Format must be: .doc .txt'
),
I specified that the file is required and also within the file array that allowEmpty should be false. So what am I doing wrong here?
Thanks in advance for any help
Controller
public function actionCreate() {
$model = new File;
if (isset($_POST['File'])) {
$model->setAttributes($_POST['File']);
// Set file
$model->file = CUploadedFile::getInstance($model,'file');
// Set directory
$dest = Yii::getPathOfAlias('application.uploads');
$model->tmp_name = time();
$model->date_added = new CDbExpression('NOW()');
$model->file_type = $model->file->type;
$model->file_size = $model->file->size;
$model->extension = $model->file->extensionName;
if ($model->save()) {
$model->file->saveAs(($dest . '/' . $model->tmp_name . '.' . $model->file->extensionName));
Yii::app()->user->setFlash('success','<strong>Success!</strong> Your file has been uploaded');
}
}
$this->render('create', array( 'model' => $model));
}
For one you're missing a , in your first rule between name and file. Then you say:
I can bypass the validation (i.e - My controller action is called ...
From that i assume you use AJAX validation and expect the upload to fail. But you can't do AJAX validation on file uploads with CActiveForm.
So if you fix the typo above, you'll at least get AJAX validation for the name attribute.
You should maybe also remove the 'on'=>'insert' scenario. And you don't need the 'safe'=>true because you don't do massive assignment with the $model->file attribute.
For me I found that if I validate before I process the uploaded file it worked. Wasn't quite sure why I had to do that as I thought the save() method automatically called the validate() method
Validation will be performed before saving the record. If the validation fails, the record will not be saved. You can call getErrors() to retrieve the validation errors.
Updated code
public function actionCreate() {
$model = new File;
if (isset($_POST['File'])) {
$model->setAttributes($_POST['File']);
if($model->validate()){ // add in validation call
// Set file
$model->file = CUploadedFile::getInstance($model,'file');
// Set directory
$dest = Yii::getPathOfAlias('application.uploads');
$model->tmp_name = time();
$model->date_added = new CDbExpression('NOW()');
$model->file_type = $model->file->type;
$model->file_size = $model->file->size;
$model->extension = $model->file->extensionName;
if ($model->save()) {
$model->file->saveAs(($dest . '/' . $model->tmp_name . '.' . $model->file->extensionName));
Yii::app()->user->setFlash('success','<strong>Success!</strong> Your file has been uploaded');
}
}
}
$this->render('create', array( 'model' => $model));
}
Hope that helps anyone, thanks to #Michael Härtl too

Resources