/** * Import mark object into system * * @param stdObj $markObject * Mark data imported from file * @return array Result array */ public function importMark($markObject) { $result = array(); $this->CI->load->helper('data_helper'); // Run in transaction $this->CI->db->trans_start(); if ($this->importData['meta']['export_version'] == 1) { $this->CI->load->model('marks_model', 'mark'); $markArray = array('created_on' => $markObject->created_on, 'title' => empty($markObject->title) ? 'No title' : $markObject->title, 'url' => $markObject->url, 'embed' => $markObject->embed); // Import mark object $mark = $this->CI->mark->import($markArray); // Succesfully created mark if ($mark !== false && isset($mark->mark_id)) { // Try to create user_mark and other related records $this->CI->load->model('users_to_marks_model', 'user_marks'); $user_mark = $this->CI->user_marks->readComplete("users_to_marks.user_id = '" . $this->importData['user_id'] . "' AND users_to_marks.mark_id = '" . $mark->mark_id . "' AND users_to_marks.active = '1'"); // User mark does not exist - add one if (!isset($user_mark->mark_id)) { // Set default options $options = array('user_id' => $this->importData['user_id'], 'mark_id' => $mark->mark_id, 'active' => $markObject->active, 'archived_on' => $markObject->archived_on, 'created_on' => $markObject->created_on); // Label ID (not required) if (isset($markObject->label_id) && is_numeric($markObject->label_id)) { $this->CI->load->model('labels_model', 'labels'); $label = $this->CI->labels->readComplete("(labels.user_id IS NULL OR labels.user_id='" . $this->importData['user_id'] . "') AND labels.active='1' AND labels.name = " . $this->CI->db->escape($markObject->label_name), 1); if (!empty($label) && isset($label->label_id)) { $options['label_id'] = $label->label_id; } else { if (!empty($this->unlabeled_label_id)) { $options['label_id'] = $this->unlabeled_label_id; $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Marked as Unlabeled.'; } else { if ($this->unlabeled_label_id === false) { $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Stripped label info.'; } else { // Label not found and no unlabeled cache - looking for unlabeled label id $label = $this->CI->labels->readComplete("(labels.user_id IS NULL OR labels.user_id='" . $this->importData['user_id'] . "') AND labels.active='1' AND labels.name = " . $this->CI->db->escape('Unlabeled'), 1); if (!empty($label) && isset($label->label_id)) { $options['label_id'] = $label->label_id; // Cache the id of unlabeled label id $this->unlabeled_label_id = $label->label_id; $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Marked as Unlabeled.'; } else { // There is no unlabeled label - cache invalid value to mark $this->unlabeled_label_id = false; $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Stripped label info.'; } } } } } // Notes (not required) if (isset($markObject->notes) && !empty($markObject->notes)) { $options['notes'] = $markObject->notes; $tags = getTagsFromHash($options['notes']); } // Figure if any automatic labels should be applied $smart_info = getSmartLabelInfo($markObject->url); if (isset($smart_info['key']) && !empty($smart_info['key']) && !isset($options['label_id'])) { // Load labels model // Sort by user_id DESC (if user has same rule as system, use the user's rule) // Try to extract label $this->CI->load->model('labels_model', 'labels'); $this->CI->labels->sort = 'user_id DESC'; $label = $this->CI->labels->readComplete("(labels.user_id IS NULL OR labels.user_id = '" . $this->importData['user_id'] . "') AND labels.smart_key = '" . $smart_info['key'] . "' AND labels.active = '1'", 1); // If a label id is found // Set it to options to save if (isset($label->settings->label->id)) { $options['label_id'] = $label->settings->label->id; } } // Create the mark $user_mark = $this->CI->user_marks->import($options); $result['result'] = 'added'; } else { $result['result'] = 'skipped'; } // Added user mark if (isset($user_mark->mark_id)) { // If tags are present, add them // Get updated result if (isset($tags)) { self::addTags($tags, $user_mark->mark_id); } } } else { if ($mark !== false) { foreach ($mark as $errorCode => $errorMessage) { $result['errors'][] = array('error_code' => $errorCode, 'error_message' => $errorMessage); } } else { $result['errors'][] = formatErrors(500); } } } else { $result['errors'][] = array('error_message' => 'Invalid data format ' . $this->importData['meta']['export_version']); } $this->CI->db->trans_complete(); // Check if DB operations succeeded if ($this->CI->db->trans_status() === FALSE) { // Internal error $result['errors'][] = formatErrors(500); } if (!empty($result['errors'])) { $result['result'] = 'failed'; } return $result; }
public function edit($mark_id = 0) { parent::redirectIfWebView(); // Figure correct way to handle if no mark id if (empty($mark_id) || !is_numeric($mark_id)) { header('Location: /'); exit; } // Check for CSRF if ($this->csrf_valid === true) { // Figure what options to send for update $options = array(); // 1.6 // If title is found, attach it if (isset($this->db_clean->title)) { $options['mark_title'] = $this->db_clean->title; } // If label ID is found, attach it if (isset($this->clean->label_id) && is_numeric($this->clean->label_id)) { $options['label_id'] = $this->clean->label_id; } // If notes are present set them if (isset($this->db_clean->notes)) { $options['notes'] = $this->db_clean->notes; // Check for hashmarks to tags $tags = getTagsFromHash($options['notes']); } // If tags are present, handle differently // Need to add to tags table first // Then create association // If notes are present set them if (isset($tags)) { parent::addTags($tags, $mark_id); } // Update users_to_marks record $mark = $this->user_marks->update("users_to_marks.user_id = '" . $this->user_id . "' AND users_to_marks.users_to_mark_id = '" . $mark_id . "'", $options); // Check if it was updated if ($mark === false) { $this->data['errors'] = formatErrors(3); } else { $this->data['mark'] = $mark; // Check if label id was set // if so get the parent mark id // Then add a smart label for this domain if (isset($options['label_id']) && !empty($options['label_id'])) { $this->load->model('labels_model', 'labels'); $smart_info = getSmartLabelInfo($mark->url); $total = $this->labels->count("labels.user_id = '" . $this->user_id . "' AND labels.smart_key = '" . $smart_info['key'] . "'"); // If not found, create it with label // Else update current if ($total < 1 && $options['label_id'] != '1') { $label = $this->labels->create(array('smart_label_id' => $options['label_id'], 'domain' => $smart_info['domain'], 'smart_key' => $smart_info['key'], 'user_id' => $this->user_id)); } else { $active = $options['label_id'] == '1' ? '0' : '1'; $label = $this->labels->update("labels.user_id = '" . $this->user_id . "' AND labels.smart_key = '" . $smart_info['key'] . "'", array('smart_label_id' => $options['label_id'], 'active' => $active)); } } } } else { $this->data['errors'] = formatErrors(600); } // Figure what to do here (api, redirect or generate view) $this->renderJSON(); }
protected function addMark($data = array()) { $this->load->helper('http_build_url'); // Set empty $result $result = array(); // Set default view & redirect $view = null; $redirect = null; // Figure the url and title $url = isset($data['url']) ? $data['url'] : null; $title = isset($data['title']) ? $data['title'] : null; // If url or title are empty // error out if (empty($url)) { return formatErrors(8); } // 1.6 // Remove utm_* from URLs before saving // Just grab the querystring $url_query_parameters = parse_url($url, PHP_URL_QUERY); // If utm_ is found, convert to array // Notice the _... so that words with utm do not get included if (strpos($url_query_parameters, 'utm_') === false) { // Whoops! echo '<br>No utm_ found.<br>'; } else { parse_str($url_query_parameters, $url_query_parameters); // convert querystring to array // Prepare a new query string $new_query_string = array(); // Loop through and reconstruct the querystring, leaving off anything with utm_ in key foreach ($url_query_parameters as $key => $value) { if (strpos($key, 'utm_') === false) { $new_query_string[] = $key . '=' . $value; } } $new_query_string = implode('&', $new_query_string); $url = http_build_url($url, array('query' => $new_query_string)); } // Sometimes browsers will not have a title // for things like a PDF or JPG. // In these instances, we still want people // to be able to save their marks. ;-) if (empty($title)) { $parsed_url_path = parse_url($url, PHP_URL_PATH); // If a path is found at all... use that for title instead if (!empty($parsed_url_path)) { $title = $parsed_url_path; } } if (empty($title)) { return formatErrors(9); } // Add mark to marks table $this->load->model('marks_model', 'mark'); $mark = $this->mark->create(array('title' => $title, 'url' => $url)); // Check ish if ($mark === false) { $user_mark = formatErrors(6); } elseif (!isset($mark->mark_id)) { $user_mark = $mark; } else { $this->load->model('users_to_marks_model', 'user_marks'); $user_mark = $this->user_marks->readComplete("users_to_marks.user_id = '" . $this->user_id . "' AND users_to_marks.mark_id = '" . $mark->mark_id . "' AND users_to_marks.active = '1'"); // Add if (!isset($user_mark->mark_id)) { // Set default options $options = array('user_id' => $this->user_id, 'mark_id' => $mark->mark_id); // Label ID (not required), if set and numeric and greater than 0, use it if (isset($data['label_id']) && is_numeric($data['label_id']) && $data['label_id'] > 0) { $options['label_id'] = $data['label_id']; } // Notes (not required) if (isset($data['notes']) && !empty($data['notes'])) { $options['notes'] = $data['notes']; $tags = getTagsFromHash($options['notes']); } // Figure if any automatic labels should be applied $smart_info = getSmartLabelInfo($url); if (isset($smart_info['key']) && !empty($smart_info['key']) && !isset($options['label_id'])) { // Load labels model // Sort by user_id DESC (if user has same rule as system, use the user's rule) // Try to extract label $this->load->model('labels_model', 'labels'); $this->labels->sort = 'user_id DESC'; $label = $this->labels->readComplete("(labels.user_id IS NULL OR labels.user_id = '" . $this->user_id . "') AND labels.smart_key = '" . $smart_info['key'] . "' AND labels.active = '1'", 1); // If a label id is found // Set it to options to save if (isset($label->settings->label->id)) { $options['label_id'] = $label->settings->label->id; } } // Create the mark $user_mark = $this->user_marks->create($options); } if ($user_mark === false) { $user_mark = formatErrors(6); } elseif (isset($user_mark->mark_id)) { // If tags are present, add them // Get updated result if (isset($tags)) { $mark_id = $user_mark->mark_id; self::addTags($tags, $mark_id); $user_mark = $this->user_marks->readComplete($mark_id); } } } return $user_mark; }