CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to find the function name when that's a 2 words entity?
Pages: [1]

Author Topic: How to find the function name when that's a 2 words entity?  (Read 1715 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
How to find the function name when that's a 2 words entity?
February 21, 2011, 12:27:48 am
Hi,

Eileen pointed out that we don't properly handle the MembershipContact and other 2 words entities.

I'm tempted to solve the problem by renaming the functions, but wanted to check if there is a function somewhere that translates MembershipContact into membership_contact so I can use it for the functions.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to find the function name when that's a 2 words entity?
February 21, 2011, 12:50:59 am
I can  play around preg_split('/(?=[A-Z])/',$entity);

or probably more [A-Z|UF] but if the function exists already, I'd rather reuse it.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: How to find the function name when that's a 2 words entity?
February 21, 2011, 02:38:58 am
Hmm - this is what I used in the function that generated the examples. I can see from the comment that it was waiting for a Belgian touch

Code: [Select]
        if (strstr($entity,'UF')){// a cleverer person than me would do it in a single regex
         $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)(?<=UF)[A-Z]/','_$0', $entity));         
        }else{
        $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)[A-Z]/','_$0', $entity));
        } 
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to find the function name when that's a 2 words entity?
February 21, 2011, 03:41:41 am
Belgium are known for their french fries, german beers and swiss chocolates. Wasn't aware of any regex skill, but given how wicked their political system is, I should have guessed ;)

Code: [Select]
$fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)[A-Z|UF]/','_$0', $entity));
Might help you. Will give it a shot if no one mentions an existing helper function.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

donquixote

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
  • CiviCRM version: none
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: How to find the function name when that's a 2 words entity?
February 22, 2011, 11:52:41 am
I made this function one day (found it in gitorious).
It is adapted from the linked blog post, but I don't remember if it is identical.
The tricky part was how to deal with multi-uppercase, like in "BBCode" or "EatABanana".
In some cases we want eat_a_banana, in others we want eat_abanana".
I don't remember what this one does exactly.

EDIT: Now I remember!!!
This was a genius piece of mine :)
The example string is used for configuration, so the user does not have to remember some arbitrarily named options.
Example: You want that "EatABanana" explodes as "Eat A Banana", not "Eat ABanana". You put "Eat A Banana" as a the example string.
Now if you want to explode "VerticalYDim", it would become "Vertical Y Dim" and not "Vertical YDim".
On the other hand, with an example string of "Eat ABanana", the "VerticalYDim" would explode as "Vertical YDim".
The overhead for this convenience is only executed one time, if you can stick with one example string.
Wow.
I think this is why I remembered this piece.
Does it work? No idea..
And the comment block is not very accurate, I must say.

EDIT:
Updated the comment block.


(external link modified so I am allowed to post it)

Quote
    /**
     * thanks a lot Charl van Niekerk, http:/ /blog.charlvn.za.net/2007/11/php-camelcase-explode-20.html
     *
     * @param $string :string
     *   The original string, that we want to explode.
     *
     * @param $lowercase :boolean
     *   should the result be lowercased?
     *
     * @param $example_string :string
     *   Example to specify how to deal with multiple uppercase characters.
     *   Can be something like "AA Bc" or "A A Bc" or "AABc".
     *
     * @param $glue :boolean
     *   Allows to implode the fragments with sth like "_" or "." or " ".
     *   If $glue is FALSE, it will just return an array.
     *
     * @return :array[int => string] or just string, depending on $glue.
     */
    function camelCaseExplode($string, $lowercase = true, $example_string = 'AA Bc', $glue = false)
    {
        static $regexp_available = array(
            '/([A-Z][^A-Z]*)/',
            '/([A-Z][^A-Z]+)/',
            '/([A-Z]+[^A-Z]*)/',
        );
        static $regexp_by_example = array();
        if (!isset($regexp_by_example[$example_string])) {
            $example_array = explode(' ', $example_string);
            foreach ($regexp_available as $regexp) {
                if (implode(' ', preg_split(
                    $regexp,
                    str_replace(' ', '', $example_string),
                    -1,
                    PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
                )) == $example_string) {
                    break;
                }
            }
            $regexp_by_example[$example_string] = $regexp;
        }
        $array = preg_split(
            $regexp_by_example[$example_string],
            $string,
            -1,
            PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
        );
        if ($lowercase) $array = array_map('strtolower', $array);
        return is_string($glue) ? implode($glue, $array) : $array;
    }
« Last Edit: February 22, 2011, 02:52:34 pm by donquixote »

donquixote

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
  • CiviCRM version: none
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: How to find the function name when that's a 2 words entity?
February 22, 2011, 11:54:33 am
Btw, the captcha settings in this forum are very paranoid.
And I was not allowed to use the code tags ("[ code ]") ...

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to find the function name when that's a 2 words entity?
February 22, 2011, 12:39:24 pm
Hi Andreas,

That's until you got a handful of posts, after a while, mr forum gets to know you and become relaxed ;)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

donquixote

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
  • CiviCRM version: none
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: How to find the function name when that's a 2 words entity?
February 22, 2011, 02:54:49 pm
Check the updates of the above post.
And please tell me if this actually works! I want to blog it, if it does.

Forum is my friend now :) no more bullies at the door.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to find the function name when that's a 2 words entity?
March 01, 2011, 01:31:20 pm
Didn't find the holy regexp, but added a str_replace in the chain and voila, seems to work

api/api.php Committed revision 32774

Code: [Select]
$function = strtolower(str_replace('U_F','uf', preg_replace('/(?=[A-Z])/','_$0', $entity)));
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to find the function name when that's a 2 words entity?

This forum was archived on 2017-11-26.