How to display extra fields in article with K2 - custom-fields

currently I've got Jreviews installed and I'd like to replace it by K2 to list specialized shops with addresses, phones, maps, opening hours ...
With K2 I guess I'll need to define extra custom fields to hold those specific information. No problem.
But, how may I configure things to have those fields displayed in the detailed article/items for a specific shop ?
Many thanks,
Tibi.

// In the item template you can skip this first line...
$this->item->extra_fields = K2ModelItem::getItemExtraFields($this->item->extra_fields);
$extraFlds = array();
if ( $this->item->extra_fields ){
foreach ( $this->item->extra_fields as $key=>$extraField ){
$extraFlds[ $extraField->name ] = $extraField->value;
}
}
Then you can access your extra fields in the associate array like $extraFlds['my field']

After a lot of tries here what i used and worked for me
<?php
// if form is empty show default form
$k2obj = new K2ModelItem();
$fields = $k2obj->getItemExtraFields($this->item->extra_fields, $this->item);
//echo $this->item->extraFields->State->name;
echo $this->item->extraFields->FIELD_ALIAS->value;
?>
This is working and noted its all pegged to instantiating the class.
Note: I am using this in the k2 item i version 2.6.7 Joomla 2.5.14

if you want show custum field in k2 table list go to:
components\com_k2\templates\default\category_item.php
and edit file near line 136 like this:
<?php foreach ($this->item->extra_fields as $key=>$extraField):
**if(strpos($extraField->name,"/")){**
?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="catItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>
<span class="catItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</li>
<?php **}** endforeach; ?>
i do that in my site: www.joomir.com

The problem is that $this->item->extra_fields is actually a JSON string retrieved from the database, so you have to decode it first. It's structure is rather complicated (and unfortunately each field is labelled by it's id, it's name doesn't appear at all), you'll see it if you execute:
print_r($this->item->extra_fields);`
If you want to call field values by it's field name I'd do it like this:
if ($this->item->params->get('itemExtraFields')) {
$item_extra_fields = json_decode($this->item->extra_fields);
$put_your_extra_field1_name_here = $item_extra_fields[1]->value;
$put_your_extra_field2_name_here = $item_extra_fields[2]->value;
$put_your_extra_field3_name_here = $item_extra_fields[3]->value;
$put_your_extra_field4_name_here = $item_extra_fields[4]->value;
}
Notice that this is useful if the extra field you need is text, but it can be an array or whatever so you might have to code a little bit more. Hope this is useful!

In K2 you set the parameters for how an item displays at the category level. There is an option to display the extra fields in both Item view options in category listings as well as the Item view options.
By default, the built in K2 template will display the extra fields under a heading "Additional Information" with an unordered list of field name and values. You can override that template and make the extra fields display any way you like.

Related

How to edit an image in laravel 5 if user upload a new one ,otherwise upload with old image file

I have an edit form which has an image field where a user can upload a new image if he wants to.
But if the user does not upload a new photo I want to just use the photo that's already in the database. And not update the image field at all. But in my code whenever I am trying to without uploading new image form is not taking the old input value.
Here is my edit function:
public function expenseupdate1(){
$input = Input::only('id','Expense_date','Expense_category_id','Vendor_id','Customer_id','Amount','Tax1_id','Tax2_id','Note','Receipt');
$data=new Expense;
$id=$input['id'];
$Expense_date=$input['Expense_date'];
$Expense_category_id=$input['Expense_category_id'];
$Vendor_id=$input['Vendor_id'];
$Customer_id=$input['Customer_id'];
$Amount=$input['Amount'];
$Tax1_id=$input['Tax1_id'];
$Tax2_id=$input['Tax2_id'];
$Note=$input['Note'];
if(Input::hasFile('Receipt')) {
$file = Input::file('Receipt');
$name = time() . '-' . $file->getClientOriginalName();
$data->Receipt = $name;
$file->move(public_path() . '/images/', $name);
}
$affectedrows=Expense::where('id', '=', $id)->update(array('Expense_date' => $Expense_date,'Expense_category_id'=>$Expense_category_id,'Vendor_id'=>$Vendor_id,'Customer_id'=>$Customer_id,'Amount'=>$Amount,'Tax1_id'=>$Tax1_id,'Tax2_id'=>$Tax2_id,'Note'=>$Note,'Receipt'=>$Receipt));
return redirect('expenseinfo');
}
and here is my update form image field code:
<td> <div class="form-group"style="margin-left:-305px">
{!! Form::label('image', 'Receipt') !!}
<input Input::old('Receipt'), type="file" name="Receipt" value = '{{$data->Receipt}}'></td><td><?php echo $data->Receipt; ?></td>
</div></td>
<tr>
<td>{!! Form::submit('Update', array( 'class'=>'' )) !!}
{!! Form::close() !!}</td>
Any help would be appreciated greatly
You wouldn't set a default value for file.
The file input type creates a field
through which users can upload files
from their local computer or network.
The VALUE attribute specifies the name
of the initial file, but it is
typically ignored by browsers as a
security precaution.
So, your application is behaving correctly. Since the image is already in the database you wouldn't need to uploaded it again.
Also, just FYI but you can clean up your controller method dramatically!
/**
* Update Expense 1
*
* #param Request $request
* #return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function expenseupdate1(Request $request){
$expense = Expense::find($request->input('id'));
$expense->fill($request->only('id','Expense_date','Expense_category_id','Vendor_id','Customer_id','Amount','Tax1_id','Tax2_id','Note','Receipt'));
if($request->hasFile('Receipt')) {
$file = $request->file('Receipt');
$name = time() . '-' . $file->getClientOriginalName();
$expense->Receipt = $name;
$file->move(public_path('/images/'), $name);
}
$expense->save();
return redirect('expenseinfo');
}
The above assumes you have the necessary use statements at the top of you're controller i.e.
use Illuminate\Http\Request;
use App\Expense; //Assuming that Expense is in this namespace
If you haven't already, you should set the fillable array for you model to allow the fill() method (mass assignment) to work http://laravel.com/docs/5.1/eloquent#mass-assignment
There is even more you can do but I have already gone outside the scope of this question. I would, however, suggest looking at:
http://laravel.com/docs/5.1/routing#route-model-binding
http://laravel.com/docs/5.1/controllers#restful-resource-controllers
http://laravel.com/docs/5.1/controllers#dependency-injection-and-controllers
Hope this helps!

enter a specific object value

i'm using zendframwork 2 , i implement the album exemple in the official documentation
http://framework.zend.com/manual/2.1/en/user-guide/overview.html#the-tutorial-application (so in my model folder i have Album.php and AlbumTable.php) and all works fine , i just want to make a small modification :
i want to have acces to the third element in album . in the index.phtml view (originally i have this code )
<?php foreach ($albums as $album) : ?>
<?php echo $this->escapeHtml($album->title);?>
<?php echo $this->escapeHtml($album->artist);?>
<?php endforeach; ?>
i tried things like
<?php echo $this->escapeHtml($album->title[3]);?>
<?php echo $this->escapeHtml($album[3]->title);?>
but i always get this error
( ! ) Fatal error: Cannot use object of type Auth\Model\Album as array in C:\wamp\www\zf2-album\module\Auth\view\auth\auth\index.phtml on line 14
any help please ?
thanks every one
Do you need ONLY the third one? Then write your Query to be more efficient ;) For all other cases, since you are working with a Zend\Db\ResultSet\ResultSet, which uses Iterator you have different options.
The first one would be to still iterate through the fieldset like
while ($albums->key() != 3) {
$albums->next();
}
$album = $albums->current();
The alternative would be to simply convert the ResultSet into an array
$myAlbums = $albums->toArray();
$album = $myAlbums[3];
Depending on how big your ResultSet is and how many entries you really need, either Solution may be faster for you. Guess you have to test that one ;)

Yii: Update model values before actionCreate and actionUpdate

I have a Location model with the following attributes -
id
City
State
Country
I wan't the user to be able to select from a list of existing states / countries, and if an additional item needs to be added it may be typed into a textbox. I've modified the _form.php partial as follows -
// city
<?php echo $form->textField($model,'city',array('size'=>60,'maxlength'=>100)); ?>
// state
<?php echo $form->dropDownList($model, 'state', CHtml::listData(Location::model()->findAll(), 'state', 'state')); ?>
<?php echo CHtml::textField('state2','',array('size'=>60,'maxlength'=>100)); ?>
// country
<?php echo $form->dropDownList($model, 'country', CHtml::listData(Location::model()->findAll(), 'country', 'country')); ?>
<?php echo CHtml::textField('country2','',array('size'=>60,'maxlength'=>100)); ?>
state2 and country2 are not a part of the model attributes. Now, in the Location Controller I have the following action -
public function actionCreate()
{
$model=new Location;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Location']))
{
$model->attributes=$_POST['Location'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
I want to set the values like this before this action executes -
if(!empty($_POST['state2'])) $model->state = $_POST['state2'];
if(!empty($_POST['country2'])) $model->country = $_POST['country2'];
What I've tried so far
1. Attempt 1
I added the lines directly to both actionCreate and actionUpdate. However, I don't think this is a clean solution.
1. Attempt 2
I tried adding a filter like this -
public function filterAlternateData($filterChain)
{
if(!empty($_POST['state2'])) $_POST['Location[state]'] = $_POST['state2'];
if(!empty($_POST['country2'])) $_POST['Location[country]'] = $_POST['country2'];
$filterChain->run();
}
Then I modified the filters() function like this so that it's bound to the create and update actions -
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'alternateData + create, update',
);
}
But this did not work.
Anyone have any ideas?
I presume this is happening in a CRUD operation based upon the actionCreate() method.
Personally I would create a separate form for the create functionality with the following attributes.
Form
Cities
States
Countries
New city (not required)
New State (not required)
New Country (not required)
Then in the form validation you can check to see if the user has entered a city, state or country which already exists etc.
You can add the lines in beforeSave() method (you will have to override it) of your model.

How to change my view file to get a appropriate output?

I have a controller like this :
def mytask = {
def user = User.findByLogin(params.id)
def mywork = user.schedules.daySchedules
[ mywork : mywork ]
}
Where I'm trying to find all the tasks assigned to a particular user. I have a corresponding view file :
<g:each in="${mywork}" var="tasks">
<div id = "todayswork">
${tasks.task}
</div>
<div id ="Dates">
${tasks.startTime}-
${tasks.endTime}
</div>
<hr/>
</g:each>
Logic works fine, I'm getting the output as I wanted. For example, if I go to http://localhost:8080/scheduleTest/daySchedule/mytask/anto my browser showing all the task for the user anto. But there is a problem in rendering it.
I'm getting the output as :
But I need the output something like this one:
How change my view file to get the appropriate output.
Thanks in advance.
It's hard to tell from your examples, but my guess is you need to be looping over the tasks item, which appears to be a List in a List.
This means change this:
<g:each in="${mywork}" var="tasks">
to this
<g:each in="${mywork[0]}" var="tasks">
// or
<g:each in="${mywork.tasks}" var="tasks">
Again, I'm not exactly sure where the problem is occurring, but one of those will fix it.
The reason you are getting the output is that Groovy will automatically perform a property expansion on a list if the property is not defined on that list. Example:
use(org.codehaus.groovy.runtime.TimeCategory) {
def d1 = 5.minutes.ago
def d2 = 1.week.from.now
assert [d1, d2].time == [d1.time, d2.time]
}
It's the same thing as writing list*.property, and returns a new list containing each property on the original items.

jQuery autocomplete not displaying my encoded values

I am working from this example: http://jqueryui.com/demos/autocomplete/#remote and I am encoding the output like this:
$rows = array();
while($r = mysql_fetch_assoc($category_result))
{
$rows[] = $r;
error_log ("rows: ".$rows[0]);
}
echo json_encode($rows);
But the dropdown on the other side shows nothing. Here is my test page: http://problemio.com/test.php - if you enter "ho" it matches 2 results in the database, but they are not getting displayed for some reason. Any idea why?
Thanks!!
The properties should be named label and value. From the JQuery UI demo page you linked to:
The local data can be a simple Array of Strings, or it contains
Objects for each item in the array, with either a label or value
property or both. The label property is displayed in the suggestion
menu.
So you would need to rename category_name to label either in PHP or later on in your JavaScript source handler function. The latter would require you to replace the PHP URL with a callback function like in the remote example. That way you could get the data any way you want (e.g. by jQuery.getJSON()) and work with it before it gets handed over to the suggestion box.
Hope this helps.
Regarding your comment, this should do it:
$rows = array();
while ($r = mysql_fetch_array($category_result)) {
$rows[] = array("label" => $r["category_name"]);
}
echo json_encode($rows);

Resources