Autor Zpráva
segaman
Profil
Zdravim, chcel by som odoslat text spravy enterom ktory sa pise v textarea moja stranka je objektovo naprogramovana ci nejak tak sa to vola cize kod nizsie je zo sablony html kde je textarea a button na odoslanie. Potreboval by som jednoduchy script ktory vlozim medzi <head> a </head> a potom odkaz na tento script ktory by som vlozil do nizsie uvedeneho kodu sablony.

Malo by to fungovat tak ze enterom sa odosle nova sprava a shift+enter bude novy riadok.


<form action="<!--URL_SEND_MESSAGE-->" method="post">

<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="2">
<textarea name="text" id="text"></textarea>
</td>
<td valign="bottom">
<input type="hidden" name="id_user_to" value="<!--ID_USER_TO-->" />
<input type="submit" class="button" id="<!--SUBMIT_SEND_MESSAGE-->" name="<!--SUBMIT_SEND_MESSAGE-->" value="<!--TEXT_SUBMIT-->"/>
</td>
</tr>
</table>

</form>
Keeehi
Profil
segaman:
Našel jsem třeba toto jsfiddle.net/jishnuap/zYEMv
jQuery se dá vynechat
segaman
Profil
vyskusal som to a po stlaceni enter mi aj reloadne stranku aj zmizne napisany text ale sprava sa neulozi do databazi cize ako keby som nic nenapisal cize mi to nefunguje :(
martin1312
Profil
segaman:
Samozrejme predpokladom riešenia [#2] Keeehi je, že odoslanie formularu a jeho uloženie je už naprogramované. Kód rieši čisto iba odoslanie po stlačení enteru.
Keeehi
Profil
segaman:
Lepší popis problému by nebyl na škodu. Ale jsem si celkem jistý tím, že vím, kde je chyba. Při tomto způsobu odeslání se neodesílá hodnota toho odesílacího tlačítka, protože na něj nebylo kliknuto. Tudíž není možné kontrolovat
if(isset($_POST["SUBMIT_SEND_MESSAGE"]))
segaman
Profil
no keeehi vobec netusim ze o com je rec lebo php neovladam :) cize to znamena ze to tym scriptom neodoslem? tu je kusok toto php scriptu na spravy ak to pomoze:

  private function render_messages($id_user_from)
  {        
    $html = $this->render_message_items($id_user_from);
    
    $send_message_form = Array();
    $send_message_form['URL_SEND_MESSAGE'] = $this->get_messages_link($id_user_from);
    $send_message_form['TEXT_SEND_MESSAGE'] = ($html == '') ? $this->get_static_text(119) : $this->get_static_text(120);
    $send_message_form['ID_USER_TO'] = $id_user_from;
    $send_message_form['SUBMIT_SEND_MESSAGE'] = $this->postname_send_message;
Keeehi
Profil
segaman:
Potřeboval bych spíš vidět tu část, která se stará o zpracování po odeslání. Tam je na 99% ten problém.
martin1312
Profil
Malo by stačiť pridať do formulára:
<input type="hidden" name="<!--SUBMIT_SEND_MESSAGE-->" />
Ale na presnejšie určenie, ako píše Keeehi, trebalo by viac kódu.
segaman
Profil
nasiel som toto staci?

    private function save_message($msg)
    {
    if (!isset($msg['id_user_from']) or !isset($msg['id_user_to']) or !isset($msg['text']))
      return false;
      
    $msg['text'] = str_replace("\n", "<br />", strip_tags($msg['text']));
    $msg['date'] = date('Y-m-d H:i:s');
    $msg['unread'] = '1';  
    $msg['status'] = '1';
 
    return ($this->db->insert('messages', $msg) != false ? true : false);
martin1312
Profil
id_user_from v tvojom formulári nie je, pričom pri overovaní je potrebné.
segaman
Profil
a da sa to nejak opravit? dopisat do toho kodu co treba?
martin1312
Profil
pravdepodobne
<input type="hidden" name="id_user_from" value="<!--ID_USER_FROM-->" /> 
Šablonovací systém zrejme dopĺňa za <!--ID_USER_TO--> konkrétne ID užívateľa, tak predpokladám že pre user_from bude použité <!--ID_USER_FROM-->
Keeehi
Profil
segaman:
Když před ten if vložíš var_dump($msg); tak zjistíš, co v něm vlastně je.

Možná, že se ale tato metoda vůbec třeba ani nevolá. Zkus najít místo, z kterého je save_message volaná. Nemůže být zádrhel někde tam?

martin1312:
id_user_from v tvojom formulári nie je, pričom pri overovaní je potrebné.
V proměnné $msg nemusí být přímo $_POST pole, ale třeba jiné pole nějak předpřipravené, takže id_user_from může klidně existovat.
martin1312
Profil
Keeehi:
Áno, ale aspoň nech vyskúša...
Z tých útržkov kódu sa v podstate nedá navrhnúť žiadne riešenie...
segaman
Profil
ano je to id_user_from.......tak som to skusil doplnit ten input a nejde to aj tak


Při zpracování BB značek došlo k samovolnému sežrání celého příspěvku. Pardon.
tak vam posielam cely php kod mozno pomoze

[pre]
<?
class messages extends module
{
function messages($template_prefix, $var, &$owner)
{
parent::module($template_prefix, $var, $owner);
}

function render()
{
// ak nie sme v sekcii spravy alebo pouzivatel nie je lognuty alebo aktivny
if ($this->owner->get[$this->owner->modules['SECTIONS']->getname] != $this->get_option(21) or
!$this->owner->modules['LOGIN']->is_logged() or !$this->owner->modules['LOGIN']->is_active())
return '';

// ak prisiel parameter na zmazanie sprav od vybraneho pouzivatela nastavime im status na 0
if (isset($this->owner->get[$this->getname_delete]))
{
$where = "(`id_user_from`='".$this->owner->get[$this->getname_delete]."' AND `id_user_to`='".$this->owner->modules['LOGIN']->get_id()."') OR (`id_user_from`='".$this->owner->modules['LOGIN']->get_id()."' AND `id_user_to`='".$this->owner->get[$this->getname_delete]."')";
$messages = $this->db->get_table('messages', 'id_message', $where, -1, '`date` DESC');

foreach ($messages as $key => $value)
{
if (empty($value['dont_show_to']))
$value['dont_show_to'] .= '|';

if (strpos($value['dont_show_to'], '|'.$this->owner->modules['LOGIN']->get_id().'|') === false)
$value['dont_show_to'] .= $this->owner->modules['LOGIN']->get_id().'|';

$this->db->update('messages', array('dont_show_to' => $value['dont_show_to'], 'unread' => 0), "`id_message`='".$value['id_message']."'");
}
}

if (isset($this->owner->post[$this->postname_send_message]) and isset($this->owner->post['id_user_to']) and isset($this->owner->post['text']))
{
$msg['id_user_from'] = $this->owner->modules['LOGIN']->get_id();
$msg['id_user_to'] = $this->owner->post['id_user_to'];
$msg['text'] = $this->owner->post['text'];
$this->save_message($msg);

// ak prijemca nie je online posleme mu mail
if (!$this->owner->modules['LOGIN']->is_online($msg['id_user_to']))
{
$user_from = $this->owner->modules['LOGIN']->get_user_info();
$user_to = $this->db->get_row('users','id_user',"`id_user`='".$msg['id_user_to']."'",-1);

if (isset($user_to['alert_new_message']) and $user_to['alert_new_message'] and !$this->owner->modules['IGNORE_LIST']->is_ignored($user_from['id_user'], $user_to['id_user']))
{
require_once "web/lib/mail.php";
$email_text = str_replace( Array('%%USER%%', '%%DATE%%'), Array($user_from['name'], date('d.n.Y H:i:s')), $this->get_email_text(2));
$mail = new mail($this->get_option(30), $user_to['email'], $this->get_email_subject(2), $email_text);
$mail->send();
}
}
}

$html = '';

if (isset($this->owner->get[$this->getname]))
$html .= $this->render_messages($this->owner->get[$this->getname]);
else
$html .= $this->render_list();

return $this->parse('wrapper', Array('MESSAGES_CONTENT' => $html));
}

// ulozi spravu do db, musi obsahova odosielatela 'id_user_from', prijemcu 'id_user_to'
// a text spravy
// vratcia true ak sa sprava ulozila, false ak nie
private function save_message($msg)
{
if (!isset($msg['id_user_from']) or !isset($msg['id_user_to']) or !isset($msg['text']))
return false;

$msg['text'] = str_replace("\n", "<br />", strip_tags($msg['text']));
$msg['date'] = date('Y-m-d H:i:s');
$msg['unread'] = '1';
$msg['status'] = '1';

return ($this->db->insert('messages', $msg) != false ? true : false);
}

// oznaci spravu id_message ako precitanu
public function update_message_was_read($id_message)
{
if ($this->db->get_one('messages', 'id_message', 'id_user_to', "`id_message`='".$id_message."'", -1) == $this->owner->modules['LOGIN']->get_id())
$this->da->update('messages', Array('undread' => '0'), "`id_message`='".$id_message."'");
}

// vyrenderuje jednotlive polozky konverzacie, pouziva aj ajax pri refreshi konverzacie
// pri stlaceni tlacidla 'Zobrazit starsie' sa posle aj $pager_value, ktore obsahuje stranu
// pokial budeme renderovat spravy
public function render_message_items($id_user_from, $ajax_pager_value = 0)
{
$html = '';

$pager_value = (isset($this->owner->post[$this->postname_load_older]) and isset($this->owner->post['msg_pager_value'])) ? intval($this->owner->post['msg_pager_value']) : 1;

// ak sme dostali pager_value z ajaxu
if (intval($ajax_pager_value) > 0)
{
$pager_value = intval($ajax_pager_value);
$from = 0;
}
else
{
$from = 0;
}

$where = "(`id_user_from`='".$id_user_from."' AND `id_user_to`='".$this->owner->modules['LOGIN']->get_id()."') OR (`id_user_from`='".$this->owner->modules['LOGIN']->get_id()."' AND `id_user_to`='".$id_user_from."')";
$limit = $from . ', ' . ($pager_value * intval($this->get_option(29)));
$messages = $this->db->get_table('messages', 'id_message', $where, -1, '`date` DESC', $limit);

$limit = $from . ', ' . (($pager_value+1) * intval($this->get_option(29)));
$next_page_messages = $this->db->get_table('messages', 'id_message', $where, -1, '`date` DESC', $limit);

$i = 0;

if (!empty($messages))
foreach ($messages as $key => $value)
{
if (strpos($value['dont_show_to'], '|'.$this->owner->modules['LOGIN']->get_id().'|') !== false)
continue;

$i++;

$output = Array();
$output['DATE'] = date('j.n.Y, H:i', strtotime($value['date']));
$output['FIRST'] = ($i == 1) ? 'first' : '';
$output['URL_USER'] = $this->owner->modules['PROFILE']->get_profile_link($id_user_from);
$output['USER_NAME'] = $this->get_user_name($value['id_user_from']);
$output['TEXT'] = $value['text'];

$userfiles_dir = $this->get_option(13);
$output['SRC'] = $userfiles_dir.$this->owner->modules['GALLERIES']->get_resized_pic($this->db->get_one('users','id_user','photo',"`id_user`='".$value['id_user_from']."'", -1), 42, 42, 'userthumbmes_');

// ak sme prave vyrenderovali neprecitanu spravu adresovanu mne tak ju oznacime v db ako precitanu
if ($value['unread'] and $value['id_user_from'] != $this->owner->modules['LOGIN']->get_id())
{
$this->db->update('messages', Array('unread' => '0'), "`id_message`='".$value['id_message']."'");
$output['UNREAD'] = 'unread';
}
else
{
$output['UNREAD'] = '';
}

$html .= $this->parse('conversation_item', $output);
}

$load_older = Array();
$load_older['PAGER_VALUE'] = $pager_value + 1;
$load_older['URL_LOAD_OLDER'] = $this->get_messages_link($id_user_from);
$load_older['SUBMIT_LOAD_OLDER'] = $this->postname_load_older;
$load_older['TEXT_SUBMIT_LOAD_OLDER'] = $this->get_static_text(172);

// nakoniec este pripojime load older button ak ma user extra ucet, pripadne ak nemame nacitane vsetky spravy
if ($this->owner->modules['LOGIN']->is_active() and (count($messages) != count($next_page_messages)))
$html .= $this->parse('load_older', $load_older);
else
{
$load_older['EXTRA_INACTIVE'] = (!$this->owner->modules['LOGIN']->is_active()) ? $this->get_static_text(185) : '';
$html .= $this->parse('load_older_last', $load_older);
}

return $html;
}

// vyrenderuje spravy od pouzivatela id_user adresovane prihlasenemu pouzivatelovi
private function render_messages($id_user_from)
{
$html = $this->render_message_items($id_user_from);

$send_message_form = Array();
$send_message_form['URL_SEND_MESSAGE'] = $this->get_messages_link($id_user_from);
$send_message_form['TEXT_SEND_MESSAGE'] = ($html == '') ? $this->get_static_text(119) : $this->get_static_text(120);
$send_message_form['ID_USER_TO'] = $id_user_from;
$send_message_form['SUBMIT_SEND_MESSAGE'] = $this->postname_send_message;
$send_message_form['TEXT_SUBMIT'] = $this->get_static_text(118);
$send_message_form['IGNORE'] = $this->owner->modules['IGNORE_LIST']->render_add_ignore_link($id_user_from);

$conversation = Array();
$conversation['TITLE'] = $this->get_static_text(154) . ' ' . $this->get_user_name($id_user_from);
$conversation['TEXT_BACK'] = $this->get_static_text(117);
$conversation['URL_BACK'] = $this->make_query_string(Array($this->owner->modules['SECTIONS']->getname => $this->get_option(21)), Array(), true);
$conversation['FORM'] = $this->parse('form', $send_message_form);
$conversation['ID_USER_FROM'] = $id_user_from;
$conversation['ITEMS'] = $html;

return $this->parse('conversation', $conversation);
}

private function render_list()
{
$html;

// vytiahneme userov od ktorych mame prijate nejaky spravy, okrem ignorovanych
$conversations = $this->db->get_table('messages','id_user_from',"`id_user_to`='".$this->owner->modules['LOGIN']->get_id()."' AND `id_user_from` NOT IN (".$this->owner->modules['IGNORE_LIST']->get_ignore_list().")", -1, '`date` DESC');

foreach ($conversations as $key => $value)
{
// kontrola ci nie su spravy v tejto konverzacii "vymazane"
$where = "(`id_user_from`='".$value['id_user_from']."' AND `id_user_to`='".$this->owner->modules['LOGIN']->get_id()."') OR (`id_user_from`='".$this->owner->modules['LOGIN']->get_id()."' AND `id_user_to`='".$value['id_user_from']."')";
$messages = $this->db->get_table('messages', 'id_message', $where, -1);
$active_messages = 0;
foreach ($messages as $mkey => $mvalue)
{
if (strpos($mvalue['dont_show_to'], '|'.$this->owner->modules['LOGIN']->get_id().'|') === false)
$active_messages++;
}
if


pokracovanie kodu

        if (strpos($mvalue['dont_show_to'], '|'.$this->owner->modules['LOGIN']->get_id().'|') === false)
          $active_messages++;
      }
      if ($active_messages == 0)
        continue;      
       
      $output = Array();
      
      $userfiles_dir = $this->get_option(13);
      $output['SRC'] = $userfiles_dir.$this->owner->modules['GALLERIES']->get_resized_pic($this->db->get_one('users','id_user','photo',"`id_user`='".$value['id_user_from']."'", -1), 42, 42, 'userthumbmes_');

      $output['TEXT_READ'] = $this->get_static_text(115);
      $output['URL_READ'] = $this->get_messages_link($value['id_user_from']);
      $output['USER_FROM'] = $this->get_user_name($value['id_user_from']);
      
      $output['TEXT_DELETE'] = $this->get_static_text(198);
      $output['URL_DELETE'] = $this->owner->make_query_string(Array($this->owner->modules['SECTIONS']->getname => $this->get_option(21), $this->getname_delete => $value['id_user_from']), Array(), true);
      
      $unread_count = $this->count_unread_messages($value['id_user_from']);
      $output['UNREAD_COUNT'] = ($unread_count > 0) ? ' <span id="new_msgs_count2">'.$this->get_static_text(121) . '' . $unread_count.'</span>' : '';
      
      $where = "(`id_user_from`='".$value['id_user_from']."' AND `id_user_to`='".$this->owner->modules['LOGIN']->get_id()."') OR (`id_user_from`='".$this->owner->modules['LOGIN']->get_id()."' AND `id_user_to`='".$value['id_user_from']."')";
      $output['LAST_SENT'] = date('j.n.Y, H:i', strtotime($this->db->get_one('messages','id_message','date', $where, -1, '`date` DESC')));

      $output['LAST_MESSAGE_TEXT'] = substr($this->db->get_one('messages','id_message','text', $where, -1, '`date` DESC'), 0, 60) . ' ...';
                                                                                                                       
      $html .= $this->parse('list_item', $output);
    }
    
    $wrapper = Array();
    $wrapper['TITLE'] = $this->db->get_one('sections', 'id_section', 'name', "`id_section`='".$this->get_option(21)."'");

    $wrapper['LIST'] = $html;

    return $this->parse('list', $wrapper);
  }
  
  // vrati meno pouzivatela vo forme: Meno, vek
  private function get_user_name($id_user)
  {
    $user_info = $this->db->get_row('users', 'id_user', "`id_user`='".$id_user."'", -1);
    
    return $user_info['name'];    
  }
  
  // vrati pocet neprecitanych sprav v databaze od pouzivatela id_user_from, kde datum je vacsi ako $date
  // ak id_user_from == 0 spocitame vsetky neprecitane spravy od vsetkych userov
  // do suctu nezapocitame spravy od userov, ktori su v ignore liste
  public function count_unread_messages($id_user_from = '0', $date = '0000-00-00')
  {
    $where = "`id_user_to`='".$this->owner->modules['LOGIN']->get_id()."' AND `unread`='1'";
    $where .= ($id_user_from != '0') ? " AND `id_user_from`='".$id_user_from."' " : " AND `id_user_from` NOT IN (".$this->owner->modules['IGNORE_LIST']->get_ignore_list().")";
    $where .= ($date != '0000-00-00') ? " AND `date` > '".$date."'" : ''; 

    $messages = $this->db->get_table('messages', 'id_message', $where, -1);
    return count($messages);
  }
  
  // vygeneruje odkaz na spravy s pouzivatelom id_user
  // ak chceme zobrazit user info treba setnut id_user do getu ako zobrazovany profil 
  // pri zadavani nastavime $set_profile_id_to_get na true 
  public function get_messages_link($id_user, $set_profile_id_to_get = false)
  {
    if ($set_profile_id_to_get)
      return $this->make_query_string(Array($this->owner->modules['SECTIONS']->getname => $this->get_option(21), $this->getname => $id_user, $this->owner->modules['PROFILE']->getname => $id_user), Array(), true);
    else
      return $this->make_query_string(Array($this->owner->modules['SECTIONS']->getname => $this->get_option(21), $this->getname => $id_user), Array(), true);
  }            
    
}
?>
martin1312
Profil
segaman:
kód sa mi ešte nechcel pozerať, ale mám otázku. Odosielanie správy funguje ak sa odošle normálne kliknutím na submit tlačidlo? Nefunguje iba vtedy, keď odoslanie správy vyvolá javascript?
Keeehi
Profil
Jak jsem si myslel. Na řádku přibližně 33 je to:
if (isset($this->owner->post[$this->postname_send_message]) and isset($this->owner->post['id_user_to']) and isset($this->owner->post['text'])) 
{ 
$msg['id_user_from'] = $this->owner->modules['LOGIN']->get_id(); 
$msg['id_user_to'] = $this->owner->post['id_user_to']; 
$msg['text'] = $this->owner->post['text']; 
$this->save_message($msg); 

Ta označená část kontroluje přítomnost toho odesílacího buttonu. Stačí to smazat a už to bude fungovat.

if (isset($this->owner->post['id_user_to']) and isset($this->owner->post['text'])) 

martin1312:
Když klikne na tlačítko odeslat, tak mu to funguje. Když to odešle javascriptem, tak mu to nefunguje. Tudíž rozdíl je v odeslání postname_send_message. Bylo tedy nanejvýš pravděpodobné, že problém bude dělat právě to. A jak se i ukázalo, bylo to tak.
segaman
Profil
hej klasicky po stlaceni submit tlacidla sa odosle sprava ale nefunguje ten javascript
martin1312
Profil
Keeehi:
áno máš pravdu. Len nerozumiem, prečo potom nepomohlo pridanie
<input type="hidden" name="<!--SUBMIT_SEND_MESSAGE-->" />
segaman
Profil
tak som ten kod vymazal ale aj tak mi to neodosle....nemal by som este nieco zmenit v tej sablone?
martin1312
Profil
segaman:
vymazal si aj to "and"?
if (isset($this->owner->post[$this->postname_send_message]) and isset($this->owner->post['id_user_to']) and isset($this->owner->post['text'])) 
{ 
segaman
Profil
hej mi ta stranka najprv nesla tak som si pozrel tu vetu nizsie ako to ma vyzerat

if (isset($this->owner->post['id_user_to']) and isset($this->owner->post['text']))

tak to mam aj tam a nejde to

Vaše odpověď


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0