Changeset 20373 for events

Show
Ignore:
Timestamp:
10/01/08 17:35:19 (2 months ago)
Author:
kenji
Message:

Refactoring Follow/Remove

Location:
events/phpframework/codeigniter/trunk/system/application
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • events/phpframework/codeigniter/trunk/system/application/controllers/friendships.php

    r20235 r20373  
    1414        } 
    1515 
     16        // Ajax「フォローする」の受信 
    1617        function create() 
    1718        { 
     19                $this->session->keep_flashdata('ticket'); 
     20                header('Content-Type: application/json; charset=UTF-8'); 
     21 
    1822                // CSRF チェック 
    19                 $ticket = $this->session->userdata('ticket'); 
     23                $ticket = $this->session->flashdata('ticket'); 
    2024                if ( ! $this->input->post('ticket')  
    2125                        || $this->input->post('ticket') !== $ticket) 
    2226                { 
    2327                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but Wrong Ticket'); 
     28                        echo json_encode(array('status' => 'ng', 'html' => 'チケットが異なります')); 
    2429                        exit; 
    2530                } 
     
    2934                { 
    3035                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but Not Logged in'); 
     36                        echo json_encode(array('status' => 'ng', 'html' => 'ログインしていません')); 
    3137                        exit; 
    3238                } 
     
    3945                { 
    4046                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but follow_id = 0'); 
     47                        echo json_encode(array('status' => 'ng', 'html' => 'ユーザIDが不正です')); 
    4148                        exit; 
    4249                } 
     
    4451                { 
    4552                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but follow_id = user_id'); 
     53                        echo json_encode(array('status' => 'ng', 'html' => 'ユーザIDが不正です')); 
    4654                        exit; 
    4755                } 
     
    5159                $this->Follower_model->create($user_id, $follow_id); 
    5260 
    53                 header('Content-Type: text/html; charset=UTF-8'); 
    54                 echo '<h1 id="msg">あなたはフォローを開始しました。</h1>'; 
     61                $html = '<h1 id="msg">あなたはフォローを開始しました。</h1>'; 
     62                echo json_encode(array('status' => 'ok', 'html' => $html)); 
    5563        } 
    5664 
     65        // Ajax「フォロー解除する」の受信 
    5766        function destroy() 
    5867        { 
     68                $this->session->keep_flashdata('ticket'); 
     69                header('Content-Type: application/json; charset=UTF-8'); 
     70 
    5971                // CSRF チェック 
    60                 $ticket = $this->session->userdata('ticket'); 
     72                $ticket = $this->session->flashdata('ticket'); 
    6173                if ( ! $this->input->post('ticket')  
    6274                        || $this->input->post('ticket') !== $ticket) 
    6375                { 
    6476                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but Wrong Ticket'); 
     77                        echo json_encode(array('status' => 'ng', 'html' => 'チケットが異なります')); 
    6578                        exit; 
    6679                } 
     
    7083                { 
    7184                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but Not Logged in'); 
     85                        echo json_encode(array('status' => 'ng', 'html' => 'ログインしていません')); 
    7286                        exit; 
    7387                } 
     
    8094                { 
    8195                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but follow_id = 0'); 
     96                        echo json_encode(array('status' => 'ng', 'html' => 'ユーザIDが不正です')); 
    8297                        exit; 
    8398                } 
     
    85100                { 
    86101                        log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but follow_id = user_id'); 
     102                        echo json_encode(array('status' => 'ng', 'html' => 'ユーザIDが不正です')); 
    87103                        exit; 
    88104                } 
     
    92108                $this->Follower_model->destroy($user_id, $follow_id); 
    93109 
    94                 header('Content-Type: text/html; charset=UTF-8'); 
    95                 echo '<h1 id="msg">あなたはフォローを解除しました。</h1>'; 
    96         } 
    97  
    98         function destroy2() 
    99         { 
    100                 // CSRF チェック 
    101                 $ticket = $this->session->userdata('ticket'); 
    102                 if ( ! $this->input->post('ticket')  
    103                         || $this->input->post('ticket') !== $ticket) 
    104                 { 
    105                         log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but Wrong Ticket'); 
    106                         exit; 
    107                 } 
    108  
    109                 // ログインチェック 
    110                 if ( ! $this->redux_auth->logged_in()) 
    111                 { 
    112                         log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but Not Logged in'); 
    113                         exit; 
    114                 } 
    115  
    116                 $user_id   = intval($this->session->userdata('id')); 
    117                 $follow_id = intval($this->input->post('id')); 
    118  
    119                 // バリデーション 
    120                 if ($follow_id == 0) 
    121                 { 
    122                         log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but follow_id = 0'); 
    123                         exit; 
    124                 } 
    125                 if ($user_id == $follow_id) 
    126                 { 
    127                         log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted but follow_id = user_id'); 
    128                         exit; 
    129                 } 
    130  
    131                 $this->load->model('Follower_model', '', TRUE); 
    132                 log_message('info', '[class]' . __CLASS__ . '/' . __FUNCTION__ . '(): Posted Follow: ' . $user_id . ' > ' . $follow_id); 
    133                 $this->Follower_model->destroy($user_id, $follow_id); 
    134  
    135                 header('Content-Type: text/html; charset=UTF-8'); 
    136                 echo '#id' . $follow_id; 
     110                $html = '<h1 id="msg">あなたはフォローを解除しました。</h1>'; 
     111                echo json_encode(array('status' => 'ok', 'html' => $html, 'id' => '#id' . $follow_id)); 
    137112        } 
    138113         
  • events/phpframework/codeigniter/trunk/system/application/controllers/status.php

    r20315 r20373  
    1414        } 
    1515 
     16        // Ajax つぶやき投稿の受信 
    1617        function update() 
    1718        { 
     
    2122                // CSRF チェック 
    2223                $ticket = $this->session->flashdata('ticket'); 
    23  
    2424                if ( ! $this->input->post('ticket')  
    2525                        || $this->input->post('ticket') !== $ticket) 
  • events/phpframework/codeigniter/trunk/system/application/controllers/user.php

    r20318 r20373  
    5656                        // CSRF 対策のワンタイムチケット 
    5757                        $data->ticket = md5(uniqid(mt_rand(), TRUE)); 
    58                         $this->session->set_userdata('ticket', $data->ticket); 
     58                        $this->session->set_flashdata('ticket', $data->ticket); 
    5959 
    6060                        $this->load->model('User_model'); 
     
    137137                                // CSRF 対策のワンタイムチケット 
    138138                                $data->ticket = md5(uniqid(mt_rand(), TRUE)); 
    139                                 $this->session->set_userdata('ticket', $data->ticket); 
     139                                $this->session->set_flashdata('ticket', $data->ticket); 
    140140         
    141141                                $this->load->model('Status_model'); 
  • events/phpframework/codeigniter/trunk/system/application/views/user/main.php

    r20311 r20373  
    33        $(function(){ 
    44                $('#follow_button').click(function(){ 
    5                         $.post('<?=site_url('friendships/create')?>', {id: $('#follow_id').val(), ticket: $('#ticket').val()}, followMsg, 'html'); 
     5                        $.post('<?=site_url('friendships/create')?>', {id: $('#follow_id').val(), ticket: $('#ticket').val()}, followMsg, 'json'); 
    66                        return false; 
    77                }); 
    88        }); 
    99    function followMsg(data){ 
    10                         $('div[class="main"]').prepend(data); 
     10                if (data.status == 'ok') { 
     11                        $('div[class="main"]').prepend(data.html); 
    1112                        $('#follow_button').hide(); 
    1213                        $('#remove_button').show(); 
    1314                        $('#msg').fadeOut(3000); 
     15                        count = parseInt($('#side_count_follow').text()) + 1; 
     16                        $('#side_count_follow').text(count); 
     17                } 
     18                else { 
     19                        alert(data.html); 
     20                } 
    1421    } 
    1522 
     
    1724        $(function(){ 
    1825                $('#remove_button').click(function(){ 
    19                         $.post('<?=site_url('friendships/destroy')?>', {id: $('#follow_id').val(), ticket: $('#ticket').val()}, removeMsg, 'html'); 
     26                        $.post('<?=site_url('friendships/destroy')?>', {id: $('#follow_id').val(), ticket: $('#ticket').val()}, removeMsg, 'json'); 
    2027                        return false; 
    2128                }); 
    2229        }); 
    2330    function removeMsg(data){ 
    24                         $('div[class="main"]').prepend(data); 
     31        if (data.status == 'ok') { 
     32                        $('div[class="main"]').prepend(data.html); 
    2533                        $('#follow_button').show(); 
    2634                        $('#remove_button').hide(); 
    2735                        $('#msg').fadeOut(3000); 
     36                        count = parseInt($('#side_count_follow').text()) - 1; 
     37                        $('#side_count_follow').text(count); 
     38                } 
     39                else { 
     40                        alert(data.html); 
     41                } 
    2842    } 
    2943</script> 
  • events/phpframework/codeigniter/trunk/system/application/views/user/main_friends.php

    r20237 r20373  
    1 <script> 
     1<script type="text/javascript"> 
    22        /* 削除 */ 
    33        function removeFollow(id){ 
    44                if (confirm('削除してもよいですか?取り消しできません!')){ 
    5                         $.post('<?=site_url('friendships/destroy2')?>', {id: id, ticket: $('#ticket').val()}, removeLine, 'html');               
     5                        $.post('<?=site_url('friendships/destroy')?>', {id: id, ticket: $('#ticket').val()}, removeLine, 'json');                
    66                }; 
    77        } 
    8         function removeLine(id){ 
    9                 $('tr').remove(id); 
    10                 count = $('span#count').text() - 1; 
    11                 $('#count').text(count); 
    12                 $('#side_count_friend').text(count); 
     8        function removeLine(data){ 
     9                if (data.status == 'ok') { 
     10                        $('tr').remove(data.id); 
     11                        count = $('span#count').text() - 1; 
     12                        $('#count').text(count); 
     13                        $('#side_count_friend').text(count); 
     14                } 
     15                else { 
     16                        alert(data.html); 
     17                } 
    1318        } 
    1419</script>