Generates a unique slug

@param string $sLabel    The label from which to generate a slug
@param int    $iIgnoreId The ID of an item to ignore

@return string
@throws ModelException
183921
protected function generateSlug(string $sLabel, int $iIgnoreId = null)
    {
        $iCounter = 0;
        $oDb      = Factory::service('Database');
        $sSlug    = Transliterator::transliterate($sLabel);

        do {

            if ($iCounter) {
                $sSlugTest = $sSlug . '-' . $iCounter;
            } else {
                $sSlugTest = $sSlug;
            }

            if ($iIgnoreId) {
                $oDb->where($this->tableIdColumn . ' !=', $iIgnoreId);
            }

            $oDb->where($this->tableSlugColumn, $sSlugTest);
            $iCounter++;

        } while ($oDb->count_all_results($this->getTableName()));

        return $sSlugTest;
    }
1306351
private function getHeaderPasteNewButton(ModelInterface $parentModel, EnvironmentInterface $environment)
    {
        $basicDefinition = $environment->getDataDefinition()->getBasicDefinition();
        if (!$basicDefinition->isCreatable()) {
            return null;
        }

        $filter = new Filter();
        $filter->andModelIsFromProvider($basicDefinition->getDataProvider());
        if ($parentDataProviderName = $basicDefinition->getParentDataProvider()) {
            $filter->andParentIsFromProvider($parentDataProviderName);
        } else {
            $filter->andHasNoParent();
        }

        if ($environment->getClipboard()->isNotEmpty($filter)) {
            return null;
        }
        $dispatcher = $environment->getEventDispatcher();

        /** @var AddToUrlEvent $urlEvent */
        $urlEvent = $dispatcher->dispatch(
            ContaoEvents::BACKEND_ADD_TO_URL,
            new AddToUrlEvent('act=create&amp;pid=' . ModelId::fromModel($parentModel)->getSerialized())
        );

        $parentDefinition = $environment->getParentDataDefinition();
        /** @var GenerateHtmlEvent $imageEvent */
        $imageEvent = $dispatcher->dispatch(
            ContaoEvents::IMAGE_GET_HTML,
            new GenerateHtmlEvent(
                'new.svg',
                $this->translate('pastenew.0', $parentDefinition->getName())
            )
        );

        return \sprintf(
            '<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a>',
            $urlEvent->getUrl(),
            StringUtil::specialchars($this->translate('pastenew.0', $parentDefinition->getName())),
            $imageEvent->getHtml()
        );
    }
482736
protected function makeItem($iElementID, $obElement = null)
    {
        $sItemClass = static::ITEM_CLASS;

        return $sItemClass::make($iElementID, $obElement);
    }
883057
protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
    {
        if (!$this->articleRouteExists($type, $locale)) {
            $routeName = '_slug';
        }
        if (!isset($parameters[$type])) {
            $parameters[$type] = $tagOrCategory;
        }
        if (!isset($parameters['url'])) {
            $parameters['url'] = $slug;
        }
        if (!isset($parameters['_locale'])) {
            $parameters['_locale'] = $locale;
        }

        return $this->router->generate($routeName, $parameters, $referenceType);
    }
332632
private function validateUnique($model, $slug)
    {
        $slug = trim(trim($slug, '/'));
        /* @var $model ActiveRecord */
        $query = SeoSlug::find()
            ->where(['=', 'slug', $slug]);
        if ($model->isNewRecord === false) {
            $query->andWhere([
                'OR',
                [
                    'AND',
                    ['!=', 'model', $model::className()],
                    ['=', 'model_id', $model->getPrimaryKey()],
                ],
                ['!=', 'model_id', $model->getPrimaryKey()],
            ]);
        }
        return $query->count() < 1;
    }
113776
public function createSecureLink($subscriptionId)
    {
        $requestData = new CreateSecureLink\RequestData($subscriptionId);
        $request = new CreateSecureLink\Request($requestData);

        return $this->sendRequest($request, CreateSecureLink\ApiResponse::class);
    }
1192113
protected function describeFieldsPrepareLabel($sLabel)
    {
        $aPatterns = [
            //  Common words
            '/\bid\b/i'   => 'ID',
            '/\burl\b/i'  => 'URL',
            '/\bhtml\b/i' => 'HTML',
            //  Common file extensions
            '/\bpdf\b/i'  => 'PDF',
        ];

        $sLabel = ucwords(preg_replace('/[\-_]/', ' ', $sLabel));
        $sLabel = preg_replace(array_keys($aPatterns), array_values($aPatterns), $sLabel);

        return $sLabel;
    }
412297
public function getId($iId = null)
    {
        $oInput = Factory::service('Input');
        $iId    = (int) $iId ?: (int) $oInput->get('id');

        if (empty($iId)) {
            throw new ApiException('Invalid Invoice ID', 404);
        }

        $oInvoiceModel = Factory::model('Invoice', 'nails/module-invoice');
        $oInvoice      = $oInvoiceModel->getById($iId, ['expand' => ['customer']]);

        if (empty($oInvoice)) {
            throw new ApiException('Invalid Invoice ID', 404);
        }

        return Factory::factory('ApiResponse', 'nails/module-api')
                      ->setData($this->formatInvoice($oInvoice));
    }
910217
public function render(Framework $framework, MenuEntry $menuEntry)
    {
        // If the MenuEntry hasn't any children we return an empty string.
        if (!$menuEntry->hasChildren()) {
            return '';
        }
        
        $templatesManager = $framework->getInstance('\\Zepi\\Web\\General\\Manager\\TemplatesManager');
        $htmlString = '';

        $emptyChilds = array();
        foreach ($menuEntry->getChildren() as $child) {
            if (!$child->hasChildren()) {
                $emptyChilds[] = $child;
            } else {
                $htmlString .= $templatesManager->renderTemplate('\\Zepi\\Web\\UserInterface\\Templates\\OverviewPageSection', array(
                    'menuEntry' => $child
                ));
            }
        }
        
        $htmlString = $templatesManager->renderTemplate('\\Zepi\\Web\\UserInterface\\Templates\\OverviewPageItems', array(
            'children' => $emptyChilds
        )) . $htmlString;
        
        return $htmlString;
    }
1128536
protected function getExistingSlug($slug, $excludeSelf = false)
    {
        $existing = static::where('slug', "$slug");

        if (isset($this->isSlugToggled) && $this->isSlugToggled == true) {
            $existing->where('is_sluggable', true);
        }

        if ($excludeSelf) {
            $existing->where('id', '!=', $this->id);
        }

        return $existing->first() ? true : false;
    }
42763
private function slugify($string, $replacement = '-', $lowercase = true)
    {
        $transliterateOptions = $this->transliterateOptions !== null ? $this->transliterateOptions : 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFKC;';
        return (new Slugifier($transliterateOptions, $replacement, $lowercase))->slugify($string);
    }
377083
private function createSliceID()
    {
        $urlFilter = URLSegmentFilter::create();
        if ($sliceID = $urlFilter->filter($this->getField('Title'))) {
            if (!$this->Parent()->PageSlices()->filter(array('ID:not' => $this->ID, 'SliceID' => $sliceID))->exists()) {
                $this->setField('SliceID', $sliceID);
            } else {
                $this->setField('SliceID', "$sliceID-{$this->ID}");
            }
        }
    }
410643
protected function makeSlugUnique(string $slug, array $config, string $attribute): string
    {
        if (!$config['unique']) {
            return $slug;
        }

        $separator = $config['separator'];

        // find all models where the slug is like the current one
        $list = $this->getExistingSlugs($slug, $attribute, $config);

        // if ...
        // 	a) the list is empty, or
        // 	b) our slug isn't in the list
        // ... we are okay
        if (
            $list->count() === 0 ||
            $list->contains($slug) === false
        ) {
            return $slug;
        }

        // if our slug is in the list, but
        // 	a) it's for our model, or
        //  b) it looks like a suffixed version of our slug
        // ... we are also okay (use the current slug)
        if ($list->has($this->model->getKey())) {
            $currentSlug = $list->get($this->model->getKey());

            if (
                $currentSlug === $slug ||
                strpos($currentSlug, $slug) === 0
            ) {
                return $currentSlug;
            }
        }

        $method = $config['uniqueSuffix'];
        if ($method === null) {
            $suffix = $this->generateSuffix($slug, $separator, $list);
        } elseif (is_callable($method)) {
            $suffix = $method($slug, $separator, $list);
        } else {
            throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
        }

        return $slug . $separator . $suffix;
    }
939250
protected function getUniquePageHandle($handle, $parentID)
    {
        $uniqueHandle = $handle;
        $parentID = (int) $parentID;
        $index = 1;
        for (; ;) {
            $pageWithSameHandle = $this->connection->fetchColumn(
                'select Pages.cID from CollectionVersions inner join Pages on (CollectionVersions.cID = Pages.cID and CollectionVersions.cvIsApproved = 1) where Pages.cParentID = ? and CollectionVersions.cvHandle = ? limit 1',
                [$parentID, $uniqueHandle]
            );
            if ($pageWithSameHandle === false) {
                return $uniqueHandle;
            }
            ++$index;
            $uniqueHandle = $handle . '-' . $index;
        }
    }
498185
private function buildPayload( $type, $to, $templateId, array $personalisation, $reference ){

        $payload = [
            'template_id'=> $templateId
        ];

        if ( $type == 'sms' ) {
            $payload['phone_number'] = $to;
        } else if ( $type == 'email' ) {
            $payload['email_address'] = $to;
        }

        if ( count($personalisation) > 0 ) {
            $payload['personalisation'] = $personalisation;
        }

        if ( isset($reference) && $reference != '' ) {
            $payload['reference'] = $reference;
        }

        return $payload;

    }
1200766
public function makeUnique($id, $slug) {
        $repo = $this->getRepository();
        $scope = $this->getConfig('scope');

        /** @type \Titon\Db\Query $query */
        foreach ([
            $repo->select()->where($this->getConfig('slug'), $slug),
            $repo->select()->where($this->getConfig('slug'), 'like', $slug . '%')
        ] as $i => $query) {
            if ($scope) {
                $query->bindCallback($scope);
            }

            if ($id) {
                $query->where($repo->getPrimaryKey(), '!=', $id);
            }

            $count = $query->count();

            if ($count <= 0) {
                break;

            } else if ($i) {
                return $slug . '-' . $count;
            }
        }

        return $slug;
    }
1013381
public function loadTemplate($templateIdent)
    {
        if (!is_string($templateIdent)) {
            throw new InvalidArgumentException(sprintf(
                'Template ident must be a string, received %s',
                is_object($templateIdent) ? get_class($templateIdent) : gettype($templateIdent)
            ));
        }
        if (!$templateIdent) {
            return '';
        }
        return $this->engine()->loadTemplate($templateIdent);
    }