mercredi 1 avril 2015

Form to comment in DataObject, SilverStripe 3.1.12

I have this structure...:


Products>mysite>code>Products>Product.php


I have my product like DataObject and ContentController to create form and handle on backend...

class Product extends DataObject
{
//...
}

class Product_Controller extends ContentController
{
private static $allowed_actions = array(
'CommentForm'
);

public function CommentForm()
{
$form = Form::create(
this,
__FUNCTION__,
FieldList::create(
TextField::create('Name',''),
EmailField::create('Email',''),
TextareaField::create('Comment','')
),
FieldList::create(
FormAction::create('handleComment', 'Post Comment')
->setUseButtonTag(true)
->addExtraClass('btn btn-default-color btn-lg')
),
RequiredFields::create('Name','Email', 'Comment')
)->addExtraClass('form-style');

foreach($form->Fields() as $field){
$field->addExtraClass('form-control')
->setAttribute('placeholder', $field->getName(), '*');
}

$data = Session::get("FormData.{$form->getName()}.data");

return $data ? $form->loadDataFrom($data) : $form;
}

public function handleComment($data, $form){
Session::set("FormData.{$form->getName()}.data", $data);
$existing = $this->CommentForm()->Comments()->filter(array(
'Comment' => $data['Comment']
));
if($existing->exists() && strlen('Comment') > 20){
$form->sessionMessage('That comment already exists!', 'bad');

return $this->redirectBack();
}

$comment = ProductComment::create();
$comment->ProductID = $this->ID;
$form->saveInto($comment);
$comment->write();

Session::clear("FormData.{$form->fetName()}.data");
$form->sessionMessage('Thanks for your comment', 'good');

return $this->redirectBack();

}

}


Products>mysite>code>Products>ProductsPage.php


Page with many Products...

class ProductsPage extends Page
{

private static $has_many = array(
'Products' => 'Product',
'Categories' => 'Category'
);
//...
}
class ProductsPage_Controller extends Page_Controller
{
private static $allowed_actions = array (
'show'
);

public function show(SS_HTTPRequest $request)
{
$product = Product::get()->byID($request->param('ID'));

if (!$product) {
return $this->httpError(404, 'That region could not be found');
}

return array(
'Product' => $product,
'Name' => $product->Name
);
}
}


Products>mysite>code>extensions>ProductExtension>ProductComments.php


Is a DataExtension with many comments...

class ProductComments extends DataExtension{

private static $has_many = array(
'Comments' => 'ProductComment'
);
}


Products>mysite>code>extensions>ProductExtension>ProductComment.php


A simple DataObject to comment...

class ProductComment extends DataObject
{
private static $db = array(
'Name' => 'Varchar',
'Email' => 'Varchar',
'Comment' => 'HTMLText'
);

private static $summary_fields = array(
'Created' => 'Created',
'Name' => 'Name',
'Email' => 'Email',
'Comment' => 'Text'
);
private static $has_one = array(
'Product' => 'Product'
);
public function getCMSFields(){
$fields = FieldList::create(
TextField::create('Name'),
TextField::create('Email'),
HtmlEditorField::create('Comment')
);
return $fields;
}
}


My file config.yml to add extension comments...


Products>mysite>_config>config.yml



Product:
extensions:
- ProductComments


SilverStripe files (.ss)...


Products>themes>my_theme>templates>Layout>ProductsPage.ss


Little view to products...

<% loop $Products %>
<div class="item col-md-2"><!-- Set width to 4 columns for grid view mode only -->
<div class="image">
<a href="$Link">
$Main_image.CroppedImage(175,150)
</a>
</div>
<div class="item">
<h3>
<a href="$Link">$Name</a>
</h3>
$Short_description
</div>
</div>
<% end_loop %>


Products>themes>my_theme>templates>Layout>ProductsPage_show.ss


View to see info and comment product.

<% with $Product %>
<%--...--%>
<h1>Comments</h1>
<% loop $Comments %>
<div class="comment">
<h3>$Name <small>$Created.Format('j F,Y')</small></h3>
<p>$Comment</p>
</div>
<% end_loop %>

<div class="comments-form">
<h3>Leave a Reply</h3>
<p>Your email address wlill no be published. Required fields are marked*</p>

$CommentForm
</div>
<% end_with %>


I can´t extend ContentController from DataObject, it´s wrong... What would be the best way to posting a product that is DataObject? I´ve seen this post Submit Form in Dataobject - Silverstripe 3.1 I can't it working.


cant get class module to work to change colour of active field

I've created a class module which is used to change the colour of the active field in a user form. The the sub within the module is called I get the error "wrong number of arguments or invalid property assignment".


The class coding us as follows:



Option Explicit

Public Event GetFocus()
Public Event LostFocus()

Private strPreCtr As String

Public Sub CheckActiveCtrl(objForm As MSForms.UserForm)

With objForm
If TypeName(.ActiveControl) = "ComboBox" Or TypeName(.ActiveControl) = "TextBox" Then
strPreCtr = .ActiveControl.Name
MsgBox strPreCtr
On Error GoTo terminate
Do
DoEvents
If .ActiveControl.Name <> strPreCtr Then
If TypeName(.ActiveControl) = "ComboBox" Or _
TypeName(.ActiveControl) = "TextBox" Then
'can only raise events that are declared as Public Events within the Class
RaiseEvent LostFocus(strPreCtr)
strPreCtr = .ActiveControl.Name
RaiseEvent GetFocus(strPreCtr)
End If
End If
Loop
End If
End With

terminate:
Exit Sub

End Sub


I declare the cls in the form activation



Private Sub UserForm_Activate()
Set objForm = New clsCtlColor


and then call the class sub



ActiveControl.BackColor = &HC0E0FF
objForm.CheckActiveCtrl frmNewJob


this is when the error is received. ""wrong number of arguments or invalid property assignment". Any suggestions as to why this is happening would be greatly appreciated.


Liste déroulante des Pays pour Contact Form 7en Français - Plugin Worpdress

Je pense que ça pourra servir à certains ; )


Bonne journée !


Is it possible to manipulate the target window of a form using Jquery

I got this question regarding the forms target and its target window.. lets say we have this Html.BeginForm



@using (Html.BeginForm("TestHtmlRedirect", "Home", FormMethod.Post, new {@target="_blank", @id = "tagging_frm", @class = "form-horizontal row-border" }))
{
<input type="submit" value="Html PsBk Click" />
}


normally upon clicking the submit button it will trigger a new tab, my question is How, or is it Possible to append something to the newly created tab of this form?


for example i want to append a loading gif on that newly created tab by the parent tab.


JS HTML5 Validate on "display:none" required input elements

I have a form which is multi-div. I mean: i fill some fields, then I click next and current div gets display css proprierty set as "none".


All the fields in this form are required, so this is a snippet of the situation



<form action="" method="post">
<div id="div1" style="display:none">
<input name="input1" type"text" required />
<input name="input2" type"text" required />
</div>
<div id="div2" style="display:block">
<input name="input3" type"text" required />
<input name="input4" type"text" required />
<input type"submit" value="submit" />
</div>
</form>


Now the problem is that, when I submit the form, if one field of the first div is not filled up, it won't submit and Chrome gives me the following error in the Console



An invalid form control with name='input1' is not focusable.


So now I would like to know how can I solve this situation. I thought about catching the error and then showing the first div, but didn't figure out how to do this.


Thank you


wordpress can't rewrite url when using form GET method

Now I have problem related to URL rewrite when I using form on Method "Get" with wordpress and can't rewrite url from http://ift.tt/1IlA2ey to "http://ift.tt/1ahKCZM"


This is my code in Wordpress function.php file



add_action('init', 'custom_rewrite_url');
function custom_rewrite_med() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
add_rewrite_tag('%goodsType%','([^&]+)');
add_rewrite_tag('%city%','([^&]+)');
add_rewrite_tag('%prices%','([^&]+)');
$wp_rewrite->add_rule('normalurl/([^/]+)/([^/]+)/([^/]+)','index.php?goodsType=$matches[1]&city=$matches[2]&prices=$matches[3]','top');
}

add_filter('query_vars', 'my_query_vars');
function my_query_vars($vars){
$vars[] = 'goodsType';
$vars[] = 'prices';
$vars[] = 'city';
return $vars;
}


But my solution not luck, I wish all of you will help me on this problem. Thanks in advance.


How to submit a form using Data tables

I'm using data tables to show questions from a survey. Questions are shown one per page. When user click the "next" button, he sees the next question, and finally he sees submit button. But it does not work, it does not send the form. How should I use submit form in Data Tables? I want submit button to be seen finally. Here's my view:





<html>
<head>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>

<script>

$(document).ready(function() {
$('#example').dataTable( {
"pagingType": "simple",
"lengthMenu": [[1], [1]],
"bSort": false,

"language": {
"paginate": {
"previous": false
}
}


} );

} );

</script>
</head>
<body>
<br/><br/>
<?php
$survey_id = $this->uri->segment(3);
<table id="example" >
<thead>
<tr><th>Въпрос</th></tr>
</thead>
<tbody>
<?php
echo validation_errors();

foreach ($survey as $row)
{

$row->question_id=$this->uri->segment(4);
$question_id=$this->uri->segment(4);
?>
<tr>
<?php
$attributes = array('id' => 'myform');
echo form_open('index/survey_fill/' .$survey_id , $attributes); ?>
<td>
<?php echo "$row->question"; ?><br/>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>

$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '5'
);
echo "<input type='hidden' name='survey_id' value='$row->survey_id'>"; ?>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />";
echo form_radio($data);
echo "5 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => ' 4'
);
echo form_radio($data);
echo " 4";
</td></tr>
<?php
}
?>

<tr><td><input type="submit" id="button" name = "submit" value="Submit">

<?php echo form_close(); ?>

</td></tr>

</tbody>
</table>

</div>
</body>
</html>



My model is:





<?php
class User_model extends CI_Model {


public function __construct() {
parent:: __construct();
$this->load->database();
$this->load->helper('array');

}
public function survey_show() {

$this->db->select('*');
$this->db->from('survey_questions');
$this->db->where('survey_id', $this->uri->segment(3));
$result=$this->db->get();

return $result->result();
}
public function survey_fill()
{
$answers=($this->input->post('answer'));

if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");

foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'created_at'=>$date->format('Y-m-d H:i:s')
);


$this->db->insert('survey_answers', $data);

}
if($this->db->affected_rows() > 0)
{

return true;
}
return false;
}
}