/** * Get abbreviated link to file connected to multimedia message * * @param Message $item * @return string */ public static function getHumanUrl(Message $item) { $data = $item->getData(); $prefix = $item->getId() % 100 . '/' . $item->getId() . '.'; if (substr($data, 0, strlen($prefix)) == $prefix) { $data = substr($data, strlen($prefix)); } return $data; }
/** * Saves message to DB * * @param Message $item * @throws \Exception * @return Message */ public function save(Message $item) { if (empty($item)) { throw new \Exception('Empty item'); } $groupDao = new GroupDAO(); $item->setTarget(implode(',', array_keys($groupDao->getNumbers($item->getGroupId())))); // if (!$item->getTarget()) $item->setTarget(''); if (!$item->getData()) { $item->setData(''); } if (!$item->getStime()) { $item->setStime(0); } $db = DBConnection::getInstance(); if ($item->getId() == 0) { $query = $db->prepare("INSERT INTO messages\n\t\t\t\t\t(group_id, sender_id, user_id, kind, target, data, ctime, stime)\n\t\t\t\t\tVALUES\n\t\t\t\t\t(:group_id, :sender_id, :user_id, :kind, :target, :data, :ctime, :stime)"); $query->bindParam('group_id', $item->getGroupId()); $query->bindParam('sender_id', $item->getSenderId()); $query->bindParam('user_id', $item->getUserId()); $query->bindParam('kind', $item->getKind()); $query->bindParam('target', $item->getTarget()); $query->bindParam('data', $item->getData()); $query->bindParam('ctime', time()); $query->bindParam('stime', $item->getStime()); $query->execute(); $item->setId($db->lastInsertId()); } else { $sql = ' UPDATE messages SET group_id = :group_id, sender_id = :sender_id, user_id = :user_id, kind = :kind, target = :target, data = :data, stime = :stime WHERE id = :id '; $query = $db->prepare($sql); $query->bindParam('id', $item->getId(), \PDO::PARAM_INT); $query->bindParam('group_id', $item->getGroupId(), \PDO::PARAM_INT); $query->bindParam('sender_id', $item->getSenderId(), \PDO::PARAM_INT); $query->bindParam('user_id', $item->getUserId(), \PDO::PARAM_INT); $query->bindParam('kind', $item->getKind()); $query->bindParam('target', $item->getTarget()); $query->bindParam('data', $item->getData()); $query->bindParam('stime', $item->getStime()); $query->execute(); } return $item; }