when I updating my values a ReflectionException occurs - laravel

I want to update actualCost
ProposalExpenses::where('EXPENSE_ID', '=', $request->id)
->update(['ACTUAL_EXPENSES' => $request->actualCost]);
//this my function
public function ProgrameLogExpUploadFiles(Request $request, $proposal_id) {
$officer = officer::where('USER_LOGIN_LOGIN_ID', '=', Auth::user()->LOGIN_ID)->first();
if (isset($request->id)) {
ProposalExpenses::where('EXPENSE_ID', '=', $request->id)
->update(['ACTUAL_EXPENSES' => $request->actualCost]);
if ($request->updated == "true" && $request->balance <= 0) {
$old_files_db = \App\model\programme_proposal\ProgrammeExpenseFileUpload::where([
'PROPOSAL_ID'=>$proposal_id,
'EXPENCE_ID'=>$request->id
])->get();
\App\model\programme_proposal\ProgrammeExpenseFileUpload::where([
'PROPOSAL_ID'=>$proposal_id,
'EXPENCE_ID'=>$request->id
])->delete();
if(isset($old_files_db)){
foreach ($old_files_db as $file) {
try {
\Illuminate\Support\Facades\Storage::delete("programme_expenses/" . $file->STORAGE_FILE_NAME);
} catch (\Exception $e) {
}
}
}
foreach ($request->file as $file) {
$upload_success = $file->store('programme_expenses');
if ($upload_success) {
\App\model\programme_proposal\ProgrammeExpenseFileUpload::create([
'STORAGE_FILE_NAME' => str_replace("programme_expenses/", "", $upload_success),
'REAL_NAME' => $file->getClientOriginalName(),
'UPLOADED_DATE' => \Illuminate\Support\Carbon::now(),
'UPLOADED_BY_STAFF_NIC' => $officer->NIC,
'PROPOSAL_ID' => $proposal_id,
'EXPENCE_ID' => $request->id
]);
}
}
}
if ($request->updated == "false" && $request->balance > 0) {
$old_files_db = \App\model\programme_proposal\ProgrammeExpenseFileUpload::where(['PROPOSAL_ID'=>$proposal_id,'EXPENCE_ID'=>$request->id])->get();
\App\model\programme_proposal\ProgrammeExpenseFileUpload::where(['PROPOSAL_ID'=>$proposal_id,'EXPENCE_ID'=>$request->id])->delete();
if(isset($old_files_db)){
foreach ($old_files_db as $file) {
try {
\Illuminate\Support\Facades\Storage::delete("programme_expenses/" . $file->STORAGE_FILE_NAME);
} catch (\Exception $e) {
}
}
}
}
}
}

Related

laravel request ajax always return false

i have problem working with laravel request->ajax() its always return false. so the if statement is not working on what function is to be called. Im using datatables to render the table.
this code i have is not working properply
ContactController.php
public function index()
{
$type = request()->get('type');
$types = ['supplier', 'customer'];
if (empty($type) || !in_array($type, $types)) {
return redirect()->back();
}
if (request()->ajax()) {
if ($type == 'supplier') {
return $this->indexSupplier();
} elseif ($type == 'customer') {
return $this->indexCustomer();
} else {
die("Not Found");
}
}
$reward_enabled = (request()->session()->get('business.enable_rp') == 1 && in_array($type, ['customer'])) ? true : false;
return view('contact.index')
->with(compact('type', 'reward_enabled'));
}
private function indexSupplier()
{
if (!auth()->user()->can('supplier.view') && !auth()->user()->can('supplier.view_own')) {
abort(403, 'Unauthorized action.');
}
$business_id = request()->session()->get('user.business_id');
$contact = $this->contactUtil->getContactQuery($business_id, 'supplier');
if (request()->has('has_purchase_due')) {
$contact->havingRaw('(total_purchase - purchase_paid) > 0');
}
if (request()->has('has_purchase_return')) {
$contact->havingRaw('total_purchase_return > 0');
}
if (request()->has('has_advance_balance')) {
$contact->where('balance', '>', 0);
}
if (request()->has('has_opening_balance')) {
$contact->havingRaw('opening_balance > 0');
}
return Datatables::of($contact)
...
}
private function indexCustomer()
{
if (!auth()->user()->can('customer.view') && !auth()->user()->can('customer.view_own')) {
abort(403, 'Unauthorized action.');
}
$business_id = request()->session()->get('user.business_id');
$is_admin = $this->contactUtil->is_admin(auth()->user());
$query = $this->contactUtil->getContactQuery($business_id, 'customer');
return Datatables::of($query)
...
}
this is the sidebarblade that links to the controller.
if (auth()->user()->can('supplier.view') || auth()->user()->can('supplier.view_own')) {
$sub->url(action('ContactController#index', ['type' => 'supplier']),__('report.supplier'),
['icon' => 'fa fas fa-star', 'active' => request()->input('type') == 'upplier']);
}
if (auth()->user()->can('customer.view') || auth()->user()->can('customer.view_own')) {
$sub->url(action('ContactController#index', ['type' => 'customer']),__('report.customer'),
['icon' => 'fa fas fa-star', 'active' => request()->input('type') == 'customer']);
$sub->url(action('CustomerGroupController#index'),__('lang_v1.customer_groups'),
['icon' => 'fa fas fa-users', 'active' => request()->segment(1) == 'customer-group']);
}

How to call Laravel helper method in Config/Constants.php

I am giving this error
Call to undefined function getDefaultConfigByHost()
In constants.php line 5:
In helper.php
function getDefaultConfigByHost($developmentValue = null, $testValue = null,
$productionValue = null)
{
try {
if (! isset($_SERVER['HTTP_HOST'])) {
return null;
}
$domain = $_SERVER['HTTP_HOST'];
switch ($domain) {
case 'xxx.gglemd.com':
return $developmentValue;
case '192.168.1.18:8001':
return $testValue;
case 'yyy.service.com':
return $productionValue;
default:
return '';
}
} catch (Exception $exception) {
return '';
}
}
In constants.php
<?php
return [
'api' => [
'base' => env('API_BASE', getDefaultConfigByHost(),
'sfile_base' => env('SXFILE_API_BASE', getDefaultConfigByHost('')),
'private_key' => env('API_REQUEST_PRIVATE_KEY', 'bn-sjaddasdsadsadasdas'),
],
];

What might be best solution to get camelCase data from angular,integrate with snake_case mysql and send response in camelCase back to angular?

what i did is:
$input = $request->all();
//convert the post camelcase to snakecase
foreach ($input as $key => $val) {
$newKey = snake_case($key);
$input[$newKey] = $val;
if ($newKey != $key) {
unset($input[$key]);
}
}
after integrating data,use helper function to convert snakecase to camelcase again by the following function:
function convert_snakeCase_to_CamelCase($data)
{
if ($data) {
foreach ($data as $key1 => $val1) {
$newKey1 = camel_case($key1);
if ($newKey1 != $key1) {
unset($data[$key1]);
}
$data[$newKey1] = $val1;
if (is_array($val1) && !empty($val1)) {
foreach ($val1 as $key2 => $val2) {
$newKey2 = camel_case($key2);
if ($newKey2 != $key2) {
unset($data[$newKey1][$key2]);
}
$data[$newKey1][$newKey2] = $val2;
if (is_array($val2) && !empty($val2)) {
foreach ($val2 as $key3 => $val3) {
$newKey3 = camel_case($key3);
if ($newKey3 != $key3) {
unset($data[$newKey1][$newKey2][$key3]);
}
$data[$newKey1][$newKey2][$newKey3] = $val3;
if (is_array($val3) && !empty($val3)) {
foreach ($val3 as $key4 => $val4) {
$newKey4 = camel_case($key4);
if ($newKey4 != $key4) {
unset($data[$newKey1][$newKey2][$newKey3][$key4]);
}
$data[$newKey1][$newKey2][$newKey3][$newKey4] = $val4;
if (is_array($val4) && !empty($val4)) {
foreach ($val4 as $key5 => $val5) {
$newKey5 = camel_case($key5);
if ($newKey5 != $key5) {
unset($data[$newKey1][$newKey2][$newKey3][$newKey4][$key5]);
}
$data[$newKey1][$newKey2][$newKey3][$newKey4][$newKey5] = $val5;
if (is_array($val5) && !empty($val5)) {
foreach ($val5 as $key6 => $val6) {
$newKey6 = camel_case($key6);
if ($newKey6 != $key6) {
unset($data[$newKey1][$newKey2][$newKey3][$newKey4][$newKey5][$key6]);
}
$data[$newKey1][$newKey2][$newKey3][$newKey4][$newKey5][$newKey6] = $val6;
}
}
}
}
}
}
}
}
}
}
}
}
return $data;
}
its working absolutely fine.I was wondering is there any solution better plugins for angular or laravel to convert the post data and send response data in required case?

Return false limits multiple error message to one?

On my multiple upload library, I have a set error function.
On my upload function I use a in_array to check file extensions. If the in_array detects error it displays multiple error messages correct.
The problem I am having is for some reason when I use return FALSE; under the $this->set_error('file_extension_not_allowed') then will on display one message. Not sure why return FALSE limits error messages.
Question: How is it possible to use my return false but be able to display multiple message correct.
<?php
class Multiple_upload {
public $set_errors = array();
public function __construct($config = array()) {
$this->CI =& get_instance();
$this->files = $this->clean($_FILES);
empty($config) OR $this->set_config($config);
}
public function set_config($config) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
return $this;
}
public function upload($field = 'userfile') {
$allowed_extension = explode('|', $this->allowed_types);
if (empty($this->upload_path)) {
$this->set_error('upload_path_not_set', 'upload_path_check');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path)) {
$this->set_error('upload_path_in_correct', 'location_check');
return FALSE;
}
if (!empty($this->files[$field]['name'][0])) {
foreach ($this->files[$field]['name'] as $key => $value) {
$this->file_name = $this->files[$field]['name'][$key];
$get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($get_file_extension));
$array_1 = array(
$allowed_extension,
);
$array_2 = array(
$get_file_extension[1],
);
if (!in_array($array_2, $array_1)) {
$this->set_error('file_extension_not_allowed', 'extension_check');
return FALSE;
}
}
return $this;
}
}
public function set_error($message, $type) {
$this->CI->lang->load('upload', 'english');
$this->error_message[] = $this->CI->lang->line($message);
return $this;
}
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {
foreach($this->error_message as $msg) {
var_dump($msg);
}
}
public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);
$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
}
return $data;
}
}
Maybe this can help...
public function upload($field = 'userfile')
{
$allowed_extension = explode('|', $this->allowed_types);
if (empty($this->upload_path))
{
$this->set_error('upload_path_not_set', 'upload_path_check');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path))
{
$this->set_error('upload_path_in_correct', 'location_check');
return FALSE;
}
if (!empty($this->files[$field]['name'][0]))
{
$check_error = 0;//added this
foreach ($this->files[$field]['name'] as $key => $value)
{
$this->file_name = $this->files[$field]['name'][$key];
$get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($get_file_extension));
$array_1 = array(
$allowed_extension,
);
$array_2 = array(
$get_file_extension[1],
);
if (!in_array($array_2, $array_1))
{
$this->set_error('file_extension_not_allowed', 'extension_check');
$check_error++;
}
}
if($check_error > 0 )
{
return FALSE;
}
return $this;
}
}

Fatal error:Class 'Mage_Adminhtml_Controller_action' not found in BookmarksController.php on line 4

it shows:
Fatal error: Class 'Mage_Adminhtml_Controller_action' not found in .../app/code/local/Magentix/SocialBookmarking/controllers/Adminhtml/BookmarksController.php on line 4
I check it and find that nothing is in bookmarkscontroller.php.on line 4. What's wrong is it?
and I also check it that the social bookmarket plugin still shows in the front page here.
Original code:
<?php
/** http://www.magentix.fr **/
class Magentix_SocialBookmarking_Adminhtml_BookmarksController extends Mage_Adminhtml_Controller_action {
protected function _initAction() {
$this->loadLayout()
->_setActiveMenu('cms/socialbookmarking')
->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
return $this;
}
public function indexAction() {
$this->_initAction()->renderLayout();
}
public function editAction() {
$id = $this->getRequest()->getParam('id');
$model = Mage::getModel('socialbookmarking/bookmarks')->load($id);
if ($model->getId() || $id == 0) {
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
Mage::register('socialbookmarking_data', $model);
$this->loadLayout();
$this->_setActiveMenu('cms/socialbookmarking');
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('socialbookmarking/adminhtml_bookmarks_edit'))
->_addLeft($this->getLayout()->createBlock('socialbookmarking/adminhtml_bookmarks_edit_tabs'));
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('socialbookmarking')->__('Bookmark does not exist'));
$this->_redirect('*/*/');
}
}
public function newAction() {
$this->_forward('edit');
}
public function saveAction() {
if ($data = $this->getRequest()->getPost()) {
if(isset($_FILES['bookmarkimage']['name']) && $_FILES['bookmarkimage']['name'] != '') {
try {
$uploader = new Varien_File_Uploader('bookmarkimage');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media').DS.'social';
$uploader->save($path, $_FILES['bookmarkimage']['name']);
} catch (Exception $e) {
}
$data['image'] = 'social/'.$_FILES['bookmarkimage']['name'];
}
if(isset($data['bookmarkimage']['delete'])) $data['image'] = '';
$model = Mage::getModel('socialbookmarking/bookmarks');
$model->setData($data)->setId($this->getRequest()->getParam('id'));
try {
if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
$model->setCreatedTime(now())->setUpdateTime(now());
} else {
$model->setUpdateTime(now());
}
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('socialbookmarking')->__('Bookmark was successfully saved'));
Mage::getSingleton('adminhtml/session')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$this->_redirect('*/*/edit', array('id' => $model->getId()));
return;
}
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setFormData($data);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('socialbookmarking')->__('Unable to find bookmark to save'));
$this->_redirect('*/*/');
}
public function deleteAction() {
if( $this->getRequest()->getParam('id') > 0 ) {
try {
$model = Mage::getModel('socialbookmarking/bookmarks');
$model->setId($this->getRequest()->getParam('id'))->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('socialbookmarking')->__('Bookmark was successfully deleted'));
$this->_redirect('*/*/');
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
public function massDeleteAction() {
$socialbookmarkingIds = $this->getRequest()->getParam('socialbookmarking');
if(!is_array($socialbookmarkingIds)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('socialbookmarking')->__('Please select bookmark(s)'));
} else {
try {
foreach ($socialbookmarkingIds as $socialbookmarkingId) {
$socialbookmarking = Mage::getModel('socialbookmarking/bookmarks')->load($socialbookmarkingId);
$socialbookmarking->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('adminhtml')->__(
'Total of %d record(s) were successfully deleted', count($socialbookmarkingIds)
)
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massStatusAction() {
$socialbookmarkingIds = $this->getRequest()->getParam('socialbookmarking');
if(!is_array($socialbookmarkingIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select bookmark(s)'));
} else {
try {
foreach ($socialbookmarkingIds as $socialbookmarkingId) {
$socialbookmarking = Mage::getSingleton('socialbookmarking/bookmarks')
->load($socialbookmarkingId)
->setStatus($this->getRequest()->getParam('status'))
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($socialbookmarkingIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream') {
$response = $this->getResponse();
$response->setHeader('HTTP/1.1 200 OK','');
$response->setHeader('Pragma', 'public', true);
$response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
$response->setHeader('Content-Disposition', 'attachment; image='.$fileName);
$response->setHeader('Last-Modified', date('r'));
$response->setHeader('Accept-Ranges', 'bytes');
$response->setHeader('Content-Length', strlen($content));
$response->setHeader('Content-type', $contentType);
$response->setBody($content);
$response->sendResponse();
die;
}
}
Your extended class name is misspelled - Mage_Adminhtml_Controller_action should be Mage_Adminhtml_Controller_Action.
Make sure you:
included the file in bookmarkscontroller.php
put BookMarksController is in the right place

Resources