Example #1
0
 /**
  * 添加关系
  */
 public static final function append()
 {
     $error = array();
     $online = front::online();
     $get = array('s_type' => isset($_GET['s_type']) ? $_GET['s_type'] : '', 't_type' => isset($_GET['t_type']) ? $_GET['t_type'] : '', 's_id' => isset($_GET['s_id']) ? (int) $_GET['s_id'] : '0', 't_id' => isset($_GET['t_id']) ? (int) $_GET['t_id'] : '0');
     $s_list = null;
     if ($get['s_type'] == 'channel') {
         $s_list = channel::get_channel_select(0, 0, $get['s_id'], null, null);
     } elseif ($get['s_type'] == 'address') {
         $s_lists = address::selects('address_id as id,name', null, array('user_id' => $online->user_id), array('ORDER BY address_id DESC'), array('id', 'column|table=address' => 'name'));
         if ($s_lists) {
             $s_list = make_option($s_lists, $get['s_id']);
         }
     } elseif ($get['s_type'] == 'book') {
         $s_lists = book::selects('book_id as id,concat_ws(\',\',create_date,item_txt,remark,ccy,amount,otype) as name', null, array('user_id' => $online->user_id), array('ORDER BY create_date DESC,book_id DESC'), array('id', 'column|table=book' => 'name'));
         if ($s_lists) {
             $s_list = make_option($s_lists, $get['s_id']);
         }
     } elseif ($get['s_type'] == 'diary') {
         $s_lists = diary::selects('diary_id as id,title as name', null, array('user_id' => $online->user_id), array('ORDER BY diary_id DESC'), array('id', 'column|table=diary' => 'name'));
         if ($s_lists) {
             $s_list = make_option($s_lists, $get['s_id']);
         }
     } elseif ($get['s_type'] == 'doc') {
         $s_lists = doc::selects('doc_id as id,title as name', null, array('user_id' => $online->user_id), array('ORDER BY doc_id DESC'), array('id', 'column|table=doc' => 'name'));
         if ($s_lists) {
             $s_list = make_option($s_lists, $get['s_id']);
         }
     } elseif ($get['s_type'] == 'site') {
         $s_lists = site::selects('site_id as id,title as name', null, array('user_id' => $online->user_id), array('ORDER BY site_id DESC'), array('id', 'column|table=site' => 'name'));
         if ($s_lists) {
             $s_list = make_option($s_lists, $get['s_id']);
         }
     } elseif ($get['s_type'] == 'user') {
         $s_lists = user::selects('user_id as id,username as name', null, array('user_id' => $online->user_id), array('ORDER BY user_id DESC'), array('id', 'column|table=user' => 'name'));
         if ($s_lists) {
             $s_list = make_option($s_lists, $get['s_id']);
         }
     } else {
     }
     $t_list = null;
     if ($get['t_type'] == 'channel') {
         $t_list = channel::get_channel_select(0, 0, $get['t_id'], null, null);
     } elseif ($get['t_type'] == 'address') {
         $t_lists = address::selects('address_id as id,name', null, array('user_id' => $online->user_id), array('ORDER BY address_id DESC'), array('id', 'column|table=address' => 'name'));
         if ($t_lists) {
             $t_list = make_option($t_lists, $get['t_id']);
         }
     } elseif ($get['t_type'] == 'book') {
         $t_lists = book::selects('book_id as id,concat_ws(\',\',create_date,item_txt,remark,ccy,amount,otype) as name', null, array('user_id' => $online->user_id), array('ORDER BY create_date DESC,book_id DESC'), array('id', 'column|table=book' => 'name'));
         if ($t_lists) {
             $t_list = make_option($t_lists, $get['t_id']);
         }
     } elseif ($get['t_type'] == 'diary') {
         $t_lists = diary::selects('diary_id as id,title as name', null, array('user_id' => $online->user_id), array('ORDER BY diary_id DESC'), array('id', 'column|table=diary' => 'name'));
         if ($t_lists) {
             $t_list = make_option($t_lists, $get['t_id']);
         }
     } elseif ($get['t_type'] == 'doc') {
         $t_lists = doc::selects('doc_id as id,title as name', null, array('user_id' => $online->user_id), array('ORDER BY doc_id DESC'), array('id', 'column|table=doc' => 'name'));
         if ($t_lists) {
             $t_list = make_option($t_lists, $get['t_id']);
         }
     } elseif ($get['t_type'] == 'site') {
         $t_lists = site::selects('site_id as id,title as name', null, array('user_id' => $online->user_id), array('ORDER BY site_id DESC'), array('id', 'column|table=site' => 'name'));
         if ($t_lists) {
             $t_list = make_option($t_lists, $get['t_id']);
         }
     } elseif ($get['t_type'] == 'user') {
         $t_lists = user::selects('user_id as id,username as name', null, array('user_id' => $online->user_id), array('ORDER BY user_id DESC'), array('id', 'column|table=user' => 'name'));
         if ($t_lists) {
             $t_list = make_option($t_lists, $get['t_id']);
         }
     } else {
     }
     // 表单处理
     while (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
         // 数据消毒
         $post = array('s_type' => isset($_POST['s_type']) ? $_POST['s_type'] : '', 't_type' => isset($_POST['t_type']) ? $_POST['t_type'] : '', 's_id' => isset($_POST['s_id']) ? (int) $_POST['s_id'] : '0', 't_id' => isset($_POST['t_id']) ? (int) $_POST['t_id'] : '0', 'user_id' => $online->user_id);
         if (!$post['s_type']) {
             $error['s_type'] = '请选择源类型';
         }
         if (!$post['t_type']) {
             $error['t_type'] = '请选择目标类型';
         }
         if (!$post['s_id']) {
             $error['s_id'] = '请选择源内容';
         }
         if (!$post['t_id']) {
             $error['t_id'] = '请选目标内容';
         }
         if (!$error['t_id']) {
             if ($post['s_type'] == $post['t_type'] && $post['s_id'] == $post['t_id']) {
                 $error['t_id'] = '不能和自己关联';
             }
         }
         if (!$error['t_id']) {
             $related_id = self::selects('related_id', null, array('user_id' => $online->user_id, 's_id' => $post['s_id'], 's_type' => $post['s_type'], 't_id' => $post['t_id'], 't_type' => $post['t_type']), null, array('column' => 'related_id'));
             if (!$related_id) {
                 $related_id = self::selects('related_id', null, array('user_id' => $online->user_id, 's_id' => $post['t_id'], 's_type' => $post['t_type'], 't_id' => $post['s_id'], 't_type' => $post['s_type']), null, array('column' => 'related_id'));
             }
             if ($related_id) {
                 $error['t_id'] = '目标内容已经关联,请重新选择';
             }
         }
         if (!empty($error)) {
             break;
         }
         // 数据入库
         $related = new self();
         $related->related_id = null;
         $related->struct($post);
         $related->insert();
         $error = '添加成功';
         front::view2('error.tpl', compact('error'));
         return;
     }
     $types = array('address' => '地址', 'book' => '账本', 'channel' => '分类', 'diary' => '日志', 'doc' => '文章', 'site' => '网址', 'user' => '用户');
     front::view2(__CLASS__ . '.' . 'form.tpl', compact('post', 'get', 'error', 'types', 's_list', 't_list'));
 }