Creating Nested tables and Using colspan not working Properly (Rtfwriter2 ; itext) - itext

While creating rtf files using itext an issue came up.
The cells that are using colspan are not generated properly in the output file.
Can someone help me with this.
I am attaching sample code for reference.{Can't attach image: 10 reputation needed}
public static void main( String[] args )
{
Document document = new Document( PageSize.A4, 50, 50, 50, 50 );
try
{
RtfWriter2 writer = RtfWriter2.getInstance( document, new FileOutputStream( "/root/Desktop/TableRtf.rtf" ) );
document.open();
Table table1 = new Table( 3 );
table1.setWidth( 100 );
table1.setBorder( 0 );
table1.getDefaultCell().setHorizontalAlignment( Element.ALIGN_CENTER );
table1.getDefaultCell().setVerticalAlignment( Element.ALIGN_MIDDLE );
table1.addCell( "!" );
table1.addCell( new Phrase( new Chunk( "cell" ) ) );
table1.addCell( "!" );
table1.addCell( "2" );
table1.addCell( new Phrase( new Chunk( "cell" ) ) );
table1.addCell( "2" );
table1.addCell( "3" );
table1.addCell( new Phrase( new Chunk( "cell" ) ) );
table1.addCell( "3" );
Table table = new Table( 2 );
table.setWidth( 100 );
table.setBorder( 0 );
com.lowagie.text.Cell tablecell1 = new com.lowagie.text.Cell();
tablecell1.addElement( new Chunk( "!!!!!!!!!!" ) );
tablecell1.setColspan( 2 );
table.addCell( tablecell1 );
table.addCell( "Text Text Text Text Text Text " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Text Text " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Text Text Text " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Text \nText \nText " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Text " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Text " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Text " );
table.addCell( new Phrase( new Chunk( "cell" ) ) );
table.addCell( "Table11 " );
table.insertTable( table1 );
document.add( table );
}
catch ( Exception de )
{
de.printStackTrace();
}
document.close();
}

Related

Phalcon uniqueness on update

folks. The uniqueness validator in my form works as expected on adding new records, but on updating an exsisting record, it throws an error that the url already exists. It exists, in fact, but only in the current record.
Here is my controller:
$feed = Feeds::findFirst($id);
$feedForm = new FeedForm($feed, array('edit' => true));
if ($this->request->isPost() == true) {
$feedData = $this->request->getPost();
if ($feedForm->isValid($feedData, $feed)) {
if ($feed->save()) {
$this->flash->success("Feed successfuly updated.");
} else {
$this->flash->error("Update failed.");
}
} else {
foreach ($feedForm->getMessages() as $message) {
$this->flash->error($message);
}
}
}
And my form class:
class FeedForm extends FormBase {
public $options;
public function initialize($entity = null, $options = null) {
parent::initialize();
$this->setEntity($entity);
$this->options = $options;
$status = new Radio('status');
$status->addValidator(
new PresenceOf(
array(
'message' => 'The status is required.'
)
));
$this->add($status);
$name = new Text('name');
$name->addValidator(
new PresenceOf(
array(
'message' => 'The name is required.'
)
));
$name->addValidator(
new StringLength(
array(
'max' => 50,
'messageMaximum' => 'The name you entered is too long.'
)
));
$this->add($name);
$xml = new Text('xml');
$xml->addValidator(
new PresenceOf(
array(
'message' => 'The URL address is required.'
)
));
$xml->addValidator(
new StringLength(
array(
'max' => 2048,
'messageMaximum' => 'The URL address you entered is too long.'
)
));
$xml->addValidator(
new Url(
array(
'message' => 'The URL you entered is invalid.'
)
));
$xml->addValidator(
new Uniqueness(
array(
'model' => 'Sravnisite\Admin\Models\Feeds',
'table' => 'feeds',
'column' => 'xml',
'message' => 'The entered URL address already exists.'
)
));
$periodOptions = array();
for ($i = 4; $i <= 24; $i++) {
array_push($periodOptions, $i);
}
$this->add($xml);
$period = new Select('period', $periodOptions);
$period->addValidator(
new PresenceOf(
array(
'message' => 'The period is required.'
)
));
$this->add($period);
$shopID = new Select('shop_id', Shops::find(), array('using' => array('id', 'name')));
$shopID->addValidator(
new PresenceOf(
array(
'message' => 'The shop is required.'
)
));
$this->add($shopID);
}
}
Any ideas?
The form validation doesn't know to ignore the record you are updating - so for uniqueness it finds the record you're trying to update and gives an error. You could do some complicated find logic to keep the uniqueness validation in the form but it is better moved to the model. Your result would end up something like:
Controller
$feed = Feeds::findFirst($id);
$feedForm = new FeedForm($feed, array('edit' => true));
if ($this->request->isPost() == true) {
$feedData = $this->request->getPost();
if ($feedForm->isValid($feedData, $feed)) {
if ($feed->save()) {
$this->flash->success("Feed successfuly updated.");
} else {
$this->flash->error("Update failed.");
// Get each of the validation messages from the model
foreach ($feed->getMessages() as $message) {
$this->flash->error($message);
}
}
} else {
foreach ($feedForm->getMessages() as $message) {
$this->flash->error($message);
}
}
}
Form
// Exactly the same as you currently have but without the Uniqueness Validator
Model
class Feeds extends Phalcon\Mvc\Model
/**
* Validate that xml URLs are unique
*/
public function validation()
{
$this->validate(new Uniqueness(array(
"field" => "xml",
"message" => "The url must be unique."
)));
return $this->validationHasFailed() != true;
}

Zend Framework Result Set

Hi I'm trying to implement the method fetchAll like the Album example but It doesn't work.
When I try to print the result with a var_dump I get this
object(Zend\Db\ResultSet\ResultSet)#258 (8) {
["allowedReturnTypes":protected]=>
array(2) {
[0]=>
string(11) "arrayobject"
[1]=>
string(5) "array"
}
["arrayObjectPrototype":protected]=>
object(Application\Model\Ubigeo)#242 (5) {
["codDpto"]=>
NULL
["codProv"]=>
NULL
["codDist"]=>
NULL
["name"]=>
NULL
["idUbigeo"]=>
NULL
}
["returnType":protected]=>
string(11) "arrayobject"
["buffer":protected]=>
NULL
["count":protected]=>
int(2057)
["dataSource":protected]=>
object(Zend\Db\Adapter\Driver\Pdo\Result)#257 (8) {
["statementMode":protected]=>
string(7) "forward"
["resource":protected]=>
object(PDOStatement)#248 (1) {
["queryString"]=>
string(52) "SELECT `ubigeo`.* FROM `ubigeo` ORDER BY `name` DESC"
}
["options":protected]=>
NULL
["currentComplete":protected]=>
bool(false)
["currentData":protected]=>
NULL
["position":protected]=>
int(-1)
["generatedValue":protected]=>
string(1) "0"
["rowCount":protected]=>
int(2057)
}
["fieldCount":protected]=>
int(5)
["position":protected]=>
int(0)
}
This is my serviceConfig:
public function getServiceConfig() {
return array(
'factories' => array(
'Application\Model\UbigeoTable' => function ($sm) {
$tableGateway = $sm->get('UbigeoTableGateway');
$table = new UbigeoTable($tableGateway);
return $table;
},
'UbigeoTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Ubigeo());
return new TableGateway('ubigeo', $dbAdapter, null, $resultSetPrototype);
}
),
);
}
Any help would be appreciate
According to var_dump your table contains these rows : codDpto, codProv, codDist, name and idUbigeo.
You can access the results this way :
foreach($resultSet as $row)
{
$result = '<p>codDpto: '.$row['codDpto'];
$result .= ', codProv: '.$row['codProv'];
$result .= ', codDist: '.$row['codDist'];
$result .= ', name: '.$row['name'];
$result .= ', idUbigeo: '.$row['idUbigeo'].'</p>';
echo $result;
}

change all posted data in zend

I want to apply this function to all posted data in zend frame work to prevent XSS attacks.
static function safe_against_xss($argument) {
$HtmlEntities_Filter = new Zend_Filter_HtmlEntities ( array ('quotestyle' => NULL, 'charset' => 'UTF-8' ) );
$argument = $HtmlEntities_Filter->filter ( $argument );
return $argument;
}
I use this code in my controller
$requests = $request->getPost() ;
foreach ($requests as $key => $value)
{
$requests[$key]=Functions::safe_against_xss($value);
}
It's worked,but i want to apply this function to all posted data in all controllers. automatically.
Sincerely
I write this codes:
$this->setRequest(Functions::safe_request($this->getRequest()));
In init of controllers
Then in Functions:
static function safe_against_xss($argument) {
// $HtmlEntities_Filter = new Zend_Filter_HtmlEntities ( NULL, 'UTF-8'
// );
$HtmlEntities_Filter = new Zend_Filter_HtmlEntities ( array ('quotestyle' => NULL, 'charset' => 'UTF-8' ) );
if (is_array($argument))
{
foreach($argument as $key => $value) {
$argument[$key] = $HtmlEntities_Filter->filter ( $value );
}
}
else
{
$argument = $HtmlEntities_Filter->filter ( $argument );
}
return $argument;
}
static function safe_post_params($params)
{
$safePostParams = array();
foreach($params as $key => $value) {
$safePostParams[$key] = self::safe_against_xss($value);
}
return $safePostParams;
}
static function safe_request($params)
{
$params->setParams(Functions::safe_post_params($params->getParams()));
$params->setPost(Functions::safe_post_params($params->getPost()));
return $params;
}

Zend Framework: Update query

That is my update function:
$id = 5; $points = 100;
public function update($id, $points) {
$this->update(array('points = ?' => new Zend_DB_Expr('points + 1')), array('id = ?' => $id));
}
But, when I call this function an occurs error:
**SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
It should have been
$this->update (array (
'points' => new Zend_DB_Expr ('points + 1')
), array (
'id = ?' => $id
));

Zend DB Update not updating

I dont know why this particular bit of code is simply not updating despite using this style of coding elsewhere succesfully.No exception is being thrown.
CREATE TABLE `accounts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`accounts_username` varchar(60) DEFAULT NULL,
`accounts_fullname` varchar(600) DEFAULT NULL,
`accounts_email` varchar(600) DEFAULT NULL,
`accounts_password` varchar(600) DEFAULT NULL,
`accounts_status` varchar(600) DEFAULT NULL,
`accounts_roles` varchar(600) DEFAULT NULL,
`accounts_comments` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
In my form I have the code
public function __construct($options = null)
{
parent::__construct($options);
$optionsrole = array(
'guest' => 'guest',
'user' => 'user',
'writer' => 'writer',
'admin' => 'admin'
);
$optionsstatus = array(
'active' => 'active',
'pending' => 'pending'
);
$this->setName('account');
$id = new Zend_Form_Element_Hidden('id');
$username = new Zend_Form_Element_Text('accounts_username');
$username->setLabel('Username')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$fullname = new Zend_Form_Element_Text('accounts_fullname');
$fullname->setLabel('Fullname')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('accounts_email');
$email->setLabel('Email')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty')
->addValidator(new Zend_Validate_EmailAddress());
$password = new Zend_Form_Element_Password('accounts_password');
$password->setLabel('Password')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$status = new Zend_Form_Element_Select('accounts_status');
$status->setLabel('Status')
->setRequired(true)
//->addMultiOptions(array('Active','Pending'));
->addMultiOptions($optionsstatus);
$role = new Zend_Form_Element_Select('accounts_roles');
$role->setLabel('Role')
->setRequired(true)
//->addMultiOptions(array('guest','user','writer','admin'));
->addMultiOptions($optionsrole);
$comments = new Zend_Form_Element_Textarea('accounts_comments');
$comments->setLabel('Comments')
//->setAttrib('size',200)
->setAttrib('rows',20)
->setAttrib('cols',50);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $username, $fullname,$email,
$password,$status,$role,$comments,$submit));
}
In my accounts controller
public function editAction()
{
$this->view->title = "Edit User Account";
$this->view->headTitle($this->view->title, 'PREPEND');
$form = new Form_Account();
$form->submit->setLabel('Save');
$accounts = new Model_DbTable_Account();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
/*
if($accounts->checkUnique($formData['accounts_username'])){
$this->view->errorMessage = "Username already taken. Please choose another one.";
return;
}
*/
$id = (int)$form->getValue('id');
$username = $form->getValue('accounts_username');
$fullname = $form->getValue('accounts_fullname');
$email = $form->getValue('accounts_email');
$password = $form->getValue('accounts_password');
$status = $form->getValue('accounts_status');
$roles = $form->getValue('accounts_roles');
$comments = $form->getValue('accounts_comments');
//$accounts = new Model_DbTable_Account();
$accounts->updateaccount($username, $fullname,$email,
$password,$status,$roles,$comments);
$this->_redirect('/account');
} else {
$form->populate($formData);
}
} else {
$id = $this->_getParam('id', 0);
if ($id > 0) {
$account = new Model_DbTable_Account();
$form->populate($account->getaccount($id));
}
}
}
In my model
public function updateaccount($id,$username, $fullname, $email,
$password,$status,$roles,$comments)
{
$data = array(
'accounts_username' => $username,
'accounts_fullname' => $fullname,
'accounts_email' => $email,
'accounts_password' => md5($password),
'accounts_status' => $status,
'accounts_roles' => $roles,
'accounts_comments' => $comments,
);
$this->update($data, 'id = '. (int)$id);
}
Am i missing something?
I did find the problem.I was not passing the $id creteria and therefore no record was been updated.
$accounts->updateaccount($username, $fullname,$email,
$password,$status,$roles,$comments);
It should have instead read
$accounts->updateaccount($id,$username, $fullname,$email,
$password,$status,$roles,$comments);