开发者

Symfony 1.3: Any opinion about this code? Could be shorter or better?

I need your opinion about this code below.

I have a list of messages: each message has a link that change the state of the message (read - non read).

In the partial "_message" i have this:

<div class="switching_link" id="switching_link_<?php echo $message ?>">

         echo include_partial('link_switch_state', array('message' =>
$message))

</div>

In the partial "_link_switch_state" i have this:

if((int)$message->getState() == 1) {

            $string_state_message="non read";

} else {

            $string_state_message="read";

}

echo link_to_remote('Mark as '.$string_state_message, array(

                    'url' => 'message/switchState?id='.$message->getId(),

                     'update' => 'switching_link_'.$message,

                     "complete" => "switchClassMessage('$message');",

));

And in message/actions/actions.class.php i have this:

public function executeSwitchState(sfWebRequest $request) {

         // searching the message we want to change its state.
         $this->messages =
Doctrine::getTable('Message')->findById($request->getParameter('id'));

         // changing the state of the message.
         if($this->messages[0]->getState() == 1) {

             $this->messages[0]->setState(0);
         }
         else {

             $this->messages[0]->setState(1);
         }

         $this->messages[0]->save();

         // rendering the partial that shows the link ("Mark as read/non
read").
         return $this->renderPartial('mensaje/link_switch_state', array(
'message' => $this->messages[0开发者_JS百科]));

     }


Mmmh not sure if you can write it shorter in Symonfy, but you can get rid of the if-else statements (assuming that the state can either be 0 or 1):

In _message:

$string_state_message = ($message->getState()) ? "non read" : "read";

0 and "0" evaluate to false, any other non-empty string to true.

In message/actions/actions.class.php:

$this->messages[0]->setState(!$this->messages[0]->getState());

This is again due the fact that 0 == false and 1 == true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜