Extending Backpack Admin ArticleCrudController - laravel-backpack

I am trying to extend Backpack\NewsCRUD\app\Http\Controllers\Admin\ArticleCrudController so that I can add a new field. But it does not show a new field. What am I doing wrong here?
namespace App\Http\Controllers\Admin;
use Backpack\NewsCRUD\app\Http\Controllers\Admin\ArticleCrudController as OriginalArticleCrudController;
class ArticleCrudController extends OriginalArticleCrudController
{
public function __construct()
{
parent::__construct();
$this->crud->addField([
'name' => 'thumb',
'label' => 'Thumb',
'type' => 'browse',
]);
}
}
It seems the link in resources/vendor/backpack/base/inc/sidebar.php is not pointing to this new ArticleCrudController.
<ul class="treeview-menu">
<li><i class="fa fa-newspaper-o"></i> <span>Articles</span></li>
<li><i class="fa fa-list"></i> <span>Categories</span></li>
<li><i class="fa fa-tag"></i> <span>Tags</span></li>
</ul>

I needed to add a route to routes/web.php to make it work.
// Admin Interface Routes
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
...
...
// Backpack\NewsCRUD
CRUD::resource('article', 'Admin\ArticleCrudController');
});

Related

How to display the componentProperties options by the selected option in the dropdown?

I'm sure this is very simple but it's proving just a bit beyond me at the moment.
I have made a plugin that I would like to use for displaying galleries which is working fine. However, trying to add the options of the galleries that I have created in my component is proving to be difficult.
When I add the component to a page, I have now got the option to choose all the galleries that I created but displaying the gallery based upon which one I selected is what I have been unsuccessful in doing.
Any help would be greatly appreciated!
I'm sure this is very simple but it's proving just a bit beyond me at the moment.
I have made a plugin that I would like to use for displaying galleries which is working fine. However, trying to add the options of the galleries that I have created in my component is proving to be difficult.
When I add the component to a page, I have now got the option to choose all the galleries that I created but displaying the gallery based upon which one I selected is what I have been unsuccessful in doing.
Any help would be greatly appreciated!
Components/Gallery.php:
use Cms\Classes\ComponentBase;
use MartinSmith\Gallerys\Models\Gallery as GalleryModel;
class gallerys extends ComponentBase
{
public $gallery;
public function componentDetails(){
return [
'name' => 'Frontend Gallery',
'description' => 'A gallery for you webpage'
];
}
public function defineProperties() {
$lists = $this->getLists();
return [
'galleryName' => [
'title' => 'Gallery',
'type' => 'dropdown',
'placeholder' => 'Select Gallery',
'options' => $lists
]
];
}
public function getLists() {
$agreements = GalleryModel::all()->pluck('name', 'id');
return $agreements->toArray();
}
public function getList() {
$agreement = GalleryModel::where('id', $this->property('galleryName'))->get();
return $agreement->first();
}
}
Components/gallery/default.htm:
{% set gallerys = __SELF__.gallery %}
{% for gallery in gallerys %}
<div class="container-fluid px-0">
<div class="gallery">
<div class="row">
{% for image in gallery.fullImage %}
<div class="col-md-4 px-0 home-galleryImg">
<a href="{{ image.path }}">
<div class="gallery-imgOverlay">
<p>{{ image.title }}</p>
<h5>{{ image.description }}</h5>
</div>
<img class="img-fluid" src="{{ image.thumb(650,auto) }}" alt="{{ thumbnail.description }}">
</a>
</div>
{% endfor %}
</div>
</div>
</div>
{% endfor %}
See screenshot
I solved this for myself by creating a function that returns the "name" and indexed by the 'id' using the laravel pluck method. pluck('name', 'id') The first argument selects the column to use as the value and the second argument selects the column to use as a key. Note* the toArray() method I don't think the options field can take collections.
public function getLists() {
$agreements = Agreements::all()->pluck('agrnum', 'id');
return $agreements->toArray();
}
//returns
array:3 [▼
2 => "DLE-2"
4 => "DLE-1"
5 => "DLE-3"
]
Now in my properties area I call the function $list = $this->getList();
public function defineProperties() {
$lists = $this->getLists();
return [
'getList' => [
'title' => 'List',
'type' => 'dropdown',
'placeholder' => 'Select List',
'options' => $lists
]
];
}
After that you can proceed to do a Lists::where('id', $this->property('getList')); or something of that sort in a function to show the selected list or in your case gallery.
My results:
The CMS Page Backend from component
public function defineProperties() {
$lists = $this->getLists();
return [
'getList' => [
'title' => 'List',
'type' => 'dropdown',
'placeholder' => 'Select List',
'options' => $lists
]
];
}
public function getLists() {
$agreements = Agreements::all()->pluck('agrnum', 'id');
return $agreements->toArray();
}
public function getList() {
$agreement = Agreements::where('id', $this->property('getList'))->get();
return $agreement->first();
}
The Webpage from default.htm in the component template folder
{{ d(__SELF__.getList) }}
Also if I do {{ d(__SELF__.property('getList')) }} it shows me the value is "5".

Can't insert data into Mongodb in Laravel

Let me explain all the step that I followed. I am using LAMP.
First of all I installed Laravel , MongoDB and jenssegers/laravel-mongodb pakage. For this I followed this link.
After that I create database, table and insert data using terminal with all success.
Next step is to integrate mongodb with laravel so I add MongoDB connection detail in app/config/database.php file.
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'usedgoodstore'),
],
'default' => env('DB_CONNECTION', 'mongodb'),
Up to this point all work fine.
Next I create route, view, controller, model file and insert code.
routes/web.php
Route::post('/index', 'UserController#index');
welcome.blade.php
<form action="{{ url('/index') }}" method="post">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
UserController.php
<?php
namespace App\Http\Controllers;
use App\Model\User;
use Illuminate\Http\Request;
class UserController extends Controller{
protected $user;
public function __construct(User $user){
$this->user = $user;
}
public function Index(Request $request){
$data = array('firstname' => $request['firstname'], 'lastname' => $request['lastname']);
$user = $this->user->PostUser($data);
return response()->json($user,200);
}
}
User.php (model file)
<?php
namespace App\Model;
use Jenssegers\Mongodb\Eloquent\Model as Moloquent;
use DB; // if I use and not use this statement and then getting different errors
class User extends Moloquent {
protected $connection = 'mongodb';
protected $collection = 'user';
public function PostUser($data){
$insertData = DB::collection('user')->insert($data); // I'm getting error on this line.
if($insertData){
return true;
}
}
}
Error I'm getting is :
FatalThrowableError in User.php line 25:
Class 'App\Model\DB' not found
If I add use db in User.php (model file) I'm getting below error.
FatalThrowableError in DatabaseManager.php line 317:
Call to undefined method Illuminate\Database\MySqlConnection::collection()
What have I missed?
First open app/config/database.php file and make your mongodb default database.
'default' => 'mongodb',
and if you don't want mongodb as a default database connection then you can do following thing.
public function PostUser($data){
$insertData = DB::connection('mongodb')->collection('user')->insert($data); // I'm getting error on this line.
if($insertData){
return true;
}

TinyMCE CakePHP 3.0+ not selecting textarea (nothing happens)

CakePHP seems to be loading the plugin because I'm not getting any errors when loading from bootstrap, including the helper in the control and then using it in the view, but nothing happens to my <textarea></textarea> tags which I placed before and after the script in the view. I have also loaded jQuery. Any ideas on what might be wrong?
Controller:
public $helpers = ['TinyMCE.TinyMCE'];
View:
<textarea></textarea>
$this->TinyMCE->editor(array('theme' => 'advanced'));
<textarea></textarea>
HTML (view source, when the page is loaded):
<textarea>
</textarea>
<script>
//<![CDATA[
tinymce.init({
script : "/TinyMCE/js/tiny_mce/tiny_mce.js",
load_script : "1",
theme : "advanced"
});
//]]>
</script>
<textarea>
</textarea>
Bootstrap:
Plugin::load('TinyMCE', ['autoload' => true]);
Configure::write('TinyMCE.configs', array(
'advanced' => array(
'mode' => 'textareas',
'theme' => 'advanced')));
I also just noticed, when I pr($this->TinyMCE);
I get:
TinyMCE\View\Helper\TinyMCEHelper Object
(
[helpers] => Array
(
[0] => Html
)
[theme] =>
[plugin] =>
[fieldset] => Array
(
)
[tags] => Array
(
)
[implementedEvents] => Array
(
[View.beforeRender] => beforeRender
)
[_config] => Array
(
)
)
For some reason there is nothing in [theme] value, it should be - advanced. What am I doing wrong? Is something wrong with this line:
$this->TinyMCE->editor(array('theme' => 'advanced'));
Helper TinyMCE for CakePHP 2 with preset function.
TinyMCE
Download TinyMCE : http://www.tinymce.com/download/download.php
Copy /tinymce/jscripts/ tiny_mce folder to /app/webroot/js (like : /app/webroot/js/tiny_mce).
Controller
public $helpers = array('Tinymce');
Behavior
$this->Tinymce->input($Model.fieldName, $options = array(), $tinyoptions = array(), $preset = null)
Example
<div class="posts form">
<?php echo $this->Form->create('Post');?>
<fieldset>
<legend><?php echo __('Add Post'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Tinymce->input('Post.content', array(
'label' => 'Content'
),array(
'language'=>'en'
),
'bbcode'
);
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
http://bakery.cakephp.org/2012/04/11/Helper-TinyMCE-for-CakePHP-2.html#

Yii2 Modal Dialog on Gridview update button not load the update form

With reference to How to implement Yii2 Modal Dialog on Gridview view and update button?. I did my code below, but it popups a window with no update form. could anyone please let me know the correct code? (i use Kartik Gridview)
This is the code for column:
['class' => '\kartik\grid\ActionColumn',
'template'=>'{update}{delete}{view}',//{view}'//{view}{delete}',
'headerOptions' => ['width' => '20%', 'class' => 'activity- update-link',],
'contentOptions' => ['class' => 'padding-left-5px'],
'buttons' => [
'update' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>','/projects/update?id='.$key.'', [
'class' => 'activity-update-link',
'title' => Yii::t('yii', 'Project Update'),
'data-toggle' => 'modal',
'data-target' => '#activity-modal',
'data-id' => $key,
'data-pjax' => '0',
]);
},
],
],
This is JS code:
<?php $this->registerJs(
'
function init_click_handlers(){
$(".activity-update-link").click(function(e) {
var fID = $(this).closest("tr").data("key");
$.get(
"update",
{
id: fID
},
function (data)
{
$("#activity-modal").find(".modal-body").html(data);
$(".modal-body").html(data);
$("#activity-modal").modal("show");
}
);
});
}
init_click_handlers(); //first run
$("#project_pjax_id").on("pjax:success", function() {
init_click_handlers(); //reactivate links in grid after pjax update
});
');
?>
<?php
Modal::begin([
'id' => 'activity-modal',
'header' => '<h4 class="modal-title">Update Project Testing</h4>',
'size'=>'modal-lg',
'footer' => '<a href="#" class="btn btn-primary" data-
dismiss="modal">Close</a>',
]); ?>
<div class='well'></div>
<?php Modal::end(); ?>
Thanks all for watching my question. I solved my issue by adding modal into main.js file of the assets folder using class instead of ID.

cakephp 3: change class input error

I build my form template according the documentation. It seemed everything was fine until I get fields errors. Now I have two problems:
How can I change the class name of the forms fields when they get error?
Solution:
$this->loadHelper('Form', [
'templates' => 'your_template_file',
'errorClass' => 'your-class',
]);
How can I set escape => false in the error-message from cakephp, when the field get error? Because I have icon within that div, such as
<div class="error-message"><i class="fa fa-times"></i> My error</div>
Well, I got part of th solution. To escape HTML I could put $this->Form->error('field', null, ['escape' => false]); in all fields, but it´s a hard manually task. I´d like to keep escape with default of all fields errors. I could edit the FormHelper.php class. However, I think that is not good idea.
My form template is:
'formStart' => '<form {{attrs}} class="form-horizontal" novalidate>',
'inputContainer' => '{{content}}',
'input' => '<input type="{{type}}" name="{{name}}" {{attrs}} class="form-control"/>',
'checkbox' => '<input type="checkbox" value="{{value}}" name="{{name}}" {{attrs}}/>',
'textareaContainerError' => '{{content}}',
'textarea' => '<textarea name="{{name}}" {{attrs}} class="form-control"></textarea>',
'select' => '<select name="{{name}}" {{attrs}} class="form-control">{{content}}</select>',
'button' => '<button {{attrs}} class="btn btn-primary">{{text}}</button>',
'nestingLabel' => '{{input}}',
'formGroup' => '{{input}}',
to the second part of the question: you can extend FormHelper like in code below, so that escape will be set to false by default
// extended FormHelper, this goes in src/View/Helper
namespace App\View\Helper;
use Cake\View\Helper;
class MyFormHelper extends Helper\FormHelper
{
public function error($field, $text = null, array $options = [])
{
if (!isset($options['escape'])) {
$options['escape'] = false;
}
return parent::error($field, $text, $options);
}
}
next create alias for this helper in AppController.php
public $helpers = [
'Form' => ['className' => 'MyForm']
];
this also allows you to add more customization of your own and at any time, you can go back to default implementation of FormHelper, just remove that alias from AppController.php.
For those who wants an 'easy solution' to escape error message on some fields, you cant simply set escape options to false :
<?= $this->Form->input('email', [
"label" => "Email",
"error" => [
"escape" => false
]
]) ?>