/** * Update / insert taxonomy for a given content-unit. * * @param string $contenttype * @param integer $content_id * @param array $taxonomy */ protected function updateTaxonomy($contenttype, $content_id, $taxonomy) { $tablename = $this->prefix . "taxonomy"; foreach ($taxonomy as $taxonomytype => $newvalues) { if (!is_array($newvalues)) { $newvalues = explode(",", $newvalues); } // Get the current values from the DB.. $query = "SELECT id, slug FROM {$tablename} WHERE content_id=? AND contenttype=? AND taxonomytype=?"; $currentvalues = $this->db->fetchAll($query, array($content_id, $contenttype, $taxonomytype)); $currentvalues = makeValuePairs($currentvalues, 'id', 'slug'); // Add the ones not yet present.. foreach ($newvalues as $value) { if (!in_array($value, $currentvalues) && !empty($value)) { // Insert it! $row = array('content_id' => $content_id, 'contenttype' => $contenttype, 'taxonomytype' => $taxonomytype, 'slug' => $value); $this->db->insert($tablename, $row); } } // Delete the ones that have been removed. // Add the ones not yet present.. foreach ($currentvalues as $id => $value) { if (!in_array($value, $newvalues)) { // Delete it! $row = array('content_id' => $content_id, 'contenttype' => $contenttype, 'taxonomytype' => $taxonomytype, 'slug' => $value); $this->db->delete($tablename, array('id' => $id)); // echo "delete: $id, $value<br />"; } } } }
/** * Update / insert taxonomy for a given content-unit. * * @param string $contenttype * @param integer $content_id * @param array $taxonomy */ protected function updateTaxonomy($contenttype, $content_id, $taxonomy) { $tablename = $this->getTablename("taxonomy"); $configTaxonomies = $this->app['config']->get('taxonomy'); // Make sure $contenttypeslug is a 'slug' if (is_array($contenttype)) { $contenttypeslug = $contenttype['slug']; } else { $contenttypeslug = $contenttype; } // If our contenttype has no taxonomies, there's nothing for us to do here. if (!isset($contenttype['taxonomy'])) { return; } foreach ($contenttype['taxonomy'] as $taxonomytype) { // Set 'newvalues to 'empty array' if not defined if (!empty($taxonomy[$taxonomytype])) { $newslugs = $taxonomy[$taxonomytype]; } else { $newslugs = array(); } // Get the current values from the DB.. $query = sprintf("SELECT id, slug, sortorder FROM %s WHERE content_id=? AND contenttype=? AND taxonomytype=?", $tablename); $currentvalues = $this->app['db']->executeQuery($query, array($content_id, $contenttypeslug, $taxonomytype), array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_STR))->fetchAll(); if (!empty($currentvalues)) { $currentsortorder = $currentvalues[0]['sortorder']; $currentvalues = makeValuePairs($currentvalues, 'id', 'slug'); } else { $currentsortorder = 'id'; $currentvalues = array(); } // Add the ones not yet present.. foreach ($newslugs as $slug) { // If it's like 'desktop#10', split it into value and sortorder.. list($slug, $sortorder) = explode('#', $slug . "#"); if (empty($sortorder)) { $sortorder = 0; } // Make sure we have a 'name'. if (isset($configTaxonomies[$taxonomytype]['options'][$slug])) { $name = $configTaxonomies[$taxonomytype]['options'][$slug]; } else { $name = ""; } if ((!in_array($slug, $currentvalues) || $currentsortorder != $sortorder) && !empty($slug)) { // Insert it! $row = array('content_id' => $content_id, 'contenttype' => $contenttypeslug, 'taxonomytype' => $taxonomytype, 'slug' => $slug, 'name' => $name, 'sortorder' => $sortorder); $this->app['db']->insert($tablename, $row); } } // Delete the ones that have been removed. foreach ($currentvalues as $id => $slug) { // Make it look like 'desktop#10' $valuewithorder = $slug . "#" . $currentsortorder; if (!in_array($slug, $newslugs) && !in_array($valuewithorder, $newslugs)) { $this->app['db']->delete($tablename, array('id' => $id)); } } } }
/** * Update / insert taxonomy for a given content-unit. * * @param string $contenttype * @param integer $content_id * @param array $taxonomy */ protected function updateTaxonomy($contenttype, $content_id, $taxonomy) { $tablename = $this->getTablename("taxonomy"); $configTaxonomies = $this->app['config']->get('taxonomy'); // Make sure $contenttypeslug is a 'slug' if (is_array($contenttype)) { $contenttypeslug = $contenttype['slug']; } else { $contenttypeslug = $contenttype; } // If our contenttype has no taxonomies, there's nothing for us to do here. if (!isset($contenttype['taxonomy'])) { return; } foreach ($contenttype['taxonomy'] as $taxonomytype) { // Set 'newvalues to 'empty array' if not defined if (!empty($taxonomy[$taxonomytype])) { $newslugs = $taxonomy[$taxonomytype]; } else { $newslugs = array(); } // Get the current values from the DB.. $query = sprintf("SELECT id, slug, sortorder FROM %s WHERE content_id=? AND contenttype=? AND taxonomytype=?", $tablename); $currentvalues = $this->app['db']->executeQuery($query, array($content_id, $contenttypeslug, $taxonomytype), array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_STR))->fetchAll(); if (!empty($currentvalues)) { $currentsortorder = $currentvalues[0]['sortorder']; $currentvalues = makeValuePairs($currentvalues, 'id', 'slug'); } else { $currentsortorder = 'id'; $currentvalues = array(); } // Add the ones not yet present.. foreach ($newslugs as $slug) { // If it's like 'desktop#10', split it into value and sortorder.. list($slug, $sortorder) = explode('#', $slug . "#"); // @todo clean up and/or refactor // If you save this content via anything other than the Bolt // backend (see Content->setFromPost), then taxonomies that // behave like groupings, will have their sortorders reset to 0. if ($configTaxonomies[$taxonomytype]['behaves_like'] == 'grouping' && empty($sortorder) && $sortorder !== '0') { $sortorder = $currentsortorder; } if (empty($sortorder)) { $sortorder = 0; } // Make sure we have a 'name'. if (isset($configTaxonomies[$taxonomytype]['options'][$slug])) { $name = $configTaxonomies[$taxonomytype]['options'][$slug]; } else { $name = $slug; } // Make sure the slug is also set correctly if (!isset($configTaxonomies[$taxonomytype]['options'][$slug])) { // Assume we passed a value, instead of a slug. Turn it back into a proper slug if (isset($configTaxonomies[$taxonomytype]['options']) && is_array($configTaxonomies[$taxonomytype]['options']) && array_search($slug, $configTaxonomies[$taxonomytype]['options'])) { $slug = array_search($slug, $configTaxonomies[$taxonomytype]['options']); } else { // make sure it's at least a slug-like value. $slug = makeSlug($slug); } } if ((!in_array($slug, $currentvalues) || $currentsortorder != $sortorder) && !empty($slug)) { // Insert it! $row = array('content_id' => $content_id, 'contenttype' => $contenttypeslug, 'taxonomytype' => $taxonomytype, 'slug' => $slug, 'name' => $name, 'sortorder' => $sortorder); $this->app['db']->insert($tablename, $row); } } // Delete the ones that have been removed. foreach ($currentvalues as $id => $slug) { // Make it look like 'desktop#10' $valuewithorder = $slug . "#" . $currentsortorder; $slugkey = '/' . $configTaxonomies[$taxonomytype]['slug'] . '/' . $slug; if (!in_array($slug, $newslugs) && !in_array($valuewithorder, $newslugs) && !array_key_exists($slugkey, $newslugs)) { $this->app['db']->delete($tablename, array('id' => $id)); } } } }