Bolt\Legacy\Storage::updateTaxonomy PHP Method

updateTaxonomy() protected method

Update / insert taxonomy for a given content-unit.
protected updateTaxonomy ( string $contenttype, integer $contentId, array $taxonomy )
$contenttype string
$contentId integer
$taxonomy array
    protected function updateTaxonomy($contenttype, $contentId, $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 = [];
            }
            // 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, [$contentId, $contenttypeslug, $taxonomytype], [\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_STR])->fetchAll();
            if (!empty($currentvalues)) {
                $currentsortorder = $currentvalues[0]['sortorder'];
                $currentvalues = Arr::makeValuePairs($currentvalues, 'id', 'slug');
            } else {
                $currentsortorder = 0;
                $currentvalues = [];
            }
            // 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 = $this->app['slugify']->slugify($slug);
                    }
                }
                if ((!in_array($slug, $currentvalues) || $currentsortorder != $sortorder) && !empty($slug)) {
                    // Insert it!
                    $row = ['content_id' => $contentId, 'contenttype' => $contenttypeslug, 'taxonomytype' => $taxonomytype, 'slug' => $slug, 'name' => $name, 'sortorder' => (int) $sortorder];
                    $this->app['db']->insert($tablename, $row);
                }
            }
            // Convert new slugs to lowercase to compare in the delete process
            $newSlugsNormalised = [];
            foreach ($newslugs as $slug) {
                // If it's like 'desktop#10', split it into value and sortorder.
                list($slug, $sortorder) = explode('#', $slug . "#");
                $slug = $this->app['slugify']->slugify($slug);
                if (!empty($sortorder)) {
                    $slug = $slug . '#' . $sortorder;
                }
                $newSlugsNormalised[] = $slug;
            }
            // 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, $newSlugsNormalised) && !in_array($valuewithorder, $newSlugsNormalised) && !array_key_exists($slugkey, $newSlugsNormalised)) {
                    $this->app['db']->delete($tablename, ['id' => $id]);
                }
            }
        }
    }