Ejemplo n.º 1
0
 /**
  * Generates a URN(Uniform Resource Name) for the given title to uniquely identify a subject(eg: in seo friendly urls)
  * @return mixed boolean false if can't generate a urn, otherwise a string with the urn
  */
 public function generateUrn($title)
 {
     //Translate the text to english as all urns should be english
     if ($translated = SiteLibrary::translate($title)) {
         $title = $translated;
     }
     //then lets generate a clean title(just numbers and letters, remove everything else)
     $clean_title = ereg_replace("[^A-Za-z0-9 ]", "", $title);
     $clean_title = strtolower($clean_title);
     $clean_title = preg_replace('/\\s+/', ' ', $clean_title);
     //replace any sequence of whitespace greater than one, with only one
     $clean_title = trim($clean_title);
     //And replace whitespaces with dashes(this gives us the possible urn)
     $possible_urn = str_replace(" ", "-", $clean_title);
     //Verify that the obtained possible_urn its really unique on the database table
     //if not, generate one adding a sequence number to the possible_urn variable
     //Make 50 attempts to find a unique urn
     for ($i = 1; $i <= 50; $i++) {
         $possible_urn_i = $i == 1 ? $possible_urn : $possible_urn . '_' . $i;
         if (!Subject::model()->find('urn=:urn', array(':urn' => $possible_urn_i))) {
             $urn = $possible_urn_i;
             //we found it
             return $urn;
         }
     }
     return false;
 }